【问题标题】:Java code works, but unsure if I have fulfilled the requirements (objects and classes)Java 代码有效,但不确定我是否满足要求(对象和类)
【发布时间】:2016-06-19 16:00:38
【问题描述】:

我有这个作业要求,我已经为它写了一个解决方案。但不确定我是否得到了正确的答案。 代码工作并编译。

Question

这是我的 java 文件(2 个文件)

首先是主文件

public class Employee
{
String name;
int idNumber;
String department;
String position;

public Employee(String emName, int idNum, String depart, String pos)
{
    name = emName;
    idNumber = idNum;
    department= depart;
    position = pos;
}

public Employee(String emName, int idNum)
{
    name = emName;
    idNumber = idNum;
    department= "";
    position = "";
}

public Employee()
{
    name = "";
    idNumber = 0;
    department= "";
    position = "";
}

//set name
public void setName(String nameSet)
{
    name = nameSet;
}

//get name
public String getName()
{
    String nameGet = name;
    return nameGet;
}

//set idnum
public void setID(int idSet)
{
    idNumber = idSet;
}

//get idnum
public int getId()
{
    int idGet = idNumber;
    return idGet;
}

//
//set department
public void setDep(String depSet)
{
    department = depSet;
}

//get department
public String getDep()
{
    String depGet = department;
    return depGet;
}

//
//set position
public void setPos(String posSet)
{
    position = posSet;
}

//get position
public String getPos()
{
    String posGet = position;
    return posGet;
}

}

第二次添加数据并打印数据

       public class Employee_test
{
public static void main(String[] args)
{
    Employee employee1 = new Employee("Susan Meyers", 47899, "Accounting", "Vice President");
    Employee employee2 = new Employee("Mark Jones", 39119, "IT", "Programmer"); //use second with Mark
    //Employee employee2 = new Employee("Mark Jones", 39119);
    Employee employee3 = new Employee("Joy Rogers", 81774, "Manufacturing", "Engineer"); //use third with Joy
    //Employee employee3 = new Employee();

    System.out.println("Name           ID Number   Department     Position");
    System.out.println("");
    System.out.println(employee1.getName() + "   " + employee1.getId() +  "       " + employee1.getDep() + "     " + employee1.getPos());
    System.out.println(employee2.getName() + "     " + employee2.getId() +  "       " + employee2.getDep() + "             " + employee2.getPos());
    System.out.println(employee3.getName() + "     " + employee3.getId() +  "       " + employee3.getDep() + "  " + employee3.getPos());
    System.out.println("");
}

}

这是我运行 employee_test 文件时的输出。

Output terminal

我对什么感到困惑。

问题说

Make sure you use all three constructors. Use the first constructor with Susan, the second with Mark and the third one with Joy.

如果问题希望第二个员工使用只有(姓名,身份证号)的构造函数,而第三个员工什么都没有(),如何实现输出。 提前致谢。

【问题讨论】:

  • 调用构造函数后使用setName、setID等函数。它们的存在是有原因的,而这可能就是任务的目的
  • 基本上,他们希望您将 setter 方法与第二和第三员工对象一起使用
  • @JacobH - 所以我应该使用这两个构造函数。然后使用setName、setID、setDep、setPos来达到效果?
  • @Yellow_13 - 刷新浏览器 - Bohemian 在下面给出的答案

标签: java class object methods bluej


【解决方案1】:

使用setter填充其他数据:

Employee employee1 = new Employee("Susan Meyers", 47899, "Accounting", "Vice President");

Employee employee2 = new Employee("Mark Jones", 39119);
employee2.setDep("IT");
employee2.setPos("Programmer");

Employee employee3 = new Employee();
employee3.setName("Joy Rogers");
employee3.setID(81774);
employee3.setDep("Manufacturing");
employee3.setPos("Engineer");

请注意,从设计方面来看,所有字段都应为private,ID 也应为final,因为一旦创建员工,ID 就不会更改。如果此人更改姓名(婚姻、证人保护等),name 字段可能会更改,而其他字段可能会不时更改。

【讨论】:

  • 好的,我已经根据您的建议更改了代码。 - 但是如何将“ID”设置为 final?
  • @Yellow_13 private 是字段修饰符,例如 private int ID,但如果这样做,则必须在构建时提供 ID 值,因此您要么必须删除 noargs 构造函数或在该构造函数中生成一个 ID。
  • 我将employee.java 中的所有参数都更改为“private”——程序仍然有效。对于最终的 ID,我应该为每个员工创建 3 ID 变量吗? (例如“private final int idNumber1;”)?
  • @yellow 没有。 对于本练习,不要将字段设为私有 - 它会过多地改变问题。让员工类保持原样。我只是说在现实世界中它们应该是私有的。
  • 啊,我明白了。感谢您的建议。
猜你喜欢
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 2022-07-02
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
相关资源
最近更新 更多