Java笔记(05):面向对象--继承

1、静态代码块和构造代码块

/*
    代码块:在Java中,使用{}括起来的代码被称为代码块。
    根据其位置和声明的不同,可以分为
        局部代码块:局部位置,用于限定变量的生命周期。
        构造代码块:在类中的成员位置,用{}括起来的代码。每次调用构造方法执行前,都会先执行构造代码块。
            作用:可以把多个构造方法中的共同代码放到一起,对对象进行初始化。
        静态代码块:在类中的成员位置,用{}括起来的代码,只不过它用static修饰了。
            作用:一般是对类进行初始化。
            
    面试题?
        静态代码块,构造代码块,构造方法的执行顺序?
        静态代码块 -- 构造代码块 -- 构造方法
        静态代码块:只执行一次
        构造代码块:每次调用构造方法都执行
*/
class Code {
    static {
        int a = 1000;
        System.out.println(a);
    }

    //构造代码块
    {
        int x = 100;
        System.out.println(x);
    }
    
    //构造方法
    public Code(){
        System.out.println("code");
    }
    
    //构造方法
    public Code(int a){
        System.out.println("code");
    }
    
    //构造代码块
    {
        int y = 200;
        System.out.println(y);
    }
    
    //静态代码块
    static {
        int b = 2000;
        System.out.println(b);
    }
}

class CodeDemo {
    public static void main(String[] args) {
        //局部代码块
        {
            int x = 10;
            System.out.println(x);
        }
        //找不到符号
        //System.out.println(x);
        {
            int y = 20;
            System.out.println(y);
        }
        System.out.println("---------------");
        
        Code c = new Code();    
        System.out.println("---------------");
        Code c2 = new Code();
        System.out.println("---------------");
        Code c3 = new Code(1);
    }
}
 1 /*
 2     写程序的执行结果。
 3     
 4     林青霞都60了,我很伤心
 5     我是main方法
 6     Student 静态代码块
 7     Student 构造代码块
 8     Student 构造方法
 9     Student 构造代码块
10     Student 构造方法
11 */
12 class Student {
13     static {
14         System.out.println("Student 静态代码块");
15     }
16     
17     {
18         System.out.println("Student 构造代码块");
19     }
20     
21     public Student() {
22         System.out.println("Student 构造方法");
23     }
24 }
25 
26 class StudentDemo {
27     static {
28         System.out.println("林青霞都60了,我很伤心");
29     }
30     
31     public static void main(String[] args) {
32         System.out.println("我是main方法");
33         
34         Student s1 = new Student();
35         Student s2 = new Student();
36     }
37 }

2、继承概述

 1 /*
 2     继承概述:
 3         把多个类中相同的内容给提取出来定义到一个类中。
 4         
 5     如何实现继承呢?    
 6         Java提供了关键字:extends
 7         
 8     格式:
 9         class 子类名 extends 父类名 {}
10         
11     好处:
12         A:提高了代码的复用性
13         B:提高了代码的维护性
14         C:让类与类之间产生了关系,是多态的前提
15     
16     类与类产生了关系,其实也是继承的一个弊端:
17         类的耦合性增强了。
18         
19         开发的原则:低耦合,高内聚。
20         耦合:类与类的关系
21         内聚:就是自己完成某件事情的能力
22 */
23 
24 //使用继承前
25 /*
26 class Student {
27     public void eat() {
28         System.out.println("吃饭");
29     }
30     
31     public void sleep() {
32         System.out.println("睡觉");
33     }
34 }
35 
36 class Teacher {
37     public void eat() {
38         System.out.println("吃饭");
39     }
40     
41     public void sleep() {
42         System.out.println("睡觉");
43     }
44 }
45 */
46 
47 //使用继承后
48 class Person {
49     public void eat() {
50         System.out.println("吃饭");
51     }
52     
53     public void sleep() {
54         System.out.println("睡觉");
55     }
56 }
57 
58 class Student extends Person {}
59 
60 class Teacher extends Person {}
61 
62 class ExtendsDemo {
63     public static void main(String[] args) {
64         Student s = new Student();
65         s.eat();
66         s.sleep();
67         System.out.println("-------------");
68         
69         Teacher t = new Teacher();
70         t.eat();
71         t.sleep();
72     }
73 }

