好的,所以我应该创建Person类,其中包含用于保存人名,地址和电话号的字段,稍后将由客户类扩展。
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;
}
}
好吧,所以...我改变了一些代码,修复了我的私人/公共错误......我永远得到了+/-混合....这个文件现在编译。我试图创建一个类来测试它,并且我遇到了一些问题。这是我目前遇到的错误
---- jGRASP exec: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);
}
}
答案 0 :(得分:4)
您获得的确切错误是因为您正在采用以前的公共方法(toString
)并将其作为私有方法。
您的Person
类的类型为Object
,因为Java中的所有类都继承自Object
。任何通过Object
的人都希望它拥有可以使用的toString
方法。如果toString
是私有的,则他们无法执行此操作,因此Java会抱怨。
另外,toString中的代码也看起来不对。
答案 1 :(得分:2)
private String toString()
您正在覆盖Object's toString
方法,其限制条件比public
更严格,但不遵守method overriding
中java
的规则。
应该是
public String toString()
答案 2 :(得分:2)
If you want to access your person class and its details in other class
you should not make person class constructor as private .if so you
can't create a object of person class in other tester classes.
try it as public.
Private constructor can be used for **Static factory method()** inside
the class itself and also some utility based classes and also for static
method(which is for no object creation).
If you make toString() as private, for this a protected
instance method in the superclass can be made public, but not private,
in the subclass.
This because inheritance creates an IS-A relation between two classes,
for which the Liskov substitution principle must be valid. Without
having the previous constraint that would be impossible to enforce.
答案 3 :(得分:1)
toString()
需要公开,因为其他方法(例如println()
)需要访问它。
一般来说,你需要考虑什么应该是私人的,什么应该公开。你已经将一切都变得私密,即使那些没有意义(构造者)。你的吸气剂和制定者也应该是公开的;这是吸气剂和制定者的重点。