System.out.println("This is the Father's " + str);
}
//父类的非private方法,子类可以调用
publicvoidmethod(){
System.out.println("This is the Father's method");
}
publicvoidmethod(String strs){
System.out.println("This is the Father's method with param");
}
}
classSonextendsFather{
//子类可以拥有自己的属性
private String ownStr = "ownStr";
Son() {
super();
System.out.println("This is the Son's " +
super.str);
}
//子类可以拥有自己的方法
publicvoidprintStr(){
System.out.println("This is the " +
"Son's method");
}
//子类可以重写父类的方法
publicvoidmethod(String strs){
System.out.println("This is the Son's method, " +
"which is overriding from Father");
}
}
输出结果:
This is the Father’s Simple This is the Son’s Simple This is the Father’s method This is the Son’s method This is the Son’s method, which is overriding from Father
System.out.println("This is the gouzaoqi with param.");
}
}
classGouZaoQiFourextendsGouZaoQiThree{
GouZaoQiFour(String str){
//如果父类没有无参构造器,子类必须要显式的指定父类的构造器(有参数),
//而且必须是在子类构造器第一行进行调用
super(str);
System.out.println("This is GouZaoQiFour");
}
}
输出结果:
This is GouZaoQiOne This is the gouzaoqi with param. This is GouZaoQiFour
继承后类型支持向上转型,比如 B 继承 A,那么B的实例也属于 A 类型。
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
publicclass{
publicstaticvoidmain(String[] str){
GouZaoQiTwo gouZaoQiTwo = new GouZaoQiTwo();
//method方法的参数实际要求的类型是GouZaoQiOne,
//但是因为GouZaoQiTwo是GouZaoQiOne的子类,
//所以可以通过向上转型来满足参数类型的需要
gouZaoQiTwo.method(gouZaoQiTwo);
}
}
classGouZaoQiOne{
publicvoidmethod(GouZaoQiOne gouZaoQiOne){
System.out.println("This is GouZaoQiOne");
}
}
classGouZaoQiTwoextendsGouZaoQiOne{
}
继承提高了类之间的耦合性(继承的缺点,耦合度高就会造成代码之间的联系),慎用继承。
例子:
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
publicclass{
publicstaticvoidmain(String[] str){
ZiXingChe ziXingChe = new ZiXingChe("sylan215");
DianDongChe dianDongChe = new DianDongChe("sylan215-1");
ziXingChe.run();
ziXingChe.sleep();
ziXingChe.reboot();
dianDongChe.run();
dianDongChe.sleep();
dianDongChe.power();
}
}
classZiXingChe{
private String name;
ZiXingChe(String name) {
this.name = name;
System.out.println("This is a " + name);
}
publicvoidrun(){
System.out.println("I am a " + this.name + ", I can run");
}
publicvoidsleep(){
System.out.println("I am a " + this.name + ", I need sleep");
}
publicvoidreboot(){
System.out.println("I am a " + this.name + ", I need reboot");
}
}
classDianDongChe{
private String name;
DianDongChe(String name) {
this.name = name;
System.out.println("This is a " + name);
}
publicvoidrun(){
System.out.println("I am a " + this.name + ", I can run");
}
publicvoidsleep(){
System.out.println("I am a " + this.name + ", I need sleep");
}
publicvoidpower(){
System.out.println("I am a " + this.name + ", I need power");
}
}
输出结果为:
This is a sylan215 This is a sylan215-1 I am a sylan215, I can run I am a sylan215, I need sleep I am a sylan215, I need reboot I am a sylan215-1, I can run I am a sylan215-1, I need sleep I am a sylan215-1, I need power
看看合并后的代码:
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
publicclass{
publicstaticvoidmain(String[] str){
ZiXingChe ziXingChe = new ZiXingChe("sylan215");
DianDongChe dianDongChe = new DianDongChe("sylan215-1");
ziXingChe.run();
ziXingChe.sleep();
ziXingChe.reboot();
dianDongChe.run();
dianDongChe.sleep();
dianDongChe.power();
}
}
classCheZi{
private String name;
CheZi(String name) {
this.name = name;
System.out.println("This is a " + name);
}
publicvoidrun(){
System.out.println("I am a " + this.name + ", I can run");
}
publicvoidsleep(){
System.out.println("I am a " + this.name + ", I need sleep");
}
}
classZiXingCheextendsCheZi{
private String name;
ZiXingChe(String name) {
super(name);
this.name = name;
}
publicvoidreboot(){
System.out.println("I am a " + this.name + ", I need reboot");
}
}
classDianDongCheextendsCheZi{
private String name;
DianDongChe(String name) {
super(name);
this.name = name;
}
publicvoidpower(){
System.out.println("I am a " + this.name + ", I need power");
}
}
输出结果为:
This is a sylan215 This is a sylan215-1 I am a sylan215, I can run I am a sylan215, I need sleep I am a sylan215, I need reboot I am a sylan215-1, I can run I am a sylan215-1, I need sleep I am a sylan215-1, I need power
近期评论