gpt4 book ai didi

java - 设置者,为什么这不起作用?

转载 作者:行者123 更新时间:2023-12-01 18:42:52 24 4
gpt4 key购买 nike

我已经玩了好几个小时了,改变静态、私有(private)、公共(public)等等:)但它仍然不起作用。如果我在一个地方更改静态,我会在另一个地方收到错误等。

我有一个名为 person 的类。我使用了非静态 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;
}
}

然后,我有一个包含主方法的文件,以及与用户交互的不同方法。此方法也是静态的,因为它调用采用使用 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);
  • 在公共(public)类Interaction中声明该字符串,然后使用this.name传递该字符串并调用公共(public)类Interaction中的方法
  • 尝试更改静态、私有(private)等。
  • 删除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

所以我的思路是:

  • 我使用 Scanner 类使用来自 Userinput() 的数据填充构造函数;
  • 然后我创建新的 Person 对象,以便它具有该数据!
  • 当我在预订系统中创建一个新的 Person 时,构造函数中的数据将再次填充数据,但现在填充新数据:)

如果您需要更多信息,请告诉我:)

最佳答案

首先要注意的是,你的 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;
}
}

现在进入您的交互 。这需要通过 public 而不是 Public 但我认为这是一个拼写错误。

您需要有一个 Person实例来调用您的setter,因为它们是实例方法。您需要在某个地方调用new Person()

编写 takeName() 方法的最简单方法是在方法中创建一个 Person 返回实例:

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;
}
}

关于java - 设置者,为什么这不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19176134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com