为什么在实现多重继承时会出错?
import java.util.*;
interface Bakery
{
public void bake(String cake,String cream);
public void serve();
}
interface FoodCourt
{
int cost=50;
public void items(String puffs, String drinks);
public void buffet(int cost, String delivery);
}
class Stall implements
Bakery,FoodCourt
{
public void bake(String cake, String cream)
{
System.out.println("Baking A Cake");
}
public void serve()
{
System.out.println("Serving the items");
}
public void items(String puffs, String drinks);
{
System.out.println("This is Item List");
}
public void buffet(int cost, String delivery);
{
System.out.println("Buffet Starts");
}
}
class Dcoder
{
public static void main(String[] args)
{
Stall b1=new Stall();
b1.bake("BlackForest", "Chocolate");
b1.serve();
FoodCourt f1=new Stall();
f1.items("EggPuff", "ThumsUp");
f1.buffet(50,"COD");
}
}`
输出:
source_file.java:25: error: missing method body, or declare abstract
public void items(String puffs, String drinks);
^
source_file.java:29: error: missing method body, or declare abstract
public void buffet(int cost, String delivery);
^
2 errors
答案 0 :(得分:2)
这是Robby提到的正确代码。
去除这两个方法签名后的分号
import java.util.*;
interface Bakery
{
public void bake(String cake,String cream);
public void serve();
}
interface FoodCourt
{
int cost=50;
public void items(String puffs, String drinks);
public void buffet(int cost, String delivery);
}
class Stall implements
Bakery,FoodCourt
{
public void bake(String cake, String cream)
{
System.out.println("Baking A Cake");
}
public void serve()
{
System.out.println("Serving the items");
}
public void items(String puffs, String drinks)
{
System.out.println("This is Item List");
}
public void buffet(int cost, String delivery)
{
System.out.println("Buffet Starts");
}
}
public class Dcoder
{
public static void main(String[] args)
{
Stall b1=new Stall();
b1.bake("BlackForest", "Chocolate");
b1.serve();
FoodCourt f1=new Stall();
f1.items("EggPuff", "ThumsUp");
f1.buffet(50,"COD");
}
}
答案 1 :(得分:-1)
这是可执行代码
public class Dcoder {
public static void main(String[] args)
{
Stall b1=new Stall();
b1.bake("BlackForest", "Chocolate");
b1.serve();
FoodCourt f1=new Stall();
f1.items("EggPuff", "ThumsUp");
f1.buffet(50,"COD");
}
interface Bakery
{
public void bake(String cake,String cream);
public void serve();
}
interface FoodCourt
{
int cost=50;
public void items(String puffs, String drinks);
public void buffet(int cost, String delivery);
}
public static class Stall implements
Bakery,FoodCourt
{
public void bake(String cake, String cream)
{
System.out.println("Baking A Cake");
}
public void serve()
{
System.out.println("Serving the items");
}
public void items(String puffs, String drinks)
{
System.out.println("This is Item List");
}
public void buffet(int cost, String delivery)
{
System.out.println("Buffet Starts");
}
}
}
输出:
Baking A Cake
Serving the items
This is Item List
Buffet Starts
BUILD SUCCESSFUL (total time: 0 seconds)
答案 2 :(得分:-1)
您需要删除;从实现类中,更新停顿类中的以下方法
**public void items(String puffs, String drinks) {
System.out.println("This is Item List");
}
public void buffet(int cost, String delivery)
{
System.out.println("Buffet Starts");
}**