Java 继承(Java extends)
Java 继承的一个关键字就是 extends,通过 extends 就可以拥有父类的行为
我们先看一个例子
public class People {
public void eat()
{
System.out.println("吃饭...");
}
}
public class Student extends People{
public static void main(String[] args)
{
Student stu = new Student();
stu.eat();
}
}
输出结果为
吃饭...
为什么Student 没有定义eat, 确能调用People 的 eat() 方法呢?
原因是因为Student 继承了Peopole ,那么Student 就有了People所有的 public 类的方法或 protected类型的方法;
注意,如果是私有类型的方法或参数子类也是无法访问的!
说白了什么都不用干就拥有了一切,这日子真好!