3、Java中继承的特点

 1 /*
 2     Java中继承的特点:
 3         A:Java只支持单继承,不支持多继承。
 4             有些语言是支持多继承,格式:extends 类1,类2,...
 5         B:Java支持多层继承(继承体系)
 6 */
 7 
 8 /*
 9 class Father {}
10 class Mother {}
11 class Son exnteds Father {} //正确的
12 class Son extends Father,Mother {} // 错误的
13 */
14 
15 class GrandFather {
16     public void show() {
17         System.out.println("我是爷爷");
18     }
19 }
20 
21 class Father extends GrandFather {
22     public void method(){
23         System.out.println("我是老子");
24     }
25 }
26 
27 class Son extends Father {}
28 
29 class ExtendsDemo2 {
30     public static void main(String[] args) {
31         Son s = new Son();
32         s.method(); //使用父亲的
33         s.show(); //使用爷爷的
34     }
35 }

4、继承的注意事项

 1 /*
 2     继承的注意事项:
 3         A:子类只能继承父类所有非私有的成员(成员方法和成员变量)
 4         B:子类不能继承父类的构造方法,但是可以通过super(马上讲)关键字去访问父类构造方法。
 5         C:不要为了部分功能而去继承
 6             class A {
 7                 public void show1(){}
 8                 public void show2(){}
 9             }
10             
11             class B {
12                 public void show2(){}
13                 public void show3(){}
14             }
15             
16             //我们发现B类中出现了和A类一样的show2()方法,所以,我们就用继承来体现
17             class B extends A {
18                 public void show3(){}
19             }
20             这样其实不好,因为这样你不但有了show2(),还多了show1()。
21             有可能show1()不是你想要的。
22             
23     那么,我们什么时候考虑使用继承呢?
24         继承其实体现的是一种关系:"is a"。
25             Person
26                 Student
27                 Teacher
28             水果
29                 苹果
30                 香蕉
31                 橘子
32                 
33         采用假设法。
34             如果有两个类A,B。只有他们符合A是B的一种,或者B是A的一种,就可以考虑使用继承。
35 */
36 class Father {
37     private int num = 10;
38     public int num2 = 20;
39     
40     //私有方法,子类不能继承
41     private void method() {
42         System.out.println(num);
43         System.out.println(num2);
44     }
45     
46     public void show() {
47         System.out.println(num);
48         System.out.println(num2);
49     }
50 }
51 
52 class Son extends Father {
53     public void function() {
54         //num可以在Father中访问private
55         //System.out.println(num); //子类不能继承父类的私有成员变量
56         System.out.println(num2);
57     }
58 }
59 
60 class ExtendsDemo3 {
61     public static void main(String[] args) {
62         // 创建对象
63         Son s = new Son();
64         //s.method(); //子类不能继承父类的私有成员方法
65         s.show();
66         s.function();
67     }
68 }

5、继承中成员变量

 1 /*
 2     类的组成:
 3         成员变量:
 4         构造方法:
 5         成员方法:
 6     而现在我们又讲解了继承,所以,我们就应该来考虑一下,类的组成部分的各自关系。
 7     
 8     继承中成员变量的关系:
 9         A:子类中的成员变量和父类中的成员变量名称不一样,这个太简单。
10         B:子类中的成员变量和父类中的成员变量名称一样,这个怎么玩呢?
11             在子类方法中访问一个变量的查找顺序:
12                 a:在子类方法的局部范围找,有就使用
13                 b:在子类的成员范围找,有就使用
14                 c:在父类的成员范围找,有就使用
15                 d:如果还找不到,就报错。
16 */
17 class Father {
18     public int num = 10;
19     
20     public void method() {
21         int num = 50;
22     }
23 }
24 
25 class Son extends Father {
26     public int num2 = 20;
27     public int num = 30;
28     
29     public void show() {
30         int num = 40;
31         System.out.println(num);
32         System.out.println(num2);
33         // 找不到符号
34         System.out.println(num3);
35     }
36 }
37 
38 class ExtendsDemo4 {
39     public static void main(String[] args) {
40         //创建对象
41         Son s = new Son();
42         s.show();
43     }
44 }

