JAVA小练习66——多态的练习(1)

abstract class Animal{

	String name;

	String color;
	
	int nextAge=2;

	static	int age = 10;

	public Animal(String name,String color){
		this.name = name;
		this.color = color;
	}

	public abstract void run();
	
	public void eat() {
		System.out.println("动物吃饭...");
	}

	public static void sleep(){
		System.out.println("动物睡觉...");
	}
}


//狗
class Dog extends Animal{

	static int age = 20; 
	int nextAge=4;

	public Dog(String name,String color){
		super(name,color);
	}
	
	public  void run(){
		System.out.println(name+"四条腿跑...");
	}
	
	public void eat() {
		System.out.println("狗吃饭...");
	}

	public static void sleep(){
		System.out.println("狗在睡觉...");
	}
	
	//狗特有的方法
	public void bite(){
		System.out.println(name+"咬人");
	}

}


class Demo66 
{
	public static void main(String[] args) 
	{
				
		Animal a = new Dog("哈士奇","白色");  //多态   父类的引用类型变量指向了子类的对象。
		System.out.println(a.color+a.name);
		System.out.println("年龄:"+ Animal.age+"  nextAge的值:"+a.nextAge);
		Animal.sleep();
		a.eat();
		//a.bite();   ---多态情况下,不能访问子类特有的成员
		
	}
}

你可能感兴趣的:(JAVA小练习66——多态的练习(1))