[英]How do I create an object from a specific class and change its attributes by using methods in java
public class Forklift{
double height;
Forklift fl_1, fl_2;
Forklift(){
fl_1 = new Forklift();
fl_2 = new Forklift();
}
void raise(){
height += 10;
}
void lower(){
height -= 10;
}
}
I'm currently using BlueJ.我目前正在使用 BlueJ。 I want to create two objects with the attribute 'height' and I want to use the methods raise() and lower() to change the value of that attribute.
我想创建两个具有属性“高度”的对象,并且我想使用方法 raise() 和 lower() 来更改该属性的值。 Can someone please help me?
有人可以帮帮我吗? I don't know why it's not working
我不知道为什么它不起作用
Are you trying to do something like this?你想做这样的事情吗?
public class Forklift{
double height;
Forklift(){
height=0;
}
void raise(){
height += 10;
}
void lower(){
height -= 10;
}
}
I dont understand why you are creating a forklift in the forklift constructor.我不明白你为什么要在叉车构造函数中创建叉车。 That just gives you an infinite loop.
那只会给你一个无限循环。
Edit: If you want to create two instances of the class, just use:编辑:如果要创建 class 的两个实例,只需使用:
Forklift forklift1 = new Forklift();
Forklift forklift2 = new Forklift();
somewhere outside the class. Now you have two instances of the class. class 之外的某处。现在您有 class 的两个实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.