继承关键字是extends,子类可以使用父类的方法
先调用父类的构造方法,再调用子类的构造方法
子类的构造方法第一行默认给出一个Super();
访问成员变量用this,访问父类变量用Super()
私有方法不能被重写
继承的好处是提高了代码的复用性,提高了代码的维护性
坏处是降低了代码的独立性
Java是单继承
案例:猫和狗
public class DongWu {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DongWu() {
}
public DongWu(int age,String name) {
this.age=age;
this.name=name;
}
}
*********************************************
public class Dog extends DongWu{
public Dog() {
super();
}
public Dog(int age,String name) {
super(age,name);
}
public void KanMen() {
System.out.println("看门");
}
}
***********************************************
public class Cat extends DongWu {
public Cat() {
super();
}
public Cat(int age,String name) {
super(age,name);
}
public void CatchMouse() {
System.out.println("抓老鼠");
}
}
*********************************************
// TODO Auto-generated method stub
Cat c1=new Cat();
c1.setAge(5);
c1.setName("Gl");
c1.CatchMouse();
}