6、this和super

 1 /*
 2     问题是:
 3         我不仅仅要输出局部范围的num,还要输出本类成员范围的num。怎么办呢?
 4         我还想要输出父类成员范围的num。怎么办呢?
 5             如果有一个东西和this相似,但是可以直接访问父类的数据就好了。
 6             恭喜你,这个关键字是存在的:super。
 7             
 8     this和super的区别?
 9         分别是什么呢?
10             this代表本类对应的引用。
11             super代表父类存储空间的标识(可以理解为父类引用,可以操作父类的成员)
12 
13         怎么用呢?
14             A:调用成员变量
15                 this.成员变量 调用本类的成员变量
16                 super.成员变量 调用父类的成员变量
17             B:调用构造方法
18                 this(...)    调用本类的构造方法
19                 super(...)    调用父类的构造方法
20             C:调用成员方法
21                 this.成员方法 调用本类的成员方法
22                 super.成员方法 调用父类的成员方法
23 */
24 class Father {
25     public int num = 10;
26 }
27 
28 class Son extends Father {
29     public int num = 20;
30     
31     public void show() {
32         int num = 30;
33         System.out.println(num);
34         System.out.println(this.num);
35         System.out.println(super.num);
36     }
37 }
38 
39 class ExtendsDemo5 {
40     public static void main(String[] args) {
41         Son s = new Son();
42         s.show();
43     }
44 }

7、继承中构造方法的关系

 1 /*
 2     继承中构造方法的关系
 3         A:子类中所有的构造方法默认都会访问父类中空参数的构造方法
 4         B:为什么呢?
 5             因为子类会继承父类中的数据,可能还会使用父类的数据。
 6             所以,子类初始化之前,一定要先完成父类数据的初始化。
 7             
 8             注意:子类每一个构造方法的第一条语句默认都是:super();
 9 */
10 class Father {
11     int age;
12 
13     public Father() {
14         System.out.println("Father的无参构造方法");
15     }
16     
17     public Father(String name) {
18         System.out.println("Father的带参构造方法");
19     }
20 }
21 
22 class Son extends Father {
23     public Son() {
24         //super();
25         System.out.println("Son的无参构造方法");
26     }
27     
28     public Son(String name) {
29         //super();
30         System.out.println("Son的带参构造方法");
31     }
32 }    
33 
34 class ExtendsDemo6 {
35     public static void main(String[] args) {
36         //创建对象
37         Son s = new Son();//Father的无参构造方法//Son的无参构造方法
38         System.out.println("------------");
39         Son s2 = new Son("林青霞");//Father的带参构造方法//Son的带参构造方法
40     }
41 }

8、子类调用父类构造

 1 /*
 2     如果父类没有无参构造方法,那么子类的构造方法会出现什么现象呢?
 3         报错。
 4     如何解决呢?    
 5         A:在父类中加一个无参构造方法
 6         B:通过使用super关键字去显示的调用父类的带参构造方法
 7         C:子类通过this去调用本类的其他构造方法
 8             子类中一定要有一个去访问了父类的构造方法,否则父类数据就没有初始化。
 9             
10     注意事项:
11         this(...)或者super(...)必须出现在第一条语句上。
12         如果不是放在第一条语句上,就可能对父类的数据进行了多次初始化,所以必须放在第一条语句上。
13 */
14 class Father {
15     /*
16     public Father() {
17         System.out.println("Father的无参构造方法");
18     }
19     */
20     
21     public Father(String name) {
22         System.out.println("Father的带参构造方法");
23     }
24 }
25 
26 class Son extends Father {
27     public Son() {
28         super("随便给");
29         System.out.println("Son的无参构造方法");
30         //super("随便给");//报错
31     }
32     
33     public Son(String name) {
34         //super("随便给");
35         this();
36         System.out.println("Son的带参构造方法");
37     }
38 }
39 
40 class ExtendsDemo7 {
41     public static void main(String[] args) {
42         Son s = new Son();//Father的带参构造方法//Son的无参构造方法
43         System.out.println("----------------");
44         Son ss = new Son("林青霞");//Father的带参构造方法//Son的无参构造方法//Son的带参构造方法
45     }
46 }

练习1、

