在主类的类中使用getter和setter方法

时间:2018-11-01 18:02:50

标签: java

我必须编写一个程序,该程序编写一个名为“ Employee”的类,其中包含有关雇员姓名和工资的信息。然后,我需要在主类中使用该信息来设置员工信息,然后获取员工信息并将其打印到屏幕上,但是我不知道如何在“ Employee”类中访问我的信息以在“ main”中使用”类。这是我的“员工”课程中的内容。

package classwork6_1;

public class Employee {

    private String name;
    private float salary;

    public String getName()  {        
        return name;        
    }

    public float getSalary() {          
        return salary;     
    }

    public void setName(String name){
     this.name = name;          
    }

    public void setSalary(float salary){
        this.salary = salary;        
    }     
}

4 个答案:

答案 0 :(得分:2)

在您的主类中,实例化Employee,然后调用设置器:

Employee emp = new Employee();
emp.setName("Matt");
emp.setSalary(50000);

然后使用吸气剂将其打印出来:

System.out.println("Name: " + emp.getName() + ", salary: " + emp.getSalary());

答案 1 :(得分:2)

只需执行以下简单步骤:

创建Employee实例后

Employee employee = new Employee();

您要使用setters设置值。例如:

employee.setName("John");
employee.setSalary(100f);

此后,您可以使用getters返回值。例如:

String name = employee.getName();
float salary = employee.getSalary();

通过添加构造函数来改进您的类!

此外,我将在课堂上加入一个constructor。例如:

public Employee(String name, float salary) {
    this.name = name;
    this.salary = salary;
}

现在您可以执行以下操作来创建您的类的实例:

Employee employee = new Employee("John", 100f);

答案 2 :(得分:2)

Main类应该是这样的

import classwork6_1.*;

public class Main {
    public static void main(String[] args) {
        Employee  e=new Employee();
        e.setSalary(2000f);
        e.setName("Gagan");
        System.out.println("Name: " + e.getName() + " Salary : " + e.getSalary());
    }
}

答案 3 :(得分:1)

  

如何在“雇员”类中访问我的信息以在“主要”类中使用。

  1. 实例化Employee对象。
  2. 在该对象上调用方法。

这是您的课堂的完整示例。请参阅public static void main方法,以了解上述步骤1和2如何工作。此页面上的其他正确答案也显示了这一点。

在实际工作中,在准确性至关重要的情况下,切勿使用float / Floatdouble / Double来赚钱。 Floating-point技术trades away accuracy用于提高执行速度。 BigDecimal类是相反的,缓慢但准确。

// Example app for Answer in Stack Overflow: https://stackoverflow.com/a/53107000/642706
// Simple example, not meant for use in production.
// Caution: *Not* thread-safe.
public class Employee {

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Employee {
    private String name;
    private BigDecimal salary;

    public Employee ( String name , BigDecimal salary ) {
        this.setName( name );
        this.setSalary( salary );
    }

    public String getName ( ) {
        return name;
    }

    public void setName ( String name ) {
        // … add your data validation checks here. Example: Strings that are too short or too long.
        this.name = name;
    }

    public BigDecimal getSalary ( ) {
        return salary;
    }

    public void setSalary ( BigDecimal salary ) {
        // … add your data validation checks here. Example: Numbers that are negative, too big, or too small.
        salary.setScale( 2 , RoundingMode.HALF_EVEN ); // Round to the penny, using Banker’s Rounding.
        this.salary = salary;
    }

    public void giveRaise ( int percentagePoints ) {
        BigDecimal percentageAsDecimalFraction = new BigDecimal( percentagePoints ).divide( new BigDecimal( 100 ) );
        BigDecimal mulitiplier = new BigDecimal( "1" ).add( percentageAsDecimalFraction );
        BigDecimal newSalary = this.getSalary().multiply( mulitiplier ).setScale( 2 , RoundingMode.HALF_EVEN );  // Round to the penny, using Banker’s Rounding.
        this.setSalary( newSalary );
    }

    @Override
    public String toString ( ) {
        return "Employee{ " +
                "name='" + this.getName() + '\'' +
                " | salary=" + this.getSalary() +  // In real work we would *not* be dumping sensitive private data like salary that might end up in logs or other insecure data-sinks. 
                " }";
    }

    public static void main ( String[] args ) {
        Employee x = new Employee( "Alice" , new BigDecimal( "1234.56" ) );
        Employee y = new Employee( "Bob" , new BigDecimal( "678.12" ) );

        System.out.println( x );
        System.out.println( y );

        x.giveRaise( 10 );
        y.giveRaise( 5 );

        System.out.println( "After raises." );
        System.out.println( x );
        System.out.println( y );
    }
}

运行时。

  

Employee {name ='Alice'|工资= 1234.56}

     

Employee {name ='Bob'|工资= 678.12}

     

加薪后。

     

Employee {name ='Alice'|工资= 1358.02}

     

Employee {name ='Bob'|工资= 712.03}