Java 员工、名字、姓氏和 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18456786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Employee, first name, last name and ID
提问by Max Yuan
I just started and it is really confusing because when I write my code there is no red warning on the eclipse, however when I run the program, it does not work. The question is:
我刚刚开始,这真的很混乱,因为当我编写代码时,eclipse 上没有红色警告,但是当我运行程序时,它不起作用。问题是:
Write a program that displays employee IDs and first and last names of employees. Use two classes. The first class contains the employee data and separate methods to set the IDs and names. The other class creates objects for the employees and uses the objects to call the set methods. Create several employees and display their data.
编写一个程序,显示员工 ID 以及员工的名字和姓氏。使用两个类。第一个类包含员工数据和用于设置 ID 和姓名的单独方法。另一个类为员工创建对象并使用这些对象调用 set 方法。创建多个员工并显示他们的数据。
My codes are:
我的代码是:
public class Employee {
String lastName = null;
String firstName = null;
double ID;
public Employee(String lastName, String firstName, double ID){
this.lastName = lastName;
this.firstName = firstName;
this.ID = ID;
}
public String empStat(){
return "Last Name: " + lastName + "First Name: " + firstName + "ID" + ID;
}
}
and
和
public class MainEmployee {
public static void main(String args[]){
Employee nub1 = new Employee ("Griffin", "Peter", 000001);
System.out.println(nub1);
Employee nub2 = new Employee ("Griffin", "Lois", 000002);
System.out.println(nub2);
Employee nub3 = new Employee ("Griffin", "Stewie", 000003);
System.out.println(nub3);
Employee nub4 = new Employee ("Griffin", "Brian", 000004);
System.out.println(nub4);
}
}
and all it does is display
它所做的只是显示
Employee@523ce3f Employee@71b98cbb Employee@4cc68351 Employee@7cd76237
can someone tell me why?
有人能告诉我为什么吗?
回答by Alex
Change
改变
public String empStat()
to
到
@Override
public String toString()
See how toString()
works (and why you see Employee@523ce3f) in docs
在文档中查看toString()
工作原理(以及为什么会看到Employee@523ce3f)
When you use System.out.println(nub1);
the method nub1.toString()
is called implicitly.
当您使用System.out.println(nub1);
该方法时nub1.toString()
被隐式调用。
回答by Ross
One must override the toString() method. This is called any time the object needs to return a string of the object. By default you receive 'Employee@523ce3f' which is an unique internal representation (in string form) of the object.
必须重写 toString() 方法。任何时候对象需要返回对象的字符串时都会调用它。默认情况下,您会收到“Employee@523ce3f”,它是对象的唯一内部表示(以字符串形式)。
Simply creating a method that returns String will not do this.
简单地创建一个返回 String 的方法不会做到这一点。
To override the toString() method change:
要覆盖 toString() 方法更改:
public String empStat()
public String empStat()
to
到
@Override public String toString()
回答by Malwaregeek
You are trying to print objects, it will output hashcode of objects. Printing objects calls toString()
method in Object class, you must override toString method to get state of objects.
您正在尝试打印对象,它将输出对象的哈希码。打印对象调用toString()
Object 类中的方法,必须重写 toString 方法才能获取对象的状态。
public String toString()
{
return firstName+" "+lastName+" "+ ID;
}
Overriding toString
is the easiest approach, consider a list of <Employee>
type. Printing the list will now return the current field values of objects.
覆盖toString
是最简单的方法,考虑一个<Employee>
类型列表。打印列表现在将返回对象的当前字段值。
List<Employee> list= new ArrayList<Employee>();
Employee o= new Employee("Will","Smith",1);
Employee o1= new Employee("Jason","Bourne",2);
list.add(o);
list.add(o1);
for (Employee x:list)
System.out.print(x);
Output:
输出:
[Will Smith 1, Jason Bourne 2]
回答by luoluo
You need a toString
method a
你需要一个toString
方法
public String toString() {
return lastName + " " + firstName + " " + Double.toString(ID);
}
To put it together:
把它放在一起:
class Employee {
String lastName = null;
String firstName = null;
double ID;
public Employee(String lastName, String firstName, double ID){
this.lastName = lastName;
this.firstName = firstName;
this.ID = ID;
}
public String empStat(){
return "Last Name: " + lastName + "First Name: " + firstName + "ID" + ID;
}
public String toString() {
return lastName + " " + firstName + " " + Double.toString(ID);
}
}
public class MainEmployee {
public static void main(String args[]){
Employee nub1 = new Employee ("Griffin", "Peter", 000001);
System.out.println(nub1);
Employee nub2 = new Employee ("Griffin", "Lois", 000002);
System.out.println(nub2);
Employee nub3 = new Employee ("Griffin", "ST", 000003);
System.out.println(nub3);
Employee nub4 = new Employee ("Griffin", "Brian", 000004);
System.out.println(nub4);
}
}
The result is as bellow:
结果如下:
Griffin Peter 1.0
Griffin Lois 2.0
Griffin ST 3.0
Griffin Brian 4.0