首页 > 代码库 > 8 继承

8 继承

面向对象的特征:继承,封装和多态

java 当中只支持单继承,不允许多继承

class Person

{

  String name;

   int age;

  void eat()

  {

    System.out.println("吃饭");

  }

  void introduce()

  {

    System.out.println(“我的名字是"+name+",我的年龄是"+age);

  }

}

继承的关键字:extends

class Student extends Person

{
  int grade;

  void study()

  {

    System.out.println("学习");

  }
}

class Test

{

  public static void main(String args[])

  {
    Student student=new Student();

    student.name="zhangsan";

    student.age=18;

    student.eat();

    student.introduce();
  }

}

 

8 继承