Java三大特性之继承

时间:2021-08-23 21:56:28
题图 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");
}
}
Java三大特性之继承 Java三大特性之继承 ​输出结果: 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");
}
}
Java三大特性之继承 输出结果: This is GouZaoQiOne This is the gouzaoqi with param. This is GouZaoQiFour
继承后类型支持向上转型,比如B继承A,那么B的实例也属于A类型。
例子: Java三大特性之继承
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");
}
}
Java三大特性之继承
Java三大特性之继承 输出结果为: 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");
}
}
Java三大特性之继承 Java三大特性之继承 输出结果为: 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类型。
  • 继承提高了类之间的耦合性(继承的缺点,耦合度高就会造成代码之间的联系),慎用继承;


长按关注「我在编程」 Java三大特性之继承Java三大特性之继承
Java三大特性之继承 多看 | 多想 | 多练 实践是检验真理的唯一标准