Java基础:继承

示例代码:

  1. class TwoDShapes{
  2.     private  double width;
  3.     private  double height;
  4.     //width 和 height的访问器方法
  5.     double getWidth(){
  6.         return width;
  7.         }
  8.     double getHeight(){
  9.         return height;
  10.         }
  11.     void setWidth(double w){
  12.         width = w;
  13.         }
  14.     void setHeight(double h){
  15.         height = h;
  16.         }    
  17. //父类构造方法
  18.     TwoDShapes(double w,double h){
  19.         width = w;
  20.         height = h;        
  21.         }
  22.  
  23.     void showDim(){
  24.         System.out.println("width and height:"+ width +"and"+ height);
  25.         }    
  26.     }
  27.  
  28. class Triangel extends TwoDShapes{     //通过extends关键字继承
  29.        String style;
  30.        Triangel(String sty,double w,double h){    //子类构造方法,初始化对象的变量和继承TwoDShapes的width,height
  31.             style = sty;                           
  32.             setWidth(w);
  33.             setHeight(h);
  34.         }    
  35. //或者
  36.       Triangel(String sty,double w,double h){    //子类构造方法,super()调用父类构造
  37.                super(w,h);
                   style = sty;
  38.         }
  39.  
  40.     double area(){
  41.         //return width * height /2;              //引用TwoDShapes的成员,不能访问private修饰的成员
  42.         return getWidth()*getHeight()/2;  //private 成员,可以通过访问器方法访问。
  43.         }
  44.     void showStyle(){
  45.         System.out.println("style:"+ style);
  46.         }   
  47.     }
  48.  
  49. class Shapes{
  50.     public static void main(String args[]){
  51.             Triangel t1 = new Triangel();
  52.             Triangel t2 = new Triangel();
  53.             
  54.             t1.width = 4;        //普通赋值width,height 是public修饰
  55.             t1.height =4;
  56.             t1.style ="filled";
  57.                       
  58.             t2.setWidth(8.0);    //通过访问器方法设置,width,height是private修饰
  59.             t2.setHeight(12.0);
  60.             t2.style ="outlined";            
  61.  
  62.             System.out.println("t1:");
  63.             t1.showDim(); 
  64.             t1.showStyle();
  65.             System.out.println("t1.area:"+ t1.area());
  66.             
  67.             System.out.println("t2:");
  68.             t2.showDim();            
  69.             t2.showStyle();
  70.             System.out.println("t2.area:"+ t2.area());
  71.             
  72.             Triangel t3 = new Triangel("filled",4.0,4.0);      //通过子类方法构建对象                 
  73.             System.out.println("t3:");
  74.             t3.showDim();            
  75.             t3.showStyle();
  76.             System.out.println("t3.area:"+ t3.area());
  77.         }  
  78.     }

---------------------------------------------------------------------------------------

使用super调用父类成员:

  1. class A{
  2.         int i;
  3.     }
  4.     
  5. class B extends A{
  6.         int i;
  7.         B(int a,int b){
  8.             super.i = a;    //调用父类i
  9.             i = b;
  10.         }
  11.         
  12.     void show(){
  13.         System.out.println("super.i:"+ super.i);
  14.         System.out.println("subclass.i:"+ i);
  15.         }       
  16.     }    
  17.  
  18. class UseSuper{
  19.     public static void main(String args[]){
  20.         B ob = new B(1,2);
  21.         ob.show();
  22.         }    
  23.     }

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/87938852