一长串参数

时间:2019-02-08 04:07:49

标签: java constructor parameter-passing

我在弄清楚如何组织构造函数时遇到了麻烦。

构造函数是否应该采用所有变量?

我通过调用构造函数并向其传递firstNamepizzaSizeInInchespizzaTypecheeseToppingpepperoniTopping中的值来创建PizzaOrder的实例,sausageToppingonionToppingmushroomTopping

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 ";
    }

3 个答案:

答案 0 :(得分:1)

一般的经验法则是,构造函数应接受那些没有意义的参数。例如没有引擎,汽车就没用了。可以拥有或添加更多功能的所有其他属性都可以保留为属性。

除此之外,您还可以创建一个类的层次结构,每个类都有其自己的构造函数,该构造函数检测必须强制初始化哪些属性。 例如。基类被声明为Pizza,它在构造函数中接受size作为参数。然后,您可以拥有从Pizza类派生的Cheez披萨,该类接受用于构造Cheez披萨的强制性参数。

答案 1 :(得分:0)

基本上,您可以考虑三个结构及其组合。
第一个当然是带有一长串变量的构造函数:

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; 
}

通过链接setters

可以更方便地调用pizza.setCheeseTopping("Melt").setPepperoniTopping("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 Pattern

public class Pizza {

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

    public Pizza(PizzaBuilder builder) {
        this.firstName = builder.firstName;
        this.pizzaType = builder.pizzaType;
        this.pizzaSizeInInches = builder.pizzaSizeInInches;
        this.cheeseTopping = builder.cheeseTopping;
        this.pepperoniTopping = builder.pepperoniTopping;
        this.sausageTopping = builder.sausageTopping;
    }

    String getFirstName() { return firstName;}

    String getPizzaType() { return pizzaType;}

    String getCheeseTopping() { return cheeseTopping;   }

    String getPepperoniTopping() {return pepperoniTopping;  }

    String getSausageTopping() { return sausageTopping; }

    int getPizzaSizeInInches() {return pizzaSizeInInches; }

    static class PizzaBuilder {

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

        PizzaBuilder setFirstName(String firstName) {
            this.firstName = firstName;
            return this;
        }

        PizzaBuilder setPizzaType(String pizzaType) {   
            this.pizzaType = pizzaType; 
            return this;
        }

        PizzaBuilder setPizzaSizeInInches(int pizzaSizeInInches) {  
            this.pizzaSizeInInches = pizzaSizeInInches; 
            return this;
        }

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

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

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

        Pizza buid (){
            return new Pizza(this);
        }

    }

    //make new Pizza 
    public static void main(String[] args) {
        Pizza pizza = new PizzaBuilder().
                            setFirstName("Jack").setPizzaType("Annanas").
                            setPizzaSizeInInches(10).setCheeseTopping("Melt").
                            setPepperoniTopping("Hot").setSausageTopping("None").
                            buid();
    }
}

答案 2 :(得分:0)

对于这样的问题时刻,我们应该设计一些设计模式,在这里fragment_profile最适合如下。希望对您有帮助

包装item2;

Builder Pattern