英文:
Java calling an object without using parameters
问题
我刚开始学习编程。我已经创建了这个构造函数:
public Attraction(int baseprice, String name) {
this.baseprice = baseprice;
this.name = name;
}
我在另一个类中初始化了这个构造函数:
Attraction attraction = new Attraction(ridename, price);
我希望在第三个类中使用这些数据,而无需传入参数,所以代码看起来会是这样的:
Attraction attraction = new Attraction();
这将允许我使用getter和setter来更改景点对象中的现有数据。然而,目前这是不可能的,因为我必须传入名称和价格,即使我想使用现有数据。
有人能告诉我该怎么做吗?任何帮助将不胜感激。
英文:
I am new to programming. I have made this constructor:
public Attraction(int baseprice, String name) {
this.baseprice = baseprice;
this.name = name;
}
I have initialized the constructor in a different class:
Attraction attraction = new Attraction(ridename, price)
I would like to use this data in a third class without having to pass in parameters, so it would look like this:
Attraction attraction = new Attraction()
This would then allow me to use getters and setters to change the existing data in the attraction object. However, this is not possible at the moment because I have to pass in the name and price even though I want to use the existing data.
Can anyone show me what to do? Any help would be appreciated.
答案1
得分: 3
你似乎误解了Java的工作原理。
关键字 new
被选择并不是随机的:每当你写 new Attraction([不论这里放什么])
,一个全新的对象就会被创建(这个对象就是 Attraction 的一个实例)。每个单独的对象都有它自己的一组字段。也没有什么“统治它们所有的实例” - 没有办法只是说:“给我上次创建的那个实例”或者“给我那个实例;我永远不希望有多个存在”。
你需要传递引用:
Attraction a = new Attraction(ridename, price);
... 做一些操作
someOtherMethod(a); // 通过参数传递 'a',或者...
return a; // 通过返回值传递 'a',或者...
this.someFieldOfTypeAttaction = a; // 通过赋值给字段传递 'a'。
有很多方法可以实现。
你也可以编写“给我上次创建的那个景点”甚至“永远只会有一个景点”的代码,但在这里听起来不太对:毫无疑问你计划拥有许多不同的景点,通常情况下,全局可变状态会导致难以调试的混乱代码,并且容易出错。
英文:
You seem to misunderstand how java works.
The new
name of the keyword to make new objects wasn't chosen at random: Every time you write new Attraction([doesn't matter what goes here])
, a well, new object is made (this object is then 'an instance' of Attraction). Each individual object has its own set of the fields. There is also no 'one instance to rule them all' - there is no way to just go: "Give me the instance I created last" or "Give me the one instance; I never want more than one to exist".
You need to pass the reference around:
Attraction a = new Attraction(ridename, price);
... do stuff
someOtherMethod(a); // pass 'a' around via a parameter, or...
return a; // pass 'a' around by returning it, or...
this.someFieldOfTypeAttaction = a; // pass 'a' around by assigning it to a field.
many ways to do it.
You can also program 'give me the attraction I created last' or even 'there will only ever be a single attraction', but this doesn't sound right here: Surely you are planning to have many different attractions, and in general having global mutable state leads to spaghetti code that is hard to debug and which is prone to failure.
答案2
得分: 0
我假设你有一个类如下所示:
public static class Attraction {
private String ride;
private int price;
public Attraction(String ride, int price) {
this.ride = ride;
this.price = price;
}
public String getRide() {
return ride;
}
public void setRide(String ride) {
this.ride = ride;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
因此,要创建Attraction
类,您需要传递两个参数。如果您想创建一个没有数据的对象,然后稍后想要更新它,您可以使用默认构造函数,然后稍后可以更新对象的值,如下所示:
public static class Attraction {
private String ride;
private int price;
public Attraction(String ride, int price) {
this.ride = ride;
this.price = price;
}
// 这是一个默认构造函数
public Attraction(){
this("", 0); // 传递对象的默认值。
}
public String getRide() {
return ride;
}
public void setRide(String ride) {
this.ride = ride;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
稍后,您可以创建具有或不具有传递参数的Attraction
对象。
创建没有传递参数的Attraction
对象:
Attraction attraction = new Attraction(); // 将创建带有空字符串和0的对象
attraction.setRide("Your ride"); // 设置ride
attraction.setPrice(1233); // 设置price
attraction.getRide(); // 获取ride的值
attraction.getPrice(); // 获取ride的价格
或者您可以使用参数数据创建对象:
Attraction attraction = new Attraction("myRide", 20); // 将使用传递的值创建对象。
attraction.setRide("Your ride"); // 设置ride
attraction.setPrice(1233); // 设置price
attraction.getRide(); // 获取ride的值
attraction.getPrice(); // 获取ride的价格
英文:
I assume you have a class like below
public static class Attraction {
private String ride;
private int price;
public Attraction(String ride, int price) {
this.ride = ride;
this.price = price;
}
public String getRide() {
return ride;
}
public void setRide(String ride) {
this.ride = ride;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
So to create the class Attraction
you need to pass two arguments. if you want to create an object without data and later you want to update it, you can make use of default constructor and later you can update the values of the object. like below
public static class Attraction {
private String ride;
private int price;
public Attraction(String ride, int price) {
this.ride = ride;
this.price = price;
}
// this is a default construtor
public Attraction(){
this("", 0); // pass a default values for the object.
}
public String getRide() {
return ride;
}
public void setRide(String ride) {
this.ride = ride;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
later you can create object of Attraction
with or without passing argument parameters.
Attraction attraction = new Attraction() //will create the object with emptyString and 0
attraction.setRide("Your ride"); //sets ride
attraction.setPrice(1233); // sets price
attraction.getRide(); //gives the value of ride
attraction.getPrice(); // gives the price of the ride
Or You can create with argument data.
Attraction attraction = new Attraction("myRide", 20) //will create the object with value passed.
attraction.setRide("Your ride"); //sets ride
attraction.setPrice(1233); // sets price
attraction.getRide(); //gives the value of ride
attraction.getPrice(); // gives the price of the ride
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论