继承是面向对象编程技术的一块基石,因为它允许创建分等级层次的类。运用继承,你能够创建一个通用类,它定义了一系列相关项目的一般特性。该类可以被更具体的类继承,每个具体的类都增加一些自己特有的东西。在java 术语学中,被继承的类叫超类(superclass ),继承超类的类叫子类(subclass )。因此,子类是超类的一个专门用途的版本,它继承了超类定义的所有实例变量和方法,并且为它自己增添了独特的元素。
继承一个类,只要用extends 关键字把一个类的定义合并到另一个中就可以了。为了理解怎样继承,让我们从简短的程序开始。下面的例子创建了一个超类a和一个名为b的子类。注意怎样用关键字extends 来创建a的一个子类。
// a simple example of inheritance.
// create a superclass.
class a {
int i, j;
void showij() { system.out.println("i and j: " + i + " " + j); }}
// create a subclass by extending class a.
class b extends a {
int k;
void showk() {
system.out.println("k: " + k); } void sum() {
system.out.println("i+j+k: " + (i+j+k));
}
}
class simpleinheritance {
public static void main(string args[]) {
a superob = new a();
b subob = new b();
// the superclass may be used by itself. superob.i = 10; superob.j = 20;
system.out.println("contents of superob: "); superob.showij(); system.out.println();
/* the subclass has access to all public members of
its superclass. */ subob.i = 7; subob.j = 8; subob.k = 9; system.out.println("contents of subob: "); subob.showij();subob.showk(); system.out.println();
system.out.println("sum of i, j and k in subob:");
subob.sum();
}
}
该程序的输出如下:
contents of superob:
i and j: 10 20
contents of subob:
i and j: 7 8
k: 9
sum of i, j and k in subob:
i+j+k: 24
像你所看到的,子类b包括它的超类a中的所有成员。这是为什么subob 可以获取i和j 以及调用showij( ) 方法的原因。同样,sum( ) 内部,i和j可以被直接引用,就像它们是b的一部分。
尽管a是b的超类,它也是一个完全独立的类。作为一个子类的超类并不意味着超类不能被自己使用。而且,一个子类可以是另一个类的超类。声明一个继承超类的类的通常形式如下:
class subclass-name extends superclass-name {
// body of class
}
你只能给你所创建的每个子类定义一个超类。java 不支持多超类的继承(这与c++ 不同,在c++中,你可以继承多个基础类)。你可以按照规定创建一个继承的层次。该层次中,一个子类成为另一个子类的超类。然而,没有类可以成为它自己的超类。
8.1.1 成员的访问和继承
尽管子类包括超类的所有成员,它不能访问超类中被声明成private 的成员。例如,考虑下面简单的类层次结构:
/* in a class hierarchy, private members remain private to their class.
this program contains an error and will not
compile.
*/
// create a superclass.
class a {int i; // public by default private int j; // private to a
void setij(int x, int y) { i = x; j = y;
}
}
// a's j is not accessible here.
class b extends a { int total; void sum() {
total = i + j; // error, j is not accessible here
}
}
class access {
public static void main(string args[]) {
b subob = new b();
subob.setij(10, 12);
subob.sum();
system.out.println("total is " + subob.total);
}
}
该程序不会编译,因为b中sum( ) 方法内部对j的引用是不合法的。既然j被声明成private,它只能被它自己类中的其他成员访问。子类没权访问它。
注意:一个被定义成private 的类成员为此类私有,它不能被该类外的所有代码访问,包括子类。
8.1.2 更实际的例子
让我们看一个更实际的例子,该例子有助于阐述继承的作用。这里,前面章节改进的box类的最后版本将被扩展。它包括第四成员名为weight 。这样,新的类将包含一个盒子的宽度、高度、深度和重量。
// this program uses inheritance to extend box.
class box { double width; double height; double depth;
// construct clone of an object
box(box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth;
}
// constructor used when all dimensions specified
box(double w, double h, double d) { width = w; height = h; depth = d;
}
// constructor used when no dimensions specified
box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box
}
// constructor used when cube is created box(double len) { width = height = depth = len; }
// compute and return volume double volume() { return width * height * depth; }}
// here, box is extended to include weight.class boxweight extends box {double weight; // weight of box
// constructor for boxweight
boxweight(double w, double h, double d, double m) { width = w; height = h; depth = d; weight = m;
}}class demoboxweight {
public static void main(string args[]) { boxweight mybox1 = new boxweight(10, 20, 15, 34.3); boxweight mybox2 = new boxweight(2, 3, 4, 0.076); double vol;
vol = mybox1.volume(); system.out.println("volume of mybox1 is " + vol); system.out.println("weight of mybox1 is " + mybox1.weight); system.out.println();
vol = mybox2.volume(); system.out.println("volume of mybox2 is " + vol); system.out.println("weight of mybox2 is " + mybox2.weight);
}
}
该程序的输出显示如下:
volume of mybox1 is 3000.0
weight of mybox1 is 34.3
volume of mybox2 is 24.0
weight of mybox2 is 0.076
boxweight 继承了box 的
闽公网安备 35060202000074号