gpt4 book ai didi

java - 我如何从Java对象获取信息?我可以进一步简化我的代码吗?

转载 作者:行者123 更新时间:2023-12-01 08:52:01 24 4
gpt4 key购买 nike

我被分配了一个项目来创建一个类,并使用方法而不编辑“EmployeeTester”类。我如何从对象 harry 获取名称而不是设置 name ="harry"?我还可以进一步简化我的代码吗?

public class EmployeeTester
{
/**
* main() method
*/
public static void main(String[] args)
{
Employee harry = new Employee("Hacker, Harry", 50000.0);**\\
harry.raiseSalary(25.0); // Harry gets a 25 percent raise!

System.out.println(harry.getName() + " now makes " +
harry.getSalary());
}

}



public class Employee
{
// instance variables
private double salary;
private String name;
/**
* Constructor for objects of class Employee
*/
public Employee(String employeeName, double currentSalary)
{
//Initializes instance variables
salary = currentSalary;
name = employeeName;
}
public String getName()
{
//Sets name to harry
name = "Harry";
return name;
}
public void raiseSalary(double byPercent)
{
//multiplies by 1.25 percent to get the 25% raise
salary = salary *(1 + byPercent/100);
return;
}
public double getSalary()
{
//returns the salary
return salary;
}
}

最佳答案

可以是这样的

class Employee
{
private double salary;
private String name;

public Employee(String titleName, double salary) {
this.name = titleName.split(",")[1];
this.salary =salary;
}

public void raiseSalary(double byPercent) {
salary = salary *(1 + byPercent/100);
}

public String getName() {
return name;
}

public double getSalary() {
return salary;
}
}

关于java - 我如何从Java对象获取信息?我可以进一步简化我的代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327435/

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