无法从静态上下文中引用非静态 方法 是什么原因,还有什么错误,谢谢
问题:
定义宠物类,定义Dog,Cat 子类
定义主人类,实现和宠物类玩方法playwith( )
在测试类中用子类实例调用主人类的playwith()方法
我·:1.接口
package pa; public interface Pet { public abstract void playwith(); }
2.Dog类
package pa; public class Dog implements Pet{ @Override public void playwith() { System.out.println("The onwer is playing with the dog."); } }
3.Cat类
package pa; public class Cat implements Pet{ @Override public void playwith() { System.out.println("The owner is playing with the cat."); } }
4.Owner类
package pa; public class Owner { public static void main(String[] args) { Pet pet =new Cat(); Pet.playwith(); Pet pet =new Dog(); Pet.playwith(); } }
运行结果:
无法从静态上下文中引用非静态 方法 playwith()
已在方法 main(java.lang.String[])中定义了变量 pet