Java Core

Posted by millzhang on November 6, 2019

Java三大特性

封装

  • 良好的封装可以减少耦合
  • 类内部的结构可以自由修改
  • 可以对成员变量更精准的控制
  • 隐藏信息,实现细节

继承

  • 子类拥有父类非private的属性和方法
  • 子类可以扩展父类的属性和方法,即拥有自己属性和方法
  • 子类可以重载父类的方法
  • 构造器只能够被调用,不能够被继承
  • 对于继承而已,子类会默认调用父类的构造器,但是如果没有默认的父类构造器,子类必须要显示的指定父类的构造器,而且必须是在子类构造器中做的第一件事
  • protected修饰符对类本身而言是私有的,但可给继承它的子类访问;
  • 继承破坏了封装性,慎用。

多态

实现多态的三个必要条件:继承、重写、向上转型。

  • 在多态中,必须存在有继承关系的子类和父类;
  • 子类对父类的某些方法可以重新;
  • 在多态中,需要将子类的引用类型赋给父类对象,这样才能够同时调用子类和父类方法。

多态有两种实现方式:继承和接口。

经典实例:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class A {
    public String show(D obj) {
        return ("A and D");
    }

    public String show(A obj) {
        return ("A and A");
    }

}

public class B extends A{
    public String show(B obj){
        return ("B and B");
    }

    public String show(A obj){
        return ("B and A");
    }
}

public class C extends B{

}

public class D extends B{

}

public class Test {
    public static void main(String[] args) {
        A a1 = new A();
        A a2 = new B();
        B b = new B();
        C c = new C();
        D d = new D();

        System.out.println("1--" + a1.show(b)); // A and A
        System.out.println("2--" + a1.show(c)); // A and A
        System.out.println("3--" + a1.show(d)); //  A and D
        System.out.println("4--" + a2.show(b)); // B and A
        System.out.println("5--" + a2.show(c)); // B and A
        System.out.println("6--" + a2.show(d)); //  A and D
        System.out.println("7--" + b.show(b)); // B and B
        System.out.println("8--" + b.show(c)); // B and B
        System.out.println("9--" + b.show(d));   //  A and D
    }
}

根据 A a2 = new B();,我们对多态有个概括性总结,由父类的引用变量a2引用子类对象时,被引用的对象类型B决定了调用B的成员方法,而不是由引用的变量类型A来决定。但是这个被调用的方法必须在父类中定义过,也就是被子类集成覆盖的方法。 在继承链中,调用的优先级是:this.show(O)>super.show(O)>this.show((super)O)>super.show((super)O)。 针对上述优先级,做个简要的描述(以5为例子):

首先this指向了引用的变量

this.show(O): 先去引用变量a2的类型A中找,有没有该方法show(C obj)的实现,发现没有;

super.show(O): 这是要去A的父类中找了,A的父类是Object,显然没有;

this.show((super)O): 对方法中的对象进行向上转型,C的父类是B,B的父类又是A,所以(super)OB、Athis是指向A的,在A中找到了show(A obj),同时B中也有show(A obj)的重载,决定调哪个是由a2指向的引用对象B决定的,所以调用B中的show(A obj)方法,输出(B and A)

Java的抽象类和接口