/*
    看程序写结果:
        A:成员变量    就近原则
        B:this和super的问题
            this访问本类的成员
            super访问父类的成员
        C:子类构造方法执行前默认先执行父类的无参构造方法
        D:一个类的初始化过程
            成员变量进行初始化
                默认初始化
                显示初始化
                构造方法初始化
                
    结果:
        fu
        zi
        30
        20
        10
*/
class Fu{
    public int num = 10;
    public Fu(){
        System.out.println("fu");
    }
}
class Zi extends Fu{
    public int num = 20;
    public Zi(){
        System.out.println("zi");
    }
    public void show(){
        int num = 30;
        System.out.println(num); //30
        System.out.println(this.num); //20
        System.out.println(super.num); //10
    }
}
class ExtendsTest {
    public static void main(String[] args) {
        Zi z = new Zi();
        z.show();
    }
}

练习2、

 1 /*
 2     看程序写结果:
 3         A:一个类的静态代码块,构造代码块,构造方法的执行流程
 4             静态代码块 > 构造代码块 > 构造方法
 5         B:静态的内容是随着类的加载而加载
 6             静态代码块的内容会优先执行
 7         C:子类初始化之前先会进行父类的初始化
 8         
 9     结果是:
10         静态代码块Fu
11         静态代码块Zi
12         构造代码块Fu
13         构造方法Fu
14         构造代码块Zi
15         构造方法Zi
16 */
17 class Fu {
18     static {
19         System.out.println("静态代码块Fu");
20     }
21 
22     {
23         System.out.println("构造代码块Fu");
24     }
25 
26     public Fu() {
27         System.out.println("构造方法Fu");
28     }
29 }
30 
31 class Zi extends Fu {
32     static {
33         System.out.println("静态代码块Zi");
34     }
35 
36     {
37         System.out.println("构造代码块Zi");
38     }
39 
40     public Zi() {
41         System.out.println("构造方法Zi");
42     }
43 }
44 
45 class ExtendsTest2 {
46     public static void main(String[] args) {
47         Zi z = new Zi();
48     }
49 }

练习3、

 1 /*
 2     看程序写结果:
 3         A:成员变量的问题
 4             int x = 10; //成员变量是基本类型
 5             Student s = new Student(); //成员变量是引用类型
 6         B:一个类的初始化过程
 7             成员变量的初始化
 8                 默认初始化
 9                 显示初始化
10                 构造方法初始化
11         C:子父类的初始化(分层初始化)
12             先进行父类初始化,然后进行子类初始化。
13             
14     结果:
15         YXYZ
16         
17     问题:
18         虽然子类中构造方法默认有一个super()
19         初始化的时候,不是按照那个顺序进行的。
20         而是按照分层初始化进行的。
21         它仅仅表示要先初始化父类数据,再初始化子类数据。
22 */
23 class X {
24     Y b = new Y();
25     X() {
26         System.out.print("X");
27     }
28 }
29 
30 class Y {
31     Y() {
32         System.out.print("Y");
33     }
34 }
35 
36 public class Z extends X {
37     Y y = new Y();
38     Z() {
39         //super
40         System.out.print("Z");
41     }
42     public static void main(String[] args) {
43         new Z(); 
44     }
45 }

9、继承中成员方法的关系

 1 /*
 2     继承中成员方法的关系:
 3         A:子类中的方法和父类中的方法声明不一样,这个太简单。
 4         B:子类中的方法和父类中的方法声明一样,这个该怎么玩呢?
 5             通过子类对象调用方法:
 6                 a:先找子类中,看有没有这个方法,有就使用
 7                 b:再看父类中,有没有这个方法,有就使用
 8                 c:如果没有就报错。
 9 */
10 class Father {
11     public void show() {
12         System.out.println("show Father");
13     }
14 }
15 
16 class Son extends Father {
17     public void method() {
18         System.out.println("method Son");
19     }
20     
21     public void show() {
22         System.out.println("show Son");
23     }
24 }
25 
26 class ExtendsDemo8 {
27     public static void main(String[] args) {
28         //创建对象
29         Son s = new Son();
30         s.show();//show Son
31         s.method();//method Son
32         //s.fucntion(); //找不到符号
33     }
34 }

