本帖最后由 张海涛 于 2012-12-11 01:07 编辑
57行为什么必须要加static,不加就报错??- abstract class Animal
- {
- public abstract void eat();
- }
- class Cat extends Animal
- {
- public void eat()
- {
- System.out.println("eat fish");
- }
- public void catchMoust()
- {
- System.out.println("catch mouse");
- }
- }
- class Dog extends Animal
- {
- public void eat()
- {
- System.out.println("eat bone");
- }
- public void LookHome()
- {
- System.out.println("look home");
- }
- }
- class Pig extends Animal
- {
- public void eat()
- {
- System.out.println("si liao");
- }
- public void gongDi()
- {
- System.out.println("gong di");
- }
- }
- class DuoTaiDemo
- {
- public static void main(String[] args)
- {
- /*
- Cat c1 = new Cat();
- Cat c2 = new Cat();
- Pig p = new Pig();
- function(c1);
- function(new Dog());
- function(p);
- */
- //Animal c = new Cat();
- //c.eat();
- function(new Cat());
- function(new Dog());
- function(new Pig());
- }
-
- public static void function(Animal a)//这里为什么必须要加static
- {
- a.eat();
- }
- /*
- public static void function(Cat c)
- {
- c.eat();
- }
- public static void function(Dog d)
- {
- d.eat();
- }
- public static void function(Pig p)
- {
- p.eat();
- }
- */
- }
复制代码 |