[英]My method from another class, will not call an input in my Main class
我正在自学 Java,并且正在尝试运行我编写的一段代码。 我正处于 Udemy 自学课程中,我们正在谈论课程。 我在我的 IDE IntelliJ 中做了一个项目,叫做 Person。 本质上,目标是将名字和姓氏作为字符串输入,将年龄作为数字输入,然后运行并吐出人的全名,如果他们是青少年。
但是,我处于输入阶段,并且在我的主类中不断收到错误消息。 但是,当我尝试输入 Dan (person.SetFirstName("Dan")) 时,它返回一个错误,提示“setFirstName() in person 不能应用于 java.lang.String”
下面你会找到我的主类代码,以及我的 person 类的前几行,我觉得如果我能用第一种方法找出问题,我就可以解决其余的问题。
我在我的课堂论坛上问过,但我似乎无法得到答案,有些人认为这可能是我的 IDE 本身的问题?
公共课主要{
public static void main(String[] args) {
Person person = new Person();
person.setFirstName("Dan");
person.setLastName();
person.setAge();
System.out.println("Full Name = " + person.getFullName());
System.out.println("Are they a teenager? " + person.isTeen());
person.setFirstName();
person.setAge();
System.out.println("Full Name = " + person.getFullName());
System.out.println("Are they a teenager? " +person.isTeen());
person.setLastName();
System.out.println("Full Name = " + person.getFullName());
}
}
公共类人{
private String firstName;
private String lastName;
private int age;
public String getFirstName(){
return this.firstName;
}
public void setFirstName(){
this.firstName = firstName;
}
public void setFirstName()
没有定义任何参数,但是您将字符串参数传递给它( person.setFirstName("Dan");
) - 因此是错误。
尝试将方法签名更改为:
public void setFirstName(String firstName){
this.firstName = firstName;
}
改变这个
public void setFirstName(){
this.firstName = firstName;
}
到
public String setFirstName(){
this.firstName = firstName;
return firstName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.