不知道能不能进黑马,我都愁死了
/*
多态的小练习:
需求:电脑运行实例,
*/
/*interface PCI
{
abstract void open();
abstract void close();
}
class Net implements PCI
{
public void open()
{
System.out.println("net-----run");
}
public void close()
{
System.out.println("net-----close");
}
}
class Qq implements PCI
{
public void open()
{
System.out.println("QQ-----run");
}
public void close()
{
System.out.println("QQ-----close");
}
}
class ComputerTest
{
public static void function(PCI p)
{
if(p instanceof Net)
{
Net n=(Net)p;
n.open();
n.close();
}
else if(p instanceof Qq)
{
Qq q=(Qq)p;
q.open();
q.close();
}
}
public static void main(String args[])
{
PCI n=new Net();
n.open();
n.close();
PCI q=new Qq();
q.open();
q.close();
function(new Net());
function(new Qq());
}
}
*/
abstract class Animal
{
abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println("吃鱼");
}
public void catchMouse()
{
System.out.println("抓老鼠");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("啃骨头");
}
public void protectF()
{
System.out.println("看家");
}
}
class AnimalTest
{
public static void function(Animal a)
{
if(a instanceof Cat)//用instaceof关键字判断实例该存属于那个对象
{
Cat c=(Cat)a;
a.eat( |
|