gpt4 book ai didi

java - 尝试测试Person类,不成功。

转载 作者:行者123 更新时间:2023-12-01 06:50:55 25 4
gpt4 key购买 nike

好的,所以我应该创建 Person 类,其中包含用于保存人员姓名、地址和电话号码的字段,稍后将通过 Customer 类对其进行扩展。

    public class Person
{
//Instance Variables
private String name;
private String street;
private String cityStateZip;
private String phone;

//Constructors
public Person()
{
String name = "";
String street = "";
String cityStateZip = "";
String phone = "";
}
public Person(String name, String street, String cityStateZip,
String phone)
{
this.name = name;
this.street = street;
this.cityStateZip = cityStateZip;
this.phone = phone;
}
public Person(Person person)
{
new Person();
}

//Mutators
public void setName(String name)
{
this.name = name;
}
public void setStreet(String street)
{
this.street = street;
}
public void setCityStateZip(String cityStateZip)
{
this.cityStateZip = cityStateZip;
}
public void setPhone(String phone)
{
this.phone = phone;
}

//Accessors
public String getName()
{

return name;
}
public String getStreet()
{

return street;
}
public String getCityStateZip()
{

return cityStateZip;
}
public String getPhone()
{

return phone;
}
public String toString()
{

return name + "/n" + street + "/n" + cityStateZip + "/n" + phone;
}
}

好吧,所以...我更改了一些代码,修复了我该死的私有(private)/公共(public)错误...我永远把+/-搞混了...这个文件现在可以编译了。我正在尝试创建一个类来测试它,但遇到了一些问题。这是我目前遇到的错误

----jGRASP 执行:javac -g TestPerson.java

TestPerson.java:23: error: cannot find symbol
name = keyboard.nextLine;
^
symbol: variable nextLine
location: variable keyboard of type Scanner
TestPerson.java:27: error: cannot find symbol
street = keyboard.nextLine;
^
symbol: variable nextLine
location: variable keyboard of type Scanner
TestPerson.java:31: error: cannot find symbol
cityStateZip = keyboard.nextLine;
^
symbol: variable nextLine
location: variable keyboard of type Scanner
TestPerson.java:35: error: cannot find symbol
phone = keyboard.nextLine;
^
symbol: variable nextLine
location: variable keyboard of type Scanner
TestPerson.java:38: error: non-static method getName() cannot be referenced from a static context
getName()+ "\n" + getStreet() + "\n" + getCityStateZip() + "\n"
^
TestPerson.java:38: error: non-static method getStreet() cannot be referenced from a static context
getName()+ "\n" + getStreet() + "\n" + getCityStateZip() + "\n"
^
TestPerson.java:38: error: non-static method getCityStateZip() cannot be referenced from a static context
getName()+ "\n" + getStreet() + "\n" + getCityStateZip() + "\n"
^
TestPerson.java:39: error: non-static method getPhone() cannot be referenced from a static context
+ getPhone());
^
8 errors



import java.util.Scanner;
import java.io.*;

public class TestPerson extends Person
{


public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

String name;
System.out.println("Enter your name: ");
name = keyboard.nextLine;

String street;
System.out.println("Enter your street: ");
street = keyboard.nextLine;

String cityStateZip;
System.out.println("Enter your city, state and zip code: ");
cityStateZip = keyboard.nextLine;

String phone;
System.out.println("Enter your phone number: ");
phone = keyboard.nextLine;

System.out.println("Tested person's information is as follows: " +
getName()+ "\n" + getStreet() + "\n" + getCityStateZip() + "\n"
+ getPhone());
System.out.println(new Person());

System.exit(0);
}
}

最佳答案

您收到的确切错误是因为您采用了以前的公共(public)方法 (toString) 并将其设为私有(private)方法。

您的 Person 类的类型为 Object,因为 Java 中的所有类都继承自 Object。任何获得 Object 传递的人都希望它有一个可以使用的 toString 方法。如果 toString 是私有(private)的,则它们无法执行此操作,因此 Java 会提示。

顺便说一句,toString 中的代码看起来也是错误的。

关于java - 尝试测试Person类,不成功。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29839255/

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