简体   繁体   English

如何从该对象打印? 爪哇

[英]How can I print from this object? Java

I need to print the first name, last name, and salary from two employee objects but I keep getting a cannot find symbol error. 我需要从两个员工对象中打印名字,姓氏和薪水,但我不断遇到找不到符号的错误。 What would I do to fix this? 我将如何解决此问题?

Here is the constructor class: 这是构造函数类:

public class Employee
    {
        private String firstName;
        private String lastName;
        private double monthlySalary;

        public Employee( String firstName1, String lastName1, double monthlySalary1) {
            setfirstName(firstName1);
            setlastName(lastName1);
            setmonthlySalary(monthlySalary1);
        }

        String getfirstName() {
            return firstName;
        }

        String getlastName() {
            return lastName;
        }

        double getmonthlySalary() {
            return monthlySalary;
        }

        public void setfirstName (String firstName1) {
            firstName = firstName1;
        }

        public void setlastName (String lastName1) {
            lastName = lastName1;
        }

        public void setmonthlySalary (double monthlySalary1) {
            monthlySalary = ( monthlySalary1 >= 0 ? monthlySalary1 : 0);
        }
    }

And here is what I have so far to print the objects: 到目前为止,这是我要打印的对象:

public class EmployeeTest {

      public static void main(String[] args) {
            Employee a = new Employee("John", "Smith", 10000);
            Employee b = new Employee("Jane", "Smith", 11000);
            System.out.print(a.firstName1);
         }
   }

I need to be able to have it print out something along the lines of "Name: Salary:" But I am clueless as to how to make this work. 我需要能够按照“名称:薪水:”的格式打印出一些内容,但是我对如何使它工作一无所知。 Any help would be greatly appreciated. 任何帮助将不胜感激。

In your employee class, you need to override the toString() method. 在您的员工类中,您需要重写toString()方法。

You can try something like: 您可以尝试如下操作:

@Override
public String toString()
{
   System.out.println("Name: "+name+"Salary: "+salary);
}

Then for each of your employees, when you want to print them, just call 然后,对于每个员工,当您要打印它们时,只需致电

System.out.println(employee);

You cant print out firstName (or firstName1 , because that doesnt exist in your class), because its marked as private. 您不能打印出firstName (或firstName1 ,因为在您的课程中不存在),因为它被标记为私有。 You should do something like this: 您应该执行以下操作:

System.out.print(a.getfirstName())

firstName is private , which means that it cannot be seen outside of the object/class it resides in. I suggest you try overriding the toString() method on your Employee class. firstNameprivate ,这意味着它不能在它所驻留的对象/类的外部看到。我建议您尝试覆盖Employee类上的toString()方法。 That method would have access to all the private members of Employee . 该方法将可以访问Employee所有私有成员。

Alternately, you could use getfirstName() to return the first name. 或者,您可以使用getfirstName()返回名字。

Also, this may be a typo, but there is no firstName1 in Employee - it is firstName . 另外,这可能是一个错字,但是Employee中没有firstName1它是firstName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM