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();   ---多态情况下,不能访问子类特有的成员
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值