Java 继承初探

Java继承的基础

Java中,被继承的类叫做超类,继承超类的类叫子类。(一个子类亦可以是另一个类的超类)

继承一个类,只需要用关键字 extends 把一个类的定义合并到另一个类中就可以了。

例子中创建了一个超类A和一个名为B的子类。

class A {

         int i,j ;

         void showij() {

                   System.out.println( "i and j : " + i + " " + j ) ;

         }

}

扫描二维码关注公众号,回复: 5672058 查看本文章

class B extends A

         // B类继承了A类 由此B类可以直接使用A类的所有内容

         int k ;

        

         void showk() {

                   System.out.println("K: " + k ) ;

         }

         void sum() { // 因为B继承了A所以可以获得A类中的变量i,j

                   System.out.println("i+j+k: " + (i+j+k)) ;

         }

}

public class SimpleInheritance {

         public static void main(String args[]) {

                   A superOb = new A() ;

                   B subOb = new B() ;

                   superOb.i = 10 ;

                   superOb.j = 20 ;

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

                   // 因为继承A类,所以B类的实例对象可以调用A类的方法

                   subOb.showk() ;

                   System.out.println() ;

                   System.out.println("Sum of i,j and k in subOb: ") ;

                   subOb.sum() ;

         }

}

虽然子类包括超类的所有成员,但是子类不能访问超类中被声明为private的成员。

class Box {

         double width ;

         double height ;

         double depth ;

         Box(Box ob) {

                   width = ob.width ;

                   height = ob.height ;

                   depth = ob.depth ;

         }

         Box() {

                   width = -1 ;

                   height = -1 ;

                   depth = -1 ;

         }

        

         Box(double len) {

                   width = height = depth = len ;

         }

         double volume() {

                   return width * height * depth ;

         }

}

class BoxWeight extends Box {  // BoxWeight 继承了Box的所有特征(功能)

    // 在继承Box类后,子类BoxWeight也可以在不改变Box类的情况下独立完成成员的添加

 

    double weight ;  //为自己添加了一个变量成员

 

    BoxWeight (double w , double h , double d , double m ) {

       width = w ;

       height = h ;

       depth = d ;

       weight = m ;

    }

}

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

                   System.out.println();

         }

}

超类的一个引用变量可以被任何从该超类派生的子类的引用赋值。

理解是引用变量的类型,而不是引用对象的类型;决定了什么成员可以被访问。

也就是说,当一个子类对象的引用被赋给一个超类引用变量时,你只能访问超类定义的对象的那一部分。这就是下例中为什么plainbox不能范文weight的原因,甚至是他引用了一个BoxWeight对象也不行。

因为超类不知道子类增加的属性(反之则知道)。

下例中,Box的引用访问weight域是不可能的,因为Box类没有定义。

class RefDemo {

         public static void main(String args[]) {

                   // weightbox 是 BoxWeight对象的一个引用,

                   BoxWeight weightbox = new BoxWeight(3,5,7,8.37) ;

                   // plainbox是Box对象的一个引用,

                   Box plainbox = new Box() ;

                   double vol ;

                   vol = weightbox.volume() ;

                   System.out.println("Volume of weightbox is " + vol ) ;

                   System.out.println("Weight of weightbox is " + weightbox.weight) ;

                   System.out.println() ;

                  plainbox = weightbox ;  // weightbox的对象引用给plainbox赋值*

                   vol = plainbox.volume() ;

                   System.out.println("Volume of plainbox is " + vol) ;

                   //System.out.println("Weight of plainbox is  " + plainbox.weight) ;

                   // 不可以访问 weight,因为在超类中没有赋予Box.plainbox访问的权利

         }

}

猜你喜欢

转载自www.cnblogs.com/wangyuyang1016/p/10604771.html