10、重写父类方法

 1 /*
 2     方法重写:子类中出现了和父类中方法声明一模一样的方法。
 3     
 4     方法重载:
 5         本类中出现的方法名一样,参数列表不同的方法。与返回值无关。
 6 
 7     子类对象调用方法的时候:
 8         先找子类本身,再找父类。
 9         
10     方法重写的应用:
11         当子类需要父类的功能,而功能主体子类有自己特有内容时,可以重写父类中的方法。
12         这样,即沿袭了父类的功能,又定义了子类特有的内容。
13         
14     案例:
15         A:定义一个手机类。
16         B:通过研究,我发明了一个新手机,这个手机的作用是在打完电话后,可以听天气预报。
17         按照我们基本的设计,我们把代码给写出来了。
18         但是呢?我们又发现新手机应该是手机,所以,它应该继承自手机。
19         其实这个时候的设计,并不是最好的。
20         因为手机打电话功能,是手机本身就具备的最基本的功能。
21         所以,我的新手机是不用在提供这个功能的。
22         但是,这个时候,打电话功能就没有了。这个不好。
23         最终,还是加上这个功能。由于它继承了手机类,所以,我们就直接使用父类的功能即可。
24         那么,如何使用父类的功能呢?通过super关键字调用
25 */
26 class Phone {
27     public void call(String name) {
28         System.out.println("给"+name+"打电话");
29     }
30 }
31 
32 class NewPhone extends Phone {
33     public void call(String name) {
34         //System.out.println("给"+name+"打电话");
35         super.call(name);
36         System.out.println("可以听天气预报了");
37     }
38 }
39 
40 class ExtendsDemo9 {
41     public static void main(String[] args) {
42         NewPhone np = new NewPhone();
43         np.call("林青霞");//给林青霞打电话//可以听天气预报了
44     }
45 }

11、方法重写的注意事项

 1 /*
 2     方法重写的注意事项
 3         A:父类中私有方法不能被重写
 4             因为父类私有方法子类根本就无法继承
 5         B:子类重写父类方法时,访问权限不能更低
 6             最好就一致
 7         C:父类静态方法,子类也必须通过静态方法进行重写
 8             其实这个算不上方法重写,但是现象确实如此,至于为什么算不上方法重写,多态中我会讲解
 9             
10         子类重写父类方法的时候,最好声明一模一样。
11 */
12 class Father {
13     //private void show() {}
14     
15     /*
16     public void show() {
17         System.out.println("show Father");
18     }
19     */
20     
21     void show() {
22         System.out.println("show Father");
23     }
24     /*
25     public static void method() {
26         
27     }
28     */
29     
30     public void method() {
31         
32     }
33 }
34 
35 class Son extends Father {
36     //private void show() {}
37 
38     /*
39     public void show() {
40         System.out.println("show Son");
41     }
42     */
43     
44     public void show() {
45         System.out.println("show Son");
46     }
47     
48     
49     public static void method() {
50     
51     }
52     
53     /*
54     public void method() {
55     
56     }
57     */
58 }
59 
60 class ExtendsDemo10 {
61     public static void main(String[] args) {
62         Son s = new Son();
63         s.show();
64     }
65 }

练习4、

 1 /*
 2     学生案例和老师案例讲解
 3     
 4     学生:
 5         成员变量;姓名,年龄
 6         构造方法:无参,带参
 7         成员方法:getXxx()/setXxx()
 8     老师:
 9         成员变量;姓名,年龄
10         构造方法:无参,带参
11         成员方法:getXxx()/setXxx()
12         
13     看上面两个类的成员,发现了很多相同的东西,所以我们就考虑抽取一个共性的类:
14     人:
15         成员变量;姓名,年龄
16         构造方法:无参,带参
17         成员方法:getXxx()/setXxx()
18         
19         学生 继承 人
20         老师 继承 人
21 */
22 //定义人类
23 class Person {
24     //姓名
25     private String name;
26     //年龄
27     private int age;
28     
29     public Person() {
30     }
31 
32     public Person(String name,int age) { //"林青霞",27
33         this.name = name;
34         this.age = age;
35     }
36     
37     public String getName() {
38         return name;
39     }
40     
41     public void setName(String name) {
42         this.name = name;
43     }
44     
45     public int getAge() {
46         return age;
47     }
48     
49     public void setAge(int age) {
50         this.age = age;
51     }
52 }
53 
54 //定义学生类
55 class Student extends Person {
56     public Student() {}
57     
58     public Student(String name,int age) { //"林青霞",27
59         //this.name = name;
60         //this.age = age;
61         super(name,age);
62     }
63 }
64 
65 //定义老师类
66 class Teacher extends Person {
67 
68 }
69 
70 class ExtendsTest4 {
71     public static void main(String[] args) {
72         //创建学生对象并测试
73         //方式1
74         Student s1 = new Student();
75         s1.setName("林青霞");
76         s1.setAge(27);
77         System.out.println(s1.getName()+"---"+s1.getAge());
78         
79         //方式2
80         Student s2 = new Student("林青霞",27);
81         System.out.println(s2.getName()+"---"+s2.getAge());
82     }
83 }

