transtype.java
来自「Java面向对象编程(随书配套源代码) 阐述了面向对象编程的思想」· Java 代码 · 共 48 行
JAVA
48 行
package chapter7;
import java.util.Random;
public class TransType
{
public static void main(String args[])
{
Wolf wolf = new Wolf();
Sheep sheep = new Sheep();
Duck duck = new Duck();
Goal goal = new Goal();
Animal animal[] = {wolf,sheep,duck,goal};
duck = null;
duck = (Duck) animal[2];
duck.layEgg( );
if(animal[2] instanceof Wolf)
{
wolf = (Wolf) animal[2];
wolf.eat("羊");
}
Random random = new Random();
Animal aniObj = animal[random.nextInt(4)];
if(aniObj instanceof Wolf)
{
aniObj.eat("羊");
}
else if(aniObj instanceof Sheep)
{
aniObj.eat("草");
}
else if(aniObj instanceof Duck)
{
duck = (Duck) aniObj;
duck.layEgg();
}
else
{
System.out.println("我是一个怪物!");
}
StaticNestedOuterClass outer = new StaticNestedOuterClass();
//StaticNestedOuterClass.innerClass inner = outer.new innerClass() ;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?