永发信息网

java接口程序有一个错误 (做出了+30分,帮帮忙~)

答案:1  悬赏:40  手机版
解决时间 2021-04-15 11:25
  • 提问者网友:浮克旳回音
  • 2021-04-15 06:58

程序本来是对了的,但是我想用这种方法用导致有1个错误,我不知道怎么改~~

(不是这道,我想参考这道题这样做~~)

interface Runner{public void run();}
interface Swimmer{public double swim();}
class Animal{ public int eat(){return -1;} }
class Person extends Animal implements Runner,Swimmer{
public void run(){ System.out.println("runing");}
public double swim(){System.out.println("swimming");return -1;}
public int eat(){System.out.println("eating");return -1;}
}

public class TestInterface{
public static void main(String args[]){
TestInterface t=new TestInterface();
Person p=new Person();
t.m1(p);
t.m2(p);
t.m3(p);
}
public void m1(Runner f){f.run();}
public void m2(Swimmer s){s.swim();}
public void m3(Animal a){a.eat();}
}

这道才是~~

interface Flyable{
public void fly();
}

class Bird implements Flyable{
public void fly(){
System.out.println("bird is flying in the air");
}
}

class Plane implements Flyable{
public void fly(){
System.out.println("plane is flying in the air");
}
}

class TestFlyable {

public static void main(String[] args){
TestFlyable a=new TestFlyable;
Bird b=new Bird();
Plane p=new Plane();
a.flying(b);
a.flying(p);
}


public void flying(Flyable f){
f.fly();
}

}

最佳答案
  • 五星知识达人网友:醉吻情书
  • 2021-04-15 07:07

把TestFlyable类改为public,然后TestFlyable a=new TestFlyable;这里需要加一个();


完整代码如下;


interface Flyable{
public void fly();
}


class Bird implements Flyable{
public void fly(){
System.out.println("bird is flying in the air");
}
}


class Plane implements Flyable{
public void fly(){
System.out.println("plane is flying in the air");
}
}


public class TestFlyable {


public static void main(String[] args){
TestFlyable a=new TestFlyable();
Bird b=new Bird();
Plane p=new Plane();
a.flying(b);
a.flying(p);
}



public void flying(Flyable f){
f.fly();
}


}

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