继承

来源:互联网 发布:js 判断ie9以下版本 编辑:程序博客网 时间:2024/05/24 03:06

继承是面向对象编程技术的一块基石,因为它允许创建分等级层次的类。运用继承,你能够创建一个通用类,它定义了一系列相关项目的一般特性。该类可以被更具体的类继承,每个具体的类都增加一些自己特有的东西。

在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);  

}  

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();  

System.out.println("Contents of superOb: "); 

superOb.showij();  

System.out.println();  

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++中,你可以继承多个基础类)。你可以按照规定创建一个继承的层次。该层次中,一个子类成为另一个子类的超类。然而,没有类可以成为它自己的超类。

 

成员的访问和继承

尽管子类包括超类的所有成员,它不能访问超类中被声明成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; 

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,它只能被它自己类中的其他成员访问。子类没权访问它。

  

class GrandFather

{

 public void say()

 {

 System.out.println("GrandFather say Hello");

 }

 public GrandFather()

 {

 System.out.println("GrandFather has been created.........");

 }

 

}

class Father extends GrandFather

{

 public void say()

 {

 System.out.println("Father say Hello ");

 }

 public Father()

 {

 System.out.println("Father has been created.........");

 }

 

}

class Son extends Father

{

 String name="Mike";

 public void say()

 {

 System.out.println("Hello "+this.name);

 }

 public Son()

 {

 System.out.println("Son has been created..........");

 }

 

}

public class TestInherit {

 

 public static void main(String[] args) {

 Father f = new Son();

 f.say();

 }

}

0 0