20160722(A) 多态/好处/成员基本特性/向上和下转型

 多态:

某一类事物的多种存在形态(在运行时的具体体现)

interface Animal
{
	public void say();
}

class Dog implements Animal
{
	public void say()
		{
		  System.out.println("狗叫");
	    }
    public void show()
		{
		  System.out.println("疯狗");
	    }
}
class  Demon1
{
	public static void main(String[] args) 
	{
		//狗类
		Dog dog=new Dog();
		dog.say();

		//使用接口装具体类的对象
		Animal dog1=new Dog();
		dog1.say();

	}
}

好处:

interface Animal
{
	public void say();
	public void eat();
}

//狗
class Dog implements Animal
{
   public void say()
	{
         System.out.println("狗叫");
    }
   public void eat()
	   {
	     System.out.println("狗吃饭");
       }
}

//猫
class Cat implements Animal
{
	public void say()
		{
		System.out.println("猫叫");
	    }
    public void eat()
		{
		 System.out.println("猫吃饭");
	    }
} 


class  Demon2
{
	public static void main(String[] args) 
	{
		//Dog
	Dog dog=new Dog();
		//dog.say();//狗叫
		//dog.eat();狗吃饭
		//dogPrint(dog);//--①

		//Cat
        Cat cat1=new Cat();
		//catPrint(cat);//--②

        //多态方法--打印所有动物||前提:创建了对应的对象
		System.out.println("******************");
		printAllAnimal(dog);//狗叫 狗吃饭
		printAllAnimal(cat1);//猫叫 猫吃饭

	}

	//狗打印方法--①
	public static void dogPrint (Dog dog)//方法只能调用静态方法
		{
	         dog.say();
		 dog.eat();
	    }

    //猫打印方法--②
	public static void catPrint(Cat cat)
		{
		  cat.say();
		  cat.eat();
	    }

    



	//兔子打印,鸟打印...


	//多态方法
    //      |
    //     \|/
	//所有动物类打印
	public static void printAllAnimal(Animal animal)
		{
                 animal.say();
		 animal.eat();

	    }
}<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space: normal;">
</span></span>

多态成员基本特性:

    1.成员变量:
         多态中的不能访问"具体实现类"的"特有属性"
可以访问自己的常量,但不能去访问实现类中特有的属性
    2.成员方法:
    多态中不能访问"具体实现类"中"特有的方法".

   弊端:多态中不能去访问具体实现类的特有的成员
interface Animal
{
	public static final int NUM=20;//常量
    //  ------ ------ -----

	public void eat();
}

class Dog implements Animal
{
	int age=10;

	public void eat()
		{
		  System.out.println("狗吃饭");
	    }
    
	public void say()//实现类中特有的
	    {
		  System.out.println("狗狗叫"); 
		}
}
class Demon3 
{
	public static void main(String[] args) 
	{
		Dog dog=new Dog();
		dog.eat();
		dog.say();

        System.out.println("****************"); 

		System.out.println(dog.NUM);//20
		System.out.println(dog.age);//10
		//System.out.println(dog.say);

		System.out.println("*****************");
        
		//之前都是正常的写法
		//多态 测试(用接口去装子类)
		Animal dog1=new Dog();
		System.out.println(dog1.NUM);//20
		dog1.eat();
		//System.out.println(dog1.age);//具体实现类中的特有成员
	        //dog1.say();                  //具体实现类中的特有方法
	} 
}

多态的向上转型 向下转型

interface Animal
  {
 public void eat();
  }

  class Dog implements Animal
  {
    public void eat()
{
...
}
public void say()
{
   ...
}
  }

  Dog dog=new dog();
  dog.eat();

  Animal dog1=new Dog();//这就是多态,也就是对象向上转型
  dog1.say();//这是错误的

  Dog dog2=(Dog) dog1;//这就是多态的向下转型
  dog2.say();
  

interface Animal
{
	public void say();
}

class Dog implements Animal
{
	public void say()
		{
		  System.out.println("狗狗说话");
	    }

    public void eat()
		{
		  System.out.println("狗狗吃饭");
	    }
}

class Demon4 
{
	public static void main(String[] args) 
	{
		Dog dog=new Dog();
		dog.say();
		dog.eat();

		System.out.println("********************");
        
		//多态的对象向上转型
		Animal dog1=new Dog();
		//dog1.eat();//实现类的特有属性,错误
		dog1.say();
        
		//多态的对象向下转型
		Dog dog2=(Dog) dog1;//dog1强转成dog类型,再用dog类去装
		dog2.eat();//实现类的特有属性,可以

		//Cat cat=(Cat) dog1;//错误
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值