简体   繁体   English

创建参数化构造函数

[英]Create a parameterized constructor

So, I'm doing my homework assignment right now.所以,我现在正在做我的家庭作业。 However, I stuck in the several parts of this assignment.然而,我坚持了这个作业的几个部分。

public class CheeseCake {
    //Instance Variables        
    private double Cheese;
    private int StrawBerry;
    private double Cream; 

    public CheeseCake() {
           Cheese = 0;
           StrawBerry = 0;
           Cream = 0;
        }

//Constructor       
    public CheeseCake (double milk, int fresh, double temp)
    {

    Cheese = milk;
   StrawBerry = fresh;
    Cream = temp;

    }



// (setter)

    public void setCheese(double milk){
        Cheese = milk;
    }
    public void setStrawBerry(int fresh){
        StrawBerry = fresh;
    }       
    public void setCream(double temp){
         Cream = temp;
}

//(getter)

public double getCheese(){
    return Cheese;
}

    public int getStrawBerry(){
        return StrawBerry;
}

    public double getCream(){
        return Cream;
}

{

}
//Method to display data, (need to work on this)
public void display() {

System.out.println(Cheese);
System.out.println(StrawBerry);
System.out.println(Cream);
System.out.println("You used " + Cheese + " g of Creamcheese to make Cheese");
System.out.println("You used " + StrawBerry + " StrawBerry to make StrawBerry CheeseCake");
System.out.println("You used " + Cream + " g of Cream to make Cream Cheese");


}
}

And this is my demo which help me to run this program.这是我的演示,它帮助我运行这个程序。

 public class Demo {

    public static void main(String[] args) {

        CheeseCake cheesecake = new CheeseCake();


        cheesecake.setCheese(5);


        cheesecake.setStrawBerry(20);


        cheesecake.setCream(10);

        cheesecake.display();
    }
}

So, my problem is I finish to create 3 instance methods and getter, setter methods.所以,我的问题是我完成了创建 3 个实例方法和 getter、setter 方法。 Also, I create my default constructor.另外,我创建了我的默认构造函数。 But I'm not sure how to create parameterized constructors which this.但我不确定如何创建参数化构造函数。 "Create a parameterized constructor that takes all instance variables as parameters, and sets the instance variables to the values provided by the parameters" “创建一个参数化构造函数,将所有实例变量作为参数,并将实例变量设置为参数提供的值”

Also, I'm not sure what my teacher wants me to do in the demo methods.另外,我不确定我的老师希望我在演示方法中做什么。

Create a class called Demo.java.创建一个名为 Demo.java 的类。 This class will contain your main method Create an instance of your class by using the default constructor.此类将包含您的主要方法,使用默认构造函数创建类的实例。 Call all your objects set methods to assign values to your object Call the objects display method, to print out it's values Create another instance of your class by using the parameterized constructor Call the objects display method, to print out it's values调用所有对象 set 方法为对象赋值 调用对象显示方法,打印出它的值 使用参数化构造函数创建类的另一个实例 调用对象显示方法,打印出它的值

Is my demo methods is right?我的演示方法对吗? or I should add more stuff.或者我应该添加更多的东西。 Also, should I add more constructors to make parameterized constructors?另外,我应该添加更多构造函数来制作参数化构造函数吗? I have no idea how to make parameterized constructors.我不知道如何制作参数化构造函数。

Default constructor doesn't take any argument.默认构造函数不接受任何参数。 Your second constructor is a parameterized constructors.您的第二个构造函数是参数化构造函数。 And your code is okay you just need to create one more parametrized copy constructor which takes another instance as argument.而且您的代码没问题,您只需要创建一个更多参数化的复制构造函数,它将另一个实例作为参数。 ie IE

 public CheeseCake (CheeseCake ck)
    {

    Cheese = ck.getCheese();
    StrawBerry = ck.getStrawBerry();
    Cream = ck.getCream();

    }

And now you need to create an instance in demo class using this constructor现在您需要使用此构造函数在演示类中创建一个实例

CheeseCake cheesecake2 = new CheeseCake(cheesecake);//Pass first object as argument CheeseCake cheesecake2 = new CheeseCake(cheesecake);//传递第一个对象作为参数

Now call display method for 2nd object.现在调用第二个对象的显示方法。



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM