Java:封装,继承,多态。
一、封装
封装的本质就是让类的调用者不必太多的了解类的实现者是如何实现类的, 只要知道如何使用类就行了。
1. private实现封装
private/ public 这两个关键字表示 “访问权限控制”
被 public 修饰的成员变量或者成员方法,可以直接被类的调用者使用。被 private 修饰的成员变量或者成员方法,不能被类的调用者使用。
2.getter和setter方法
二、继承
继承就是子类继承父类的一种行为,以此达到代码的复用效果。
1.语法规则
class 子类 extends 父类 {
//属性
//方法
}
注意事项:
1.使用 extends 指定父类。
2.对于父类的 private 的字段和方法, 子类中是无法访问的。
3.子类的实例中, 也包含着父类的实例。可以使用 super 关键字得到父类实例的引用。
4.子类继承父类除构造方法以外的所有。
5.在 Java 中, 一个子类只能继承一个父类。
6.子类在构造的时候, 要先帮父类进行构造。
this 和 super 的区别:
this | super |
---|---|
当前对象的引用 | 父类对象的引用 |
this( ): 调用当前类中其他的构造方法(必须放在第一行) | super( ); 调用父类的构造方法(必须放在第一行) |
this.data:调用当前类中的属性 | super.data: 访问父类的属性 |
this.func: 调用当前类中的成员方法 | super.func: 调用父类的成员方法 |
2.继承中的四种访问修饰限定符。
private: 类内部能访问,类外部不能访问。
默认(也叫包访问权限): 类内部能访问,同一个包中的类可以访问,其他类不能访问。
protected: 类内部能访问,子类和同一个包中的类可以访问,其他类不能访问。
public: 类的内部和类的调用者都能访问。
访问修饰限定符的访问权限: private < default < protected < public
3.多层继承一般情况下最多为三层继承。
4.final 关键字
1、final 修饰一个变量:表示的是一个常量,只能被初始化一次,接下来就不能再修改了。
2、final 修饰一个类:密封类,一旦这个类被 final 修饰,那他必然不能再被继承。
3、final 修饰一个方法:密封方法,一旦这个方法被 final 修饰,那他就不能再修改。
三、多态
1、向上转型
Bird bird = new Bird();
Animal bird2 = bird;
// 或者写成下面的方式
Animal bird2 = new Bird();
此时 bird2 是一个父类 (Animal) 的引用,指向一个子类 (Bird) 的实例。这种写法称为 向上转型。
发生向上转型的机遇:
直接赋值
传参
返回值
class Animal {
public String name;
public Animal(String name) {
this.name = name;
System.out.println("Animal(String)");
}
public void eat() {
System.out.println(this.name + "Animal :: eat");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
System.out.println("Cat(String)");
}
}
public class TestMain {
public static Animal func() {
Cat cat = new Cat("mimi");
return cat;
}
public static void main(String[] args) {
// 3. 返回值
Animal animal = func();
animal.eat();
}
public static void func(Animal animal) {
animal.eat();
}
public static void main2(String[] args) {
// 2. 传参
Cat cat = new Cat("mimi");
func(cat);
}
public static void main1(String[] args) {
// 1. 直接赋值
Animal animal = new Cat("mimi");
animal.eat();
}
}
运行结果:
Animal(String)
Cat(String)
mimiAnimal :: eat
2、运行时绑定 / 动态绑定
class Animal {
public String name;
public Animal(String name) {
this.name = name;
System.out.println("Animal(String)");
}
public void eat() {
System.out.println(this.name + "Animal :: eat");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
System.out.println("Cat(String)");
}
@Override // 重写
public void eat() {
System.out.println(this.name + "Cat :: eat()");
}
}
public class TestMain {
public static void main(String[] args) {
Animal animal = new Cat("mimi");
animal.eat();
}
}
运行结果:
Animal(String)
Cat(String)
mimiCat :: eat()
同样是使用 animal 调用 eat( ) 方法,这是却调用的是 Cat 的 eat( ) 方法,这是因为在 Cat 的类中,重写了 eat( ) 方法,使得程序发生了运行时绑定。
编译时还访问的是 Animal 里的 eat( ) 方法,运行时却调用的是 Cat 里的 eat( ) 方法。
3. 重写 / 覆盖 / 覆写(override)
关于重写的注意事项:
- 普通方法可以重写, static 修饰的静态方法不能重写.
- 重写中子类的方法的访问权限不能低于父类的方法访问权限。
- 重写的方法返回值类型不一定和父类的方法相同。
注意事项:
- 需要重写的方法,一定不能是被 final 修饰的。如果是 final 修饰的方法,就是密封方法,不能进行修改。
- 被重写的方法,访问限定修饰符一定不能是private。
- 被重写的方法,子类的访问限定权限一定要大于等于父类的访问限定权限。
- 被 static 修饰的方法是不能进行重写的。
重写 和 重载 的区别
重写 | 重载 |
---|---|
方法名相同 | 方法名相同 |
参数列表相同 | 参数列表不同(参数的个数, 类型) |
返回值相同 | 返回值不做要求 |
在不同的类中, 且具有继承的关系 | 在同一个类中 |
4、向下转型(不安全)
由于向下转型是极其不安全的, 因此, 我们会较少的使用向下转型.
若要使用向下转型, 最好加上 instanceof 关键字
A instanceof B : 判断 A 是不是 B 的一个实例
public static void main(String[] args) {
Animal animal = new Cat("mimi");
if(animal instanceof Bird) {
Bird bird = (Bird)animal;
bird.fly();
} else {
return;
}
}
上一篇: 封装、继承、多态
下一篇: 一不小心走光了,和浩宸聊一聊数据库的锁。