我一直在玩弄几个小时,改变静态,私人,公共等等:)但它仍然无法运作。如果我在一个地方改变静态,我会在另一个地方出现错误等。
我有一个叫做人的课。我使用了非静态Setter,因为Person()
构造函数也是非静态的。
public class Person {
private String name;
private String lastname;
private String nickname;
Person() {
this.name = "";
this.lastname = "";
this.nickname = "";
}
public void setName(String name) {
this.name = name;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
然后我有一个带有main方法的文件,以及与用户交互的不同方法。这个方法也是静态的,因为它调用带有使用Scanner
类的userInput的方法。
public class Interaction {
public static void takeName() {
String name;
String lastname;
String nickname;
System.out.println("What is your firstname:");
name = userInput(); // calls method with Scanner class
System.out.println("What is your lastname:");
lastname = userInput(); // calls method with Scanner class
System.out.println("What is your nickname:");
nickname = userInput();
person.setName(name);
person.setLastname(lastname);
person.setNickname(nickname);
}
//editor: missing closing bracket
我尝试了什么:
Person.person.setname(name);
Interaction
中声明String,然后使用this.name传递String并从公共类Interaction
Person(
课程中的构造函数类Person
)。 我在这里缺少什么?
编辑:我已根据您的要求添加了更多信息:)
如果传递if语句,我的新Person
对象将被声明。
如果有可用的地方,则会创建一个新人并将其添加到此地点。
public class Theater {
void reservationSystem () {
if (availability > 0) {
for (int i = 0; i < freespaces.length; i++) {
if (freespaces[i].person == null) {
freespaces[i].person = new Person();
break;
}
}
} else {
System.out.println("No tickets for you today :) ");
}
}
//editor: missing closing bracket
所以我的思维方式是:
Userinput()
类使用Scanner
中的数据填充构造函数; Person
对象,以便它拥有该数据!Person
时,构造函数中的数据将填充数据AGAIN但现在有新数据:)如果您需要更多信息,请告诉我们:)
答案 0 :(得分:2)
首先要注意的是,您的Person
构造函数有点无用,您可以重写Person
:
public class Person {
private String name = "";
private String lastname = "";
private String nickname = "";
public void setName(String name) {
this.name = name;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
现在进入Interaction
class
。这需要public
而不是Public
,但我认为这是一个错字。
您需要instance
Person
来调用您的二传手,因为他们是instance
方法。你需要到某个地方拨打new Person()
。
编写takeName()
方法的最简单方法是在方法中创建Person
并return
实例:
public class Interaction {
public static Person takeName() {
final Person person = new Person();
System.out.println("What is your firstname:");
person.setName(userInput());
System.out.println("What is your lastname:");
person.setLastname(userInput());
System.out.println("What is your nickname:");
person.setNickname(userInput());
return person;
}
}
答案 1 :(得分:1)
你的'takeName()`函数应该更新Person类的单个实例。
一种方法是在外部创建Person
并将其传递给函数:
Public class Interaction {
public static void takeName(Person person) {
String name;
String lastname;
String nickname;
System.out.println("What is your firstname:");
name = userInput(); // calls method with Scanner class
System.out.println("What is your lastname:");
lastname = userInput(); // calls method with Scanner class
System.out.println("What is your nickname:");
nickname = userInput();
person.setName(name);
person.setLastname(lastname);
person.setNickname(nickname);
}
}
但我认为在函数中创建person实例并返回它会更直观:
Public class Interaction {
public static Person takeName() {
String name;
String lastname;
String nickname;
Person person = new Person();
System.out.println("What is your firstname:");
name = userInput(); // calls method with Scanner class
System.out.println("What is your lastname:");
lastname = userInput(); // calls method with Scanner class
System.out.println("What is your nickname:");
nickname = userInput();
person.setName(name);
person.setLastname(lastname);
person.setNickname(nickname);
return person;
}
}
答案 2 :(得分:1)
因为这不是有效的语法:Person.person.setname(name);
在这种情况下,这意味着:
Person
Person
的{{1}}类的静态字段 person
setname
醇>
但是您的name
类 - 恰当 - 没有名为person的静态字段......
你的问题的根本原因很可能不完全熟悉类,实例和连接的概念,静态和实例成员和方法的含义......