21.多态
1.概述
:
2.多态中成员的访问特点
代码:
父类 public class Animal { public String name = "小动物"; public void dosomething() { System.out.println("动物都要吃东西!!!"); } } 子类 public class Dog extends Animal { public String name = "旺旺"; public int age = 11; @Override public void dosomething() { System.out.println("狗吃屎!!!"); } public void eat() { System.out.println("eat"); } } 执行: public class Demo { public static void main(String[] args) { Animal animal = new Dog(); System.out.println(animal.name);//小动物 // System.out.println(animal.age);编译报错,因为Animal类里面没有age变量 animal.dosomething();//狗吃屎 // animal.eat();编译报错,因为Animal类里面没有eat方法 } }
3.多态好处与弊端