Java 一长串论点

Java 一长串论点,java,constructor,parameter-passing,Java,Constructor,Parameter Passing,我搞不清楚如何组织构造器 构造函数应该接受所有变量吗 我通过调用构造函数创建了一个PizzaOrder实例,并将firstName、pizzaSizeInInches、pizzaType、cheeseTopping、pepperoniTopping、香肠顶、洋葱顶、蘑菇顶中的值传递给它 public static double calculatePizzaCost() { double cost = 12.99; //cost of the pizza

我搞不清楚如何组织构造器

构造函数应该接受所有变量吗

我通过调用构造函数创建了一个PizzaOrder实例,并将
firstName
pizzaSizeInInches
pizzaType
cheeseTopping
pepperoniTopping
香肠顶
洋葱顶
蘑菇顶
中的值传递给它

public static double calculatePizzaCost() {

    double cost = 12.99;            //cost of the pizza     

    String toppings = "";          //list of toppings     
    int numberOfToppings = 0;      //number of toppings
    String crust = "";
    final double TOPPING_PRICE = 1.25;

    if (aPizzaOrder.getHandThinDeep() == 'h' || aPizzaOrder.getHandThinDeep() == 'H') {
        crust = "Hand-Tossed";
    } else if (aPizzaOrder.getHandThinDeep() == 't' || aPizzaOrder.getHandThinDeep() == 'T') {
        crust = "Thin-Crust";
    } else if (aPizzaOrder.getHandThinDeep() == 'd' || aPizzaOrder.getHandThinDeep() == 'D') {
        crust = "Deep-Pan";
    }

    if (aPizzaOrder.getCheeseTopping()) {
        numberOfToppings += 1;
        toppings = toppings + "Additional Cheese ";
    }

一般的经验法则是,构造函数应该接受那些参数,如果没有这些参数,对象就没有意义。例如,汽车没有发动机是无用的。所有其他属性都可以保留为属性,这些属性很适合拥有或添加更多功能

除此之外,还可以创建类的层次结构,每个类都有自己的构造函数来检测哪些属性必须强制初始化。
例如,基类被声明为Pizza,它接受大小作为构造函数中的参数。然后你可以从比萨饼类中得到Cheez比萨饼,它接受强制性的参数来构造Cheez比萨。

< P>基本上你可以考虑三种结构,以及它们的组合。br/> 第一个当然是一个包含一长串变量的构造函数:

public class Pizza {

    private String firstName, pizzaType;
    private String cheeseTopping, pepperoniTopping, sausageTopping;
    private int pizzaSizeInInches;

    public Pizza(String firstName, String pizzaType, String cheeseTopping, String pepperoniTopping,
            String sausageTopping, int pizzaSizeInInches) {
        this.firstName = firstName;
        this.pizzaType = pizzaType;
        this.cheeseTopping = cheeseTopping;
        this.pepperoniTopping = pepperoniTopping;
        this.sausageTopping = sausageTopping;
        this.pizzaSizeInInches = pizzaSizeInInches;
    }
}
第二种基本结构是对长字段列表使用
setter
方法:

public class Pizza {

    private String firstName, pizzaType;
    private String cheeseTopping, pepperoniTopping, sausageTopping;
    private int pizzaSizeInInches;

    public Pizza() {};

    String getFirstName() { return firstName;}

    void setFirstName(String firstName) {this.firstName = firstName;}

    String getPizzaType() { return pizzaType;   }

    void setPizzaType(String pizzaType) {   this.pizzaType = pizzaType; }

    String getCheeseTopping() { return cheeseTopping;   }

    void setCheeseTopping(String cheeseTopping) {   this.cheeseTopping = cheeseTopping; }

    String getPepperoniTopping() {return pepperoniTopping;  }

    void setPepperoniTopping(String pepperoniTopping) { this.pepperoniTopping = pepperoniTopping;   }

    String getSausageTopping() { return sausageTopping; }

    void setSausageTopping(String sausageTopping) { this.sausageTopping = sausageTopping;}

    int getPizzaSizeInInches() {return pizzaSizeInInches; }

    void setPizzaSizeInInches(int pizzaSizeInInches) {  this.pizzaSizeInInches = pizzaSizeInInches; }
}
旁注:让
setter
方法返回
this
,例如:

Pizza setCheeseTopping(String cheeseTopping) {  
    this.cheeseTopping = cheeseTopping; 
    return this;
}

Pizza setPepperoniTopping(String pepperoniTopping) {    
        this.pepperoniTopping = pepperoniTopping;
        return this; 
}
通过链接,可以更方便地调用长列表的
setter
pizza.setcheesetting(“Melt”).setpeppernonitting(“Hot”)

上述两种方法的合理组合是让构造函数初始化基本字段,并对可选字段或具有默认值的字段使用
setters

public class Pizza {

    private String firstName, pizzaType;
    private String cheeseTopping, pepperoniTopping, sausageTopping;
    private int pizzaSizeInInches;

    public Pizza(String firstName, String pizzaType) {
        this.firstName = firstName;
        this.pizzaType = pizzaType;
    }

    String getFirstName() { return firstName;}

    String getPizzaType() { return pizzaType;}

    String getCheeseTopping() { return cheeseTopping;   }

    Pizza setCheeseTopping(String cheeseTopping) {  
        this.cheeseTopping = cheeseTopping; 
        return this;
    }

    String getPepperoniTopping() {return pepperoniTopping;  }

    Pizza setPepperoniTopping(String pepperoniTopping) {    
        this.pepperoniTopping = pepperoniTopping;
        return this; 
    }

    String getSausageTopping() { return sausageTopping; }

    Pizza setSausageTopping(String sausageTopping) {
        this.sausageTopping = sausageTopping;
        return this;
    }

    int getPizzaSizeInInches() {return pizzaSizeInInches; }

    Pizza setPizzaSizeInInches(int pizzaSizeInInches) { 
        this.pizzaSizeInInches = pizzaSizeInInches; 
        return this;
    }
} 
初始化长变量列表的第三种方法是使用:


对于这样的问题,我们应该考虑一些设计模式,这里
Builder模式
最适合如下。我希望这对你有帮助

一揽子项目2

public class PizzaOrder {

    private String firstName;
    private int pizzaSizeInInches;    
    //add other fields

    private PizzaOrder(Builder builder){
        this.firstName=builder.firstName;
        this.pizzaSizeInInches=builder.pizzaSizeInInches;
        //set other fields
    }

    public static class Builder{
        private String firstName;
        private int pizzaSizeInInches;
        //add other fields

        public Builder(){
        }

        public Builder firstName(String firstName){
            this.firstName=firstName;
            return this;
        }

        public Builder pizzaSizeInInches(int pizzaSizeInInches) {
            this.pizzaSizeInInches = pizzaSizeInInches;
            return this;
        }

        //add other setter method like above

        public PizzaOrder build(){
            return new PizzaOrder(this);
        }          
    }

    public static Builder builder(){
        return new Builder();
    }

    public static void main(String args[]){
        PizzaOrder.builder()
                .firstName("sanjeev")
                .pizzaSizeInInches(10)
                .build();
    }
}

IMHO-
PizzaOrder
不需要通过构造函数传递的所有信息。您应该只关注在其功能上下文中传递使对象“稳定”或“有效”的信息。为了制作比萨饼,你必须具备哪些要素?在这种情况下,我可能会考虑使用“Builder模式”,因为这允许您在“过渡”状态中有一个对象,并且当您准备好“构建”最终版本时。
public class PizzaOrder {

    private String firstName;
    private int pizzaSizeInInches;    
    //add other fields

    private PizzaOrder(Builder builder){
        this.firstName=builder.firstName;
        this.pizzaSizeInInches=builder.pizzaSizeInInches;
        //set other fields
    }

    public static class Builder{
        private String firstName;
        private int pizzaSizeInInches;
        //add other fields

        public Builder(){
        }

        public Builder firstName(String firstName){
            this.firstName=firstName;
            return this;
        }

        public Builder pizzaSizeInInches(int pizzaSizeInInches) {
            this.pizzaSizeInInches = pizzaSizeInInches;
            return this;
        }

        //add other setter method like above

        public PizzaOrder build(){
            return new PizzaOrder(this);
        }          
    }

    public static Builder builder(){
        return new Builder();
    }

    public static void main(String args[]){
        PizzaOrder.builder()
                .firstName("sanjeev")
                .pizzaSizeInInches(10)
                .build();
    }
}