现在的位置: 首页 > 综合 > 正文

Java Programming: simple Polymorphic programming

2013年10月09日 ⁄ 综合 ⁄ 共 421字 ⁄ 字号 评论关闭
public class Dog extends Animal{

	public void noise(){
		System.out.println("Ruff");
	}
}

public class Fish extends Animal{

	public void noise(){
		System.out.println("Gree");
	}
}

public class Animal {
	
	public void noise(){
		System.out.println("Animal dint amke noise");
	}
}

public class Main {
	public static void main(String[] args){
		
		Animal[] thelist = new Animal[2];
		Dog d = new Dog();
		Fish f = new Fish();
		
		thelist[0] = d;
		thelist[1] = f;
		
		for(Animal x: thelist){
			x.noise();
		}
		
	}
}

the result is ;

Ruff
Gree

抱歉!评论已关闭.