目前 初学interface
参考网站为
https://reurl.cc/MvGnWK
我按照教学打 但编译不过 有错误
罗辑上 我是看不出来有什么问题
没有什么头绪 可能是因为才正在学
其中最下面店员的类 其中的方法 我的IDE叫我要宣告无回传void 这是必要的吗
有人可以引导一下吗
小弟无意当个伸手党 给个关键字也好 我可以自己拜google
先感谢 各路大神..<(_ _)>
public class Interface_test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Clerk C = new Clerk(); //实例店员
PetDog dog = new PetDog(); //实例狗
PetCat cat = new PetCat(); //实例猫
C.sellFood(dog); //我是狗,我要买狗粮,汪汪!!
C.sellFood(cat); //我是猫,我要买猫粮,喵喵!!
}
public interface PetRestaurant{
public void buyFood();
}
//宠物狗想要买狗粮
public class PetDog implements PetRestaurant{
@Override //实现接口方法
public void buyFood(){
System.out.println("我是狗,我要买狗粮,汪汪!!");
}
}
//宠物猫想要买猫粮
public class PetCat implements PetRestaurant{
@Override
public void buyFood(){
System.out.println("我是猫,我要买猫粮,喵喵!!");
}
}
public class Clerk{ //店员
public sellFood(PetRestaurant pet){ //店员卖食物给实践过的宠物
pet.buyFood(); //宠物买粮食
}
}
}