java继承

2240 字
6 分钟

每日一言

[to Ichigo] You’re a Soul Reaper and I’m a Hollow. Whichever side loses will be massacred! It’s been that way for over a thousand years! Who needs any more reason than that?! Now bring it! The last man standing gets to go home alive! That’s all there is to it! — Grimmjow Jeagerjaques from Bleach

继承的概念

在java中,继承是面向对象编程的重要特征之一,它允许一个类(子类、派生类)继承另一个类(父类、基类)的属性和方法,从而实现代码的复用和扩展。

我们都知道类是对于生活中的事物的一种抽象,我们将一个事物抽象为一个类,并用类的成员和方法来刻画这个事物。现在我们将人抽象为一个简单的类:

class human{
    public double stature;  // 身高
    public double weight;   // 体重
    public String name;    // 姓名
    public int age;        // 年龄
    public void speak(String s){    // 说话
        System.out.println(s);
    }
}

我们简单定义了这个人的身高、体重、姓名、年龄,并且定义了一个行为是说话。

我们可以通过 human 类创建许多不同的人类,他们有不同的成员变量,但是有相同的行为。

我们可以根据人类这一类派生出各种职业的人。比如学生,我们可以如下定义:

class student extends human {
        int grade;
        public void study() {
            System.out.println("I am studying");
        }
    }

学生也是人类,所以具有人类所有的一切成员变量和方法,但是学生还有额外具有成员变量成绩和行为学习。

此写法与下面的效果完全一致,但是却更加一目了然:

class student {
        int grade;
        String name;
        int age;
        double height;
        double weight;
        public void speak(String text) {
            System.out.println(text);
        }
        public void study() {
            System.out.println("I am studying");
        }
    }

继承类型

面向对象编程中继承分为四种情况:

单继承:

class A{
	...
}
class B extends{
	...
}

多重继承:

class A{
	...
}
class B extends A{
	...
}
class C extends B{
	...
}

不同类继承同一个类:

class A{
	...
class B extends A{
	...
} 
class C extends A{
	...
}

多继承:(java不支持

class A{
	...
}
class B{
	...
}
class C extends A,B{
	...
}

继承的特性

  • 子类拥有父类非 private的属性和方法
  • 子类可以对父类进行扩展
  • 子类可以重写父类的方法
  • 子类并不会继承父类的构造方法,但是子类的构造方法中必须通过 super()函数来调用父类的构造方法。
    • 当父类的构造函数为无参类型时可以省略,但是若为有参类型,子类的构造方法必须在第一行用 super(参数)函数来先行调用父类的构造函数。

例如:

无参类型继承

父类:

class human {
        String name;
        int age;
        double height;
        double weight;
        public void speak(String text) {
            System.out.println(text);
        }
        public human( ) {
            System.out.println("Human is created");
        }
}

则子类可以:

class student extends human {
        int grade;
        public void study() {
            System.out.println("I am studying");
        }
        public student(String name, int age, double height, double weight, int grade) {
            System.out.println("Student is created");
        }
    }

有参类型继承

父类:

class human {
        String name;
        int age;
        double height;
        double weight;
        public void speak(String text) {
            System.out.println(text);
        }
        public human( String name, int age, double height, double weight) {
            name = name;
            age = age;
            height = height;
            weight = weight;
            System.out.println("Human is created");
        }
    }

则子类必须:

class student extends human {
        int grade;
        public void study() {
            System.out.println("I am studying");
        }
        public student(String name, int age, double height, double weight, int grade) {
            super(name, age, height, weight);	//通过此调用父函数的构造函数
            System.out.println("Student is created");
        }
}