Java入门教程(下)

Java入门教程(下)

在上一篇文章中,我们介绍了Java的基础知识,包括Java简介、开发环境安装、基本语法、变量和数据类型、运算符和控制结构。本文将继续探讨Java的面向对象编程(OOP)思想和常用的Java API。

3. 面向对象编程(OOP)

3.1 类和对象

Java是一种面向对象的编程语言,类和对象是其核心概念。类是对象的模板,对象是类的实例。

示例:

java 复制代码
public class Person {
    String name;
    int age;

    // 构造方法
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 方法
    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

    public static void main(String[] args) {
        Person person = new Person("John", 25);
        person.displayInfo();
    }
}

3.2 继承

继承是OOP的一个重要特性,通过继承,一个类可以获得另一个类的属性和方法。使用extends关键字来实现继承。

示例:

复制代码
java复制代码public class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

public class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();  // 继承的父类方法
        dog.bark(); // 子类的方法
    }
}

3.3 多态

多态是指同一操作在不同对象上表现出不同的行为。通过方法重写和接口实现来实现多态。

示例:

复制代码
java复制代码public class Animal {
    void sound() {
        System.out.println("This animal makes a sound.");
    }
}

public class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("The dog barks.");
    }
}

public class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("The cat meows.");
    }

    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.sound(); // 输出:The dog barks.
        myCat.sound(); // 输出:The cat meows.
    }
}

3.4 封装

封装是指将对象的属性私有化,并提供公共的访问方法(getter和setter)来控制对属性的访问。

示例:

复制代码
java复制代码public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void main(String[] args) {
        Person person = new Person();
        person.setName("John");
        person.setAge(25);

        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}

4. 常用的Java API

4.1 字符串操作

Java提供了丰富的字符串操作方法。常用的字符串类是String。

示例:

复制代码
java复制代码public class StringExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        
        // 获取字符串的长度
        int length = str.length();
        System.out.println("Length: " + length);

        // 获取子字符串
        String substr = str.substring(7, 12);
        System.out.println("Substring: " + substr);

        // 字符串替换
        String replacedStr = str.replace("World", "Java");
        System.out.println("Replaced String: " + replacedStr);

        // 字符串拼接
        String newStr = str.concat(" Welcome to Java.");
        System.out.println("Concatenated String: " + newStr);
    }
}

4.2 集合框架

Java的集合框架提供了一组接口和类,用于存储和操作一组对象。常用的集合类包括ArrayList、HashMap等。

示例:

复制代码
java复制代码import java.util.ArrayList;
import java.util.HashMap;

public class CollectionExample {
    public static void main(String[] args) {
        // ArrayList示例
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        System.out.println("ArrayList: " + list);

        // HashMap示例
        HashMap<String, Integer> map = new HashMap<>();
        map.put("Apple", 1);
        map.put("Banana", 2);
        map.put("Cherry", 3);
        System.out.println("HashMap: " + map);
    }
}

4.3 文件操作

Java的java.io包提供了一组类和接口,用于文件操作。常用的文件操作类包括File、FileReader、FileWriter等。

示例:

复制代码
java复制代码import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileExample {
    public static void main(String[] args) {
        // 创建文件
        File file = new File("example.txt");

        try {
            if (file.createNewFile()) {
                System.out.println("File created: " + file.getName());
            } else {
                System.out.println("File already exists.");
            }

            // 写入文件
            FileWriter writer = new FileWriter(file);
            writer.write("Hello, World!");
            writer.close();
            System.out.println("Successfully wrote to the file.");

            // 读取文件
            FileReader reader = new FileReader(file);
            int character;
            while ((character = reader.read()) != -1) {
                System.out.print((char) character);
            }
            reader.close();
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

结论

通过本系列文章,我们详细介绍了Java的基础知识和编程技巧,从Java简介、基本语法到面向对象编程和常用的Java API。希望这些内容能帮助你更好地理解和掌握Java编程。


欢迎大家在评论区分享你们在学习Java过程中遇到的问题和经验,一起交流学习。

相关推荐
ym hyd 1115 小时前
慢性病精细化管理平台源码 Java+SpringBoot+Vue3 前后分离
java·vue.js·spring boot·毕设
程序猿乐锅5 小时前
【黑马点评 | 第七篇】Redis 分布式锁的两种实现
java·数据库·redis·分布式·spring·缓存
SL_staff5 小时前
JVS私有化交付为何敢承诺100%源码开放与无兜底风险?
java·低代码·全栈
j7~5 小时前
【Python】(篇七)《使用Python库》 -- 详解
开发语言·后端·python·编程学习·python标准库·python第三方库·python拓展
心中有你02145 小时前
C语言实现文件异或加密与RLE压缩算法
c语言·开发语言
需要8265 小时前
MySQL MVCC 与事务隔离级别:从一条 update 看版本链
java·数据库·spring boot·mysql·spring cloud
Memory_荒年5 小时前
订单超时未支付?从“定时扫库”一步步到最终形态
java·后端
小宋10215 小时前
MCP 工具也有供应链风险:注册中心、签名校验、版本锁定与白名单
开发语言·网络·人工智能·php
凉茶钱5 小时前
【C++】入门基础语法
开发语言·c++
个 人 练 习 生6 小时前
C++中的内存管理
开发语言·c++·经验分享·学习