多态内存分析

package Test;

public class TestPolymorphicRequired {
	
	public void shout() {
		
	}
	
	
}


class Cat extends TestPolymorphicRequired{
	
	@Override
	public void shout() {
		
		System.out.println("喵喵喵!");
		
	}
	
}

class Dog extends TestPolymorphicRequired{
	
	@Override
	public void shout() {
		
		System.out.println("汪汪汪!");

	}
	
}
package Test;

public class TestPolymorphic {

	public static void main(String[] args) {
		// TODO 测试多态

		TestPolymorphicRequired d = new Dog();
		Dog dog = (Dog)d;
		
		dog.shout();
		
	}

}

 

 

多态的三个必要条件:

继承,重写,父类引用指向子类对象。

猜你喜欢

转载自blog.csdn.net/FYSGXFZ/article/details/84824097
0条评论
添加一条新回复