如何将参数值传递给实例变量?

时间:2013-12-04 01:54:11

标签: java class variables parameters instance

以下是我已经放弃的课程项目的说明。

构造函数应该调用String类的相应set方法,将作为参数传递的值赋值给实例变量size和toppings

我有一个Pizza类的构造函数,但即使我使用size = this.size,也不会保留传递给它的大小和顶部的参数值。我不明白老师指的是哪种方法。我所需要的只是概念性的理解,我将能够完成这个项目。

编辑:要清楚,我知道我的代码现在给出了一个NPE,因为我的变量toppings在toString()中为null,所以我试图将Pizza()更改的toppings传递给toString( )

public class Pizza {

private char size;
private String[] toppings;
private int status;
public final static int NOT_STARTED = 0;
public final static int IN_PROGRESS = 1;
public final static int READY = 2;

public static void main(String[] args) {
    String[] testArray = new String[]{"poop", "pee"};
    Pizza newpizza2 = new Pizza('L', testArray);
    System.out.println(newpizza2.toString());
}

//Boolean method with a char parameter that checks the size and sets the char of the pizza size (S,M,L) and returns true. 
public boolean setSize(char size2) {
    if (size2 == 'S'||size2 == 'M'||size2 == 'L') {
        size = size2;
        return true;
    }
    else {
        size = 'M';
        return false;
    }
}

//Boolean method with an int parameter that checks the status and sets the status of the pizza readiness.
public boolean setStatus(int status2) {
    if (status2 >= NOT_STARTED && status2 <= READY) {
        status = status2;
        return true;
    }
    else {
        status = NOT_STARTED;
        return false;
    }
}

//Void method with a String array parameter that only sets the toppings from user input and returns no value. 
public void setToppings(String[] toppings) {
    toppings = this.toppings;
}

//A Get method for setSize to return the value of size.
public char getSize() {
    return this.size;
}

//A Get method for setStatus to return the value of status.
public int getStatus() {
    return this.status;
}
//A Get method for setToppings to return the value of toppings[].
public String[] getToppings() {
    return this.toppings;
}

//Int method that checks the number of toppings and returns the amount of toppings or 0 if there are no toppings given.
public int numToppings() {
    if (toppings != null) {
        return toppings.length;
    }
    else {
        return 0;
    }
}

//Method that calculates the price of the pizza based on the instance variables and returns the price.
public double calcPrice() {
    if (size == 'S') {
        double price = 8;
        double toppingPrice = toppings.length;
        double total = price + toppingPrice;
        return total;
    }
    else if (size =='M') {
        double price = 9;
        double toppingPrice = 0;
        if (toppings != null) {
            toppingPrice = toppings.length * 1.5;
        }
        double total = price + toppingPrice;
        return total;
    }
    else if (size =='L') {
        double price = 10;
        double toppingPrice = toppings.length * 2;
        double total = price + toppingPrice;
        return total;
    }
    else {
        return 0;
    }
}
/********************************/
//No argument Constructor//
public Pizza() {
    size = 'M';
    status = NOT_STARTED;
    toppings = null; 
}
public Pizza(char size, String[] toppings) {
    size = this.size;
    System.out.println(toppings.length);
    status = NOT_STARTED;
}
public String statusPhrase() {
    if (status == NOT_STARTED) {
        return "Not Started";
    }
    else if (status == IN_PROGRESS) {
        return "In Progress";
    }
    else {
        return "Ready";
    }
}
public String toString() {
    String combo, combo2 = "", combo3;
    Pizza newpizza = new Pizza();
    newpizza.setSize(size);
    newpizza.setToppings(toppings);
    //Print out the method's return values
    if (toppings.length == 0) {
        combo = "Pizza size " + newpizza.getSize() + ". No toppings.";
    }
    else {
        combo = "Pizza size " + newpizza.getSize() + ". Toppings: ";
    }
    //List the toppings in number order using a for loop
    for (int i = 0; i<=newpizza.numToppings()-1; i++){
        combo2 += "\n" + (i+1)+ ". " + newpizza.getToppings()[i];
    }
    combo3 = "\n"+newpizza.statusPhrase();
    return combo + combo2 + combo3;
}

}

2 个答案:

答案 0 :(得分:0)

这是你当前的构造函数

public Pizza(char size, String[] toppings) {
    size = this.size;
    System.out.println(toppings.length);
    status = NOT_STARTED;
}

您的构造函数应该如下所示

public Pizza(char size, String[] toppings) {
    this.size = size;
    this.toppings = toppings
    System.out.println(toppings.length);
    status = NOT_STARTED;
}

答案 1 :(得分:0)

这很简单:

private char someChar;
public void setSomeChar (char someChar) {
    this.someChar = someCHar; // 'this' refers to THIS instance.
}

同样重要的是要知道左侧网站上的内容是值传递给的内容(请原谅我的英文)。 当你写:

private char someChar;
public void setSomeChar (char someChar) {
    someChar = this.someCHar; //
}

您将实例变量的值传递给本地方法参数(它们巧合地具有相同的名称)。

更普遍地说:

private char instanceVariable;

public void someMethod (char someLocalParameter) {
    /* local means that it is only known inside this method. That's why everything 
     *  you assign to it just disappears after executing the method.
     */   

     someLocalParameter = this.instanceVariable // nothing happens

     this.instanceVariable = someLocalParameter // will do the job
}