什么是封装、继承和多态
package Day01;
/*什么是继承*/
public class Demo27 {
public static void main(String[] args) {
Cat c=new Cat();
Animal animal=new Animal();
System.out.println(c.age);
System.out.println(c.name);
c.eat();//打印Cat类的方法
//();//会报错,因为父类不可以调用子类的私有方法
//注:子类不可调用父类的私有方法,不是因为没有继承,而是子类对父类的私有资源不可见
//();
}
}
class Animal{
String name;
Integer age;
private Integer ID;
public Animal() {//子类会默认使用父类的无参构造
System.out.println("我是父类的无参构造");
}
public void eat(){//定义父类的方法
System.out.println("动物会吃东西");
}
}
class Cat extends Animal{//创建Animal的子类Cat
/*注意:子类会默认使用父类的无参构造*/
public void eat(){//子类可以重写父类的方法
System.out.println("小猫喜欢吃小鱼干!");
}
private void jump(){//定义子类的私有方法
System.out.println("小猫会跳");
}
}