1、什么是继承:一个类从另外一个类中得到成员、属性和行为方法等。

案例:

  //父类
  public class Animal {
    // 变量
    public int height=1;

    // 方法 
    public void sayHeight(){
      System.out.println(height);
    };
  };

 

  //子类
  public class Cat extends Animal{

    // 变量

    public int width=2;
    // 方法
    public void sayWidth(){
      System.out.println(width);
    };
  };

 

//测试 

public class numberMain {

      public static void main(String[] args) {
           Cat c=new Cat();
           c.sayHeight();
           c.sayWidth();
      }
}

 

结果: 

 1
 2

posted on 2016-06-10 11:44  AMWHRW  阅读(158)  评论(0)    收藏  举报