最近读到了接口,所以创了一个名叫Phone 的抽象类别、两个分别叫SurfInternet
和Chat的接口,但编译过程一直出现错误,说非static的方法不能被static的东西给关
联,但我就是没看到有东西被我用成static啊!拜托各位帮帮我。
interface SurfInternet
{
void SurfInternet(String WebName);
}
interface Chat
{
void Chat(String ToWhom);
}
abstract class Phone
{
int price;
abstract void expensive();
void PrintPrice()
{
System.out.println("Price="+price);
}
}
class Sony extends Phone implements SurfInternet,Chat
{
public Sony(int price,String WebName,String ToWhom)
{
this.price=price;SurfInternet(WebName);Chat(ToWhom);
}
public void Chat(String ToWhom)
{
System.out.println("Talking to "+ToWhom);
}
public void SurfInternet(String WebName)
{
System.out.println("Surfing "+WebName);
}
public void expensive()
{
System.out.println("It is so expensive!");
}
}
public class Test
{
public static void main(String[] args)
{
Sony sony=new Sony(10000,"yahoo","John");
Sony.expensive();
}
}
结果:
Test.java:42: error: non-static method expensive() cannot be referenced from a static context
Sony.expensive();
^
1 error
到底~~~﹍﹍
不知道我有没有理解错,接口比抽象类别还要更抽象简洁,抽象类别是给和自己有关连的东西继承的,
而接口只是要给他多个功能用而已。然后还是搞不懂这两个东西到底有什么实际用途,就算没有这两个东西
好像也不会怎样,难道他们只是用来定义东西的吗?