Java三大特性之继承

题图 from biblolectors.tumblr.com

 

前两天我们说了Java三大特性之封装,今天主要说说第二大特性--继承。

 

下面我们会通过继承的特点和对应的例子来分别进行说明。

 

子类拥有父类非private的属性,方法。

子类可以拥有自己的属性和方法,即子类可以对父类进行扩展。

子类可以用自己的方式实现父类的方法。

 

例子:

public class TestClass {
    public static void main(String[] str) {
        Son simple = new Son();
        simple.method();
        simple.printStr();
        simple.method("simple");
    }
}

class Father {
    //父类的非private属性,子类也可以访问和使用
    public String str = "Simple";
    Father() {
        System.out.println("This is the Father's " + str);
    }
    //父类的非private方法,子类可以调用
    public void method() {
        System.out.println("This is the Father's method");
    }

    public void method(String strs) {
        System.out.println("This si the Father's mehod with param");
    }
}

class Son extends Father {
    private String ownStr = "ownStr"; //子类可以拥有自己的属性
    Son() {
        super();
        System.out.println("This is the Son's " + super.str);
    }
    //子类可以拥有自己的方法
    public void printStr() {
        System.out.println("This is the Son's method");
    }
    //子类可以重写父类的方法
    public void method(String strs) {
        System.out.println("This is the Son's method ,which is overriding from Frather");
    }
}

​输出结果:

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 Frather

 

Java的类继承是单继承,但是接口可以使用关键字implements实现多重继承(例子会在后面讲接口的时候详细说明)。

子类会默认调用父类的无参数构造器,但是如果父类没有无参构造器,子类必须要显式的指定父类的构造器(有参数),而且必须是在子类构造器第一行进行调用。

 

例子:

public class TestClass {
    public static void main(String[] str) {
        new GouZaoQiTwo();
        new GouZaoQiFour("simple");
    }
}

class GouZaoQiOne {
    GouZaoQiOne() {
        System.out.println("This is GouZaoQiOne");
    }
}

class GouZaoQiTwo extends GouZaoQiOne{
    //子类会默认调用父类的无参数构造器
}

class GouZaoQiThree {
    GouZaoQiThree(String str) {
        System.out.println("This is the gouzaoqi with param.");
    }
}

class GouZaoQiFour extends GouZaoQiThree{
    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类型。

 

例子:

public class TestClass {
    public static void main(String[] str) {
        GouZaoQiTwo gouZaoQiTwo = new GouZaoQiTwo();
        //method方法的参数实际要求的类型是GouZaoQiOne,但是因为GouZaoQiTwo是GouZaoQiOne的子类,所以可以通过向上转型来满足参数类型的需要
        gouZaoQiTwo.method(gouZaoQiTwo);
    }
}

class GouZaoQiOne {
    public void method(GouZaoQiOne gouZaoQiOne) {
        System.out.println("This is GouZaoQiOne");
    }
}

class GouZaoQiTwo extends GouZaoQiOne{

}


继承提高了类之间的耦合性(继承的缺点,耦合度高就会造成代码之间的联系),慎用继承。

 

例子:

public class TestClass {
    public static void main(String[] str) {
        ZiXingChe ziXingChe = new ZiXingChe("sylan15");
        DianDongChe dianDongChe = new DianDongChe("sylan15-1");
        ziXingChe.run();
        ziXingChe.sleep();
        ziXingChe.chongQi();
        dianDongChe.run();
        dianDongChe.sleep();
        dianDongChe.chongDian();
    }
}

class ZiXingChe {
    private String name;
    ZiXingChe(String name) {
        this.name = name;
        System.out.println("This is a " + name);
    }

    public void run() {
        System.out.println("I am a " + this.name + ", I can run");
    }

    public void sleep() {
        System.out.println("I am a " + this.name + ", I need sleep");
    }

    public void chongQi() {
        System.out.println("I am a " + this.name + ", I need chongQi");
    }
}

class DianDongChe {
    private String name;
    DianDongChe(String name) {
        this.name = name;
        System.out.println("This is a " + name);
    }

    public void run() {
        System.out.println("I am a " + this.name + ", I can run");
    }

    public void sleep() {
        System.out.println("I am a " + this.name + ", I need sleep");
    }

    public void chongDian() {
        System.out.println("I am a " + this.name + ", I need chongDian");
    }
}

输出结果为:

This is a sylan15

This is a sylan15-1

I am a sylan15, I can run

I am a sylan15, I need sleep

I am a sylan15, I need chongQi

I am a sylan15-1, I can run

I am a sylan15-1, I need sleep

I am a sylan15-1, I need chongDian

 

看看合并后的代码:

public class TestClass {
    public static void main(String[] str) {
        ZiXingChe ziXingChe = new ZiXingChe("sylan15");
        DianDongChe dianDongChe = new DianDongChe("sylan15-1");
        ziXingChe.run();
        ziXingChe.sleep();
        ziXingChe.chongQi();
        dianDongChe.run();
        dianDongChe.sleep();
        dianDongChe.chongDian();
    }
}

class CheZi {
    private String name;
    CheZi(String name) {
        this.name = name;
        System.out.println("This is a " + name);
    }

    public void run() {
        System.out.println("I am a " + this.name + ", I can run");
    }

    public void sleep() {
        System.out.println("I am a " + this.name + ", I need sleep");
    }
}
class ZiXingChe extends CheZi {
    private String name;
    ZiXingChe(String name) {
        super(name);
        this.name = name;
    }

    public void chongQi() {
        System.out.println("I am a " + this.name + ", I need chongQi");
    }
}

class DianDongChe extends CheZi {
    private String name;
    DianDongChe(String name) {
        super(name);
        this.name = name;
    }

    public void chongDian() {
        System.out.println("I am a " + this.name + ", I need chongDian");
    }
}

输出结果为:

This is a sylan15

This is a sylan15-1

I am a sylan15, I can run

I am a sylan15, I need sleep

I am a sylan15, I need chongQi

I am a sylan15-1, I can run

I am a sylan15-1, I need sleep

I am a sylan15-1, I need chongDian

 

总结下继承的特点:

  • 子类拥有父类非private的属性、方法;
  • 子类可以拥有自己的属性和方法,即子类可以对父类进行扩展;
  • 子类可以用自己的方式实现父类的方法;
  • Java的类继承是单继承,但是接口可以使用关键字implements实现多重继承;
  • 子类会默认调用父类的无参数构造器,但是如果没有默认(无参数)的父类构造器,子类必须要显式的指定父类的构造器(有参数),而且必须是在子类构造器中做的第一件事;
  • ​继承后类型支持向上转型,比如B继承A,那么B的实例也属于A类型。
  • 继承提高了类之间的耦合性(继承的缺点,耦合度高就会造成代码之间的联系),慎用继承;

 

 

 本文作者: sylan215

 本文地址: http://www.sylan215.com/

 版权声明: 本文首发于公众号「sylan215」,可以随意转载,但必须在明确位置注明出处!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值