练习5、

  1 /*
  2     猫狗案例讲解
  3     
  4     先找到具体的事物,然后发现具体的事物有共性,才提取出一个父类。
  5     
  6     猫:
  7         成员变量:姓名,年龄,颜色
  8         构造方法:无参,带参
  9         成员方法:
 10             getXxx()/setXxx()
 11             eat()
 12             palyGame()
 13     狗:
 14         成员变量:姓名,年龄,颜色
 15         构造方法:无参,带参
 16         成员方法:
 17             getXxx()/setXxx()
 18             eat()
 19             lookDoor()
 20             
 21     共性:
 22         成员变量:姓名,年龄,颜色
 23         构造方法:无参,带参
 24         成员方法:
 25             getXxx()/setXxx()
 26             eat()
 27             
 28     把共性定义到一个类中,这个类的名字叫:动物。
 29     动物类:
 30         成员变量:姓名,年龄,颜色
 31         构造方法:无参,带参
 32         成员方法:
 33             getXxx()/setXxx()
 34             eat()
 35             
 36         猫:    
 37             构造方法:无参,带参
 38             成员方法:palyGame()
 39         狗:
 40             构造方法:无参,带参
 41             成员方法:lookDoor()
 42 */
 43 //定义动物类
 44 class Animal {
 45     //姓名
 46     private String name;
 47     //年龄
 48     private int age;
 49     //颜色
 50     private String color;
 51     
 52     public Animal() {}
 53     
 54     public Animal(String name,int age,String color) {
 55         this.name = name;
 56         this.age = age;
 57         this.color = color;
 58     }
 59     
 60     public String getName() {
 61         return name;
 62     }
 63     
 64     public void setName(String name) {
 65         this.name = name;
 66     }
 67     
 68     public int getAge() {
 69         return age;
 70     }
 71     
 72     public void setAge(int age) {
 73         this.age = age;
 74     }
 75     
 76     public String getColor() {
 77         return color;
 78     }
 79     
 80     public void setColor(String color) {
 81         this.color = color;
 82     }
 83     
 84     public void eat() {
 85         System.out.println("不要睡了,该吃饭了");
 86     }
 87 }
 88 
 89 //定义猫类
 90 class Cat extends Animal {
 91     public Cat() {}
 92     
 93     public Cat(String name,int age,String color) {
 94         super(name,age,color);
 95     }
 96     
 97     public void playGame() {
 98         System.out.println("猫玩英雄联盟");
 99     }
100 }
101 
102 //定义狗类
103 class Dog extends Animal {
104     public Dog() {}
105     
106     public Dog(String name,int age,String color) {
107         super(name,age,color);
108     }
109     
110     public void lookDoor() {
111         System.out.println("狗看家");
112     }
113 }
114 
115 //测试类
116 class ExtendsTest5 {
117     public static void main(String[] args) {
118         //测试猫
119         //方式1
120         Cat c1 = new Cat();
121         c1.setName("Tom");
122         c1.setAge(3);
123         c1.setColor("白色");
124         System.out.println("猫的名字是:"+c1.getName()+";年龄是:"+c1.getAge()+";颜色是:"+c1.getColor());
125         c1.eat();
126         c1.playGame();
127         System.out.println("---------------");
128         
129         //方式2
130         Cat c2 = new Cat("杰瑞",5,"土豪金");
131         System.out.println("猫的名字是:"+c2.getName()+";年龄是:"+c2.getAge()+";颜色是:"+c2.getColor());
132         c2.eat();
133         c2.playGame();
134     }
135 }