抽象类和接口的简单使用

 

1
2
3
为了实际开发中的可拓展行,本次内容会简单说明抽象类和接口
抽象方法:不能具体实现的方法同时使用abstract修饰,就是没有方法体
抽象类:不能具体实例化的类,使用abstract修饰

  

 

定义抽象类,定义接口,如果后期需要新增别的方法,也方便后期拓展

1
2
抽象类中的抽象方法不是必须的,可有可有
但是存在抽象方法的就必须是抽象类

  

 

 

下面分析一个问题,为什么抽象类无法新建对象?

那是因为抽象类中可能存在抽象方法,抽象方法是没有方法体的,也就是调用抽象方法是没有意义的

抽象类的意义不是创建对象而在于被继承

 

 

1
接口:比抽象类还抽象的存在

  单继承却是可以多实现

 

1
2
3
4
5
6
抽象类和接口的区别
抽象类使用abstract class修饰
接口使用interface修饰
继承抽象类使用extends,实现接口使用implements
抽象类中可以有构造方法,但是接口不能有构造方法(因为接口中的都是抽象方法)
抽象类中有成员变量,接口中只能有常量

  在jdk1.8中,接口新特性,接口可以增加非抽象方法

 

 

 在jdk1.8中,接口新特性,接口也可以增加静态方法

 

 

 

 一般静态方法多适用于工具类中

 

 抽象类中可以写普通方法,方便普通类去继承

 

 对于java的模板设计模式,是将不同对象相同的共性抽到父类中

 

 

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 *
 */
package com.java.abstracttest;
 
/**
 * @author yourheart
 * @update 2022年3月29日 下午9:26:04
 * @description:
 */
public abstract class HouseTemplate {
 
    protected HouseTemplate(String name) {
        this.name = name;
    }
 
    protected String name;
 
    protected abstract void buildDoor();
 
    protected abstract void buildWindow();
 
    protected abstract void buildWall();
 
    protected abstract void buildBase();
 
    // 公共逻辑
    public final void buildHouse() {
 
        buildBase();
        buildWall();
        buildDoor();
        buildWindow();
 
    }
 
}
/**
 *
 */
package com.java.abstracttest;
 
/**
 * @author yourheart
 * @update 2022年3月29日 下午9:26:46
 * @description:
 */
public class HouseOne extends HouseTemplate {
    HouseOne(String name) {
        super(name);
    }
 
    @Override
    protected void buildDoor() {
        System.out.println(name + "的门要采用防盗门");
    }
 
    @Override
    protected void buildWindow() {
        System.out.println(name + "的窗户要面向北方");
    }
 
    @Override
    protected void buildWall() {
        System.out.println(name + "的墙使用大理石建造");
    }
 
    @Override
    protected void buildBase() {
        System.out.println(name + "的地基使用钢铁地基");
    }
 
}
 
/**
 *
 */
package com.java.abstracttest;
 
/**
 * @author yourheart
 * @update 2022年3月29日 下午9:28:12
 * @description:
 */
public class HouseTwo extends HouseTemplate {
 
    HouseTwo(String name) {
        super(name);
    }
 
    @Override
    protected void buildDoor() {
        System.out.println(name + "的门采用木门");
    }
 
    @Override
    protected void buildWindow() {
        System.out.println(name + "的窗户要向南");
    }
 
    @Override
    protected void buildWall() {
        System.out.println(name + "的墙使用玻璃制造");
    }
 
    @Override
    protected void buildBase() {
        System.out.println(name + "的地基使用花岗岩");
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
public class Clienter {
 
    public static void main(String[] args) {
        HouseTemplate houseOne = new HouseOne("房子1");
        HouseTemplate houseTwo = new HouseTwo("房子2");
        houseOne.buildHouse();
        houseTwo.buildHouse();
    }
 
}

  

 

posted @   不忘初心2021  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示