一、类与对象
类是对象的模板,对象是类的实例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class Student { private String name; private int age;
public Student(String name, int age) { this.name = name; this.age = age; }
public void study() { System.out.println(name + " 正在学习"); }
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 class Main { public static void main(String[] args) { Student s1 = new Student("小明", 20); s1.study(); System.out.println(s1.getName()); } }
|
二、封装
用 private 保护数据,通过 public 方法对外暴露有限访问,这就是封装。
| 修饰符 |
当前类 |
同包 |
子类 |
其他包 |
private |
✅ |
|
|
|
| 默认(无修饰符) |
✅ |
✅ |
|
|
protected |
✅ |
✅ |
✅ |
|
public |
✅ |
✅ |
✅ |
✅ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class BankAccount { private double balance;
public void deposit(double amount) { if (amount > 0) { balance += amount; } }
public double getBalance() { return balance; } }
|
三、继承
用 extends 实现继承,子类获得父类的所有属性和方法(构造方法除外)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class Animal { protected String name;
public Animal(String name) { this.name = name; }
public void eat() { System.out.println(name + " 在吃东西"); } }
public class Dog extends Animal { public Dog(String name) { super(name); }
public void bark() { System.out.println(name + " 汪汪叫"); }
@Override public void eat() { System.out.println(name + " 在啃骨头"); } }
|
super 关键字
1 2 3 4 5 6 7
| public class Dog extends Animal { public void test() { super.eat(); this.eat(); eat(); } }
|
Java 只支持单继承
一个类只能有一个直接父类,但可以通过接口实现多继承的效果。
1 2 3 4 5
| public class A extends B { }
|
四、多态
多态的核心是:父类引用指向子类对象,调用方法时执行子类的重写版本。
1 2 3 4 5 6 7 8 9 10 11
| public class Main { public static void main(String[] args) { Animal a1 = new Dog("旺财"); Animal a2 = new Cat("咪咪");
a1.eat(); a2.eat();
} }
|
多态的三个条件:
- 继承或实现
- 重写父类方法
- 父类引用指向子类对象
五、抽象类
用 abstract 声明,不能实例化,可以含抽象方法(没有方法体)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public abstract class Shape { protected String color;
public abstract double area();
public void show() { System.out.println("这是一个" + color + "的图形"); } }
public class Circle extends Shape { private double radius;
public Circle(double r) { this.radius = r; }
@Override public double area() { return Math.PI * radius * radius; } }
|
六、接口
接口是行为的规范,用 interface 声明,用 implements 实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public interface Flyable { void fly(); default void land() { System.out.println("降落"); } static void describe() { System.out.println("可以飞行的东西"); } }
public class Bird implements Flyable { @Override public void fly() { System.out.println("小鸟在飞翔"); } }
|
一个类可以实现多个接口:
1 2 3
| public class Superman implements Flyable, Swimmable, Runnable { }
|
接口 vs 抽象类
|
抽象类 |
接口 |
| 关键字 |
abstract class |
interface |
| 构造方法 |
有 |
没有 |
| 成员变量 |
无限制 |
只能是 public static final 常量 |
| 方法 |
可以有方法体 |
Java 8+ 支持 default/static 方法 |
| 继承 |
只能继承一个 |
可以实现多个 |
七、static 与 final
1 2 3 4 5 6 7 8 9
| public class MyClass { static int count = 0; static void showCount() { }
final int MAX = 100; final void cannotOverride() { } }
|
本系列导航:
- (一)变量与数据类型
- (二)运算符与流程控制
- (三)面向对象编程基础 ← 当前