构造器的基本架构

构造器的基本架构

public class drawa {

private String name;
private double price;
//计数起始数字为0
private static int counter = 0;

public drawa() {
}

public drawa(String n, double p) {
    name = n;
    price = p;
    //根据n得到counter的值
    counter++;
}

//得到名字的方法
public String getName() {
    return name;
}
 //得到数据的方法
public double getPrice() {
    return price;
}

// 设置名称和价格的方法
public void setName(String n) {
    name = n;
}
public void setPrice(double p) {
    price = p;
}

public String toString() {

    return "Meal Name=" + name + ", Price=" + price + ", Calories=";
}

//
public static int getCount() {
    return counter;
}

public static void main(String[] args) {

    drawa dinner = new drawa("scampi", 5.49);
    //名字为scampi,价格为5.49
    System.out.println(dinner.toString());
    System.out.println(drawa.getCount() + " meals have been created so far");
}

}
如果想要加上枚举,这里有两种方法

方法一

代码如下:
public class drawa {

enum Diet {
    OMNIVOROUS, VEGAN, VEGETARIAN, UNSPECIFIED
};
//enum提供的选项

private String name;
private double price;
private static int counter = 0;
private Diet diet;

public drawa() {
}

public drawa(String n, double p, Diet d) {
    name = n;
    price = p;
    diet = d;
    counter++;
}

// get method declarations
public String getName() {
    return name;
}

public double getPrice() {
    return price;
}

public void setDiet(Diet d) {
    diet = d;
}

public Diet getDiet() {
    return diet;
}

// set method declarations
public void setName(String n) {
    name = n;
}

public void setPrice(double p) {
    price = p;
}

public String toString() {

    return "Meal Name=" + name + ", Price=" + price + ", " + " and Diet=" + diet;
}

//
public static int getCount() {
    return counter;
}

public static void main(String[] args) {

    drawa dinner = new drawa("Special Pizza", 8.99, Diet.VEGETARIAN);
    System.out.println(dinner.toString());

}

}
运行后的结果是这样的:
Meal Name=Special Pizza, Price=8.99, and Diet=VEGETARIAN
如果是在new drawa没写 Diet.VEGETARIAN,那么最后的结果就是:
Meal Name=Special Pizza, Price=8.99, and Diet=null
类应该有一个toString方法
•枚举是一种类
•枚举可以有自己的toString方法

方法二

public class drawa {
enum Diet {
OMNIVOROUS, VEGAN, VEGETARIAN, UNSPECIFIED
};

private String name;
private double price;
private static int counter = 0;
//
private Diet diet;

public drawa() {
}

public drawa(String n, double p, Diet d) {
    name = n;
    price = p;
    diet = d;
    counter++;
}

// get method declarations
public String getName() {
    return name;
}

public double getPrice() {
    return price;
}

public void setDiet(Diet d) {
    diet = d;
}

public Diet getDiet() {
    return diet;
}

// set method declarations
public void setName(String n) {
    name = n;
}

public void setPrice(double p) {
    price = p;
}

public String toString() {
    String priceAndCalories = ", Price=" + price + ", ";
    if (diet == null)
        return name + priceAndCalories;
    else
        return name + priceAndCalories + " which is suitable for " + diet;
}

//
public static int getCount() {
    return counter;
}

public static void main(String[] args) {

    System.out.println(

            new drawa("Tomato Pizza", 8.99, Diet.VEGAN));

}

}
主要改变的是tostring和main部分
以上两种方法都是直接给出需要的枚举选项,如果想要输出识别得到的选项,则需要:
m.setDiet(
Diet.valueOf(
keyboard.readString().toUpperCase()));
并且用开关来创建一个枚举方法
public enum Diet {
OMNIVOROUS, VEGAN, VEGETARIAN;
public String toString() {…}
public static Diet called(String s) {
if ( s != null )
switch (s.toUpperCase()) {
case “OMNIVOROUS” :
return OMNIVOROUS;
case “VEGAN” :
return VEGAN;
case “VEGETARIAN” :
return VEGETARIAN;
default : return null;
}
return null;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值