创建一个class,数据字段包括main成分字符串, bread type字符串,双精度字符串,创建method获取设置每个字段的值,创建一个应用程序实例化一个对象然后演示set和get的应用

创建一个class名为Sandwich,数据字段包括main成分字符串, bread type字符串,双精度字符串,创建method获取设置每个字段的值,创建一个应用程序名为TestSandwich实例化一个Sandwich对象然后演示set和get的应用

img

img

img

提供的代码发一下。

单纯的补齐代码吗?


public class Sandwich {
    private String mainIngredient;
    private String bread;
    private double price;
    
    //the public get and set methods
    public void setMainIngredient(String ingredient) {
        this.mainIngredient = ingredient;
    }
    
    public String getMainIngredient() {
        return mainIngredient;
    }
    public void setBread(String breadType) {
        this.bread = breadType;
    }
    public String getBread() {
        return bread;
    }
    public void setPrice(double amt) {
        this.price = amt;
    }
    public double getPrice() {
        return price;
    }

}

TestSandich.java



public class TestSandwich {
    
    public static void main(String[] args ) {
        Sandwich sandwich = new Sandwich();
        sandwich.setMainIngredient("cheese");
        sandwich.setBread("rye");
        sandwich.setPrice(10);
        
        System.out.println("You have orderd a :" + 
        sandwich.getMainIngredient() + " sandwich on " +
        sandwich.getBread() + " bread, and the price is " + 
        sandwich.getPrice());
        
    }

}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632