1. 面向对象编程 (OOP) 的高级特性
1.1 继承 (Inheritance)
- 定义:子类继承父类的属性和方法,可以重用代码,提高代码的可维护性和可扩展性。
- 语法:
public class Animal { public void eat() { System.out.println("This animal eats food."); } } public class Dog extends Animal { public void bark() { System.out.println("The dog barks."); } }
- 示例:
public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // 从父类继承的方法 dog.bark(); // 子类的方法 } }
1.2 方法重写 (Method Overriding)
- 定义:子类可以重写父类的方法,实现多态性。
- 语法:
public class Animal { public void sound() { System.out.println("Animal makes a sound"); } } public class Dog extends Animal { @Override public void sound() { System.out.println("Dog barks"); } }
- 示例:
public class MathOperations { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }
1.3 方法重载 (Method Overloading)
- 定义:在同一个类中,方法名相同,但参数不同(数量或类型不同)。
- 语法:
public class MathOperations { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }
- 示例:
public class Main { public static void main(String[] args) { MathOperations math = new MathOperations(); System.out.println(math.add(5, 3)); // 输出:8 System.out.println(math.add(5.5, 3.2)); // 输出:8.7 } }
2. 抽象类和接口
2.1 抽象类 (Abstract Class)
- 定义:不能实例化的类,包含抽象方法(没有方法体),需要子类实现这些方法。
- 语法:
public abstract class Animal { public abstract void makeSound(); } public class Dog extends Animal { @Override public void makeSound() { System.out.println("Dog barks"); } }
- 示例:
public class Main { public static void main(String[] args) { Animal myDog = new Dog(); myDog.makeSound(); // 输出:Dog barks } }
2.2 接口 (Interface)
- 定义:接口是抽象方法的集合,类通过实现接口来定义特定的行为。
- 语法:
public interface Animal { void makeSound(); } public class Dog implements Animal { @Override public void makeSound() { System.out.println("Dog barks"); } }
- 示例:
public class Main { public static void main(String[] args) { Animal myDog = new Dog(); myDog.makeSound(); // 输出:Dog barks } }
3. Java 标准库
3.1 常用类
-
String 类:处理字符串
String str = "Hello, World!"; System.out.println(str.length()); // 输出:13 System.out.println(str.toUpperCase()); // 输出:HELLO, WORLD!
-
Math 类:数学运算
System.out.println(Math.max(5, 10)); // 输出:10 System.out.println(Math.sqrt(16)); // 输出:4.0
-
ArrayList 类:动态数组
ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); System.out.println(list.get(0)); // 输出:Apple
3.2 输入输出
-
读取用户输入:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Hello, " + name); } }
-
文件操作:
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) { try { // 写入文件 FileWriter writer = new FileWriter("example.txt"); writer.write("Hello, World!"); writer.close(); // 读取文件 File file = new File("example.txt"); Scanner reader = new Scanner(file); while (reader.hasNextLine()) { String line = reader.nextLine(); System.out.println(line); } reader.close(); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } }
小结
- 本课重点学习了面向对象编程的高级特性,包括继承、方法重写和方法重载。
- 介绍了抽象类和接口的定义与使用。
- 学习了Java标准库中的常用类和输入输出操作。