gpt4 book ai didi

Java:对于UML图中仅存在于父类(super class)中的抽象方法应该做什么?

转载 作者:行者123 更新时间:2023-12-01 22:47:48 28 4
gpt4 key购买 nike

我有下面的 UML 图:Class Diagram我读过抽象方法应该由子类覆盖。当任一子类中都没有 calculateMonthlyPay() 时,我应该如何重写 calculateMonthlyPay() ?我可以创建未在 UML 图中显示的方法吗?

这是我的 Employee 及其子类 PartTimeEmpFullTimeEmp 的代码,供引用。

public abstract class Employee {
Scanner keyboard = new Scanner(System.in);
public String name;
public String ID;

//Employee constructor
public Employee(String name, String ID) {
this.name = name;
this.ID = ID;
}

//ID setter
public void setID(String ID) {
ID = keyboard.nextLine();
this.ID = ID;
}

//name getter
public String getName() {
return name;
}

public abstract String getStatus();

public abstract double calculateMonthlyPay();

@Override
public String toString() {
return "Employee{" + "name=" + name + ", ID=" + ID + '}';
}
}

class PartTimeEmp extends Employee {
public double hourlyRate;
public int hoursPerWeek;
//PartTimeEmp constructor
public PartTimeEmp(String name, String ID){
super(name, ID);
}

public void setHours(int hoursPerWeek) {
this.hoursPerWeek = hoursPerWeek;
}

public void setRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}

@Override
public String toString() {
return "PartTimeEmp{" + "hourlyRate=" + hourlyRate + ", hoursPerWeek=" + hoursPerWeek + '}';
}
}

class FullTimeEmp extends Employee {
double salary;

//FullTimeEmp constructor
public FullTimeEmp(String name, String ID) {
super(name, ID);
}

//Set salary of employee
public void setSalary(double salary) {
this.salary = salary;
}

@Override
public String toString() {
return "FullTimeEmp{" + "salary=" + salary + '}';
}
}

最佳答案

UML 图仅在首先引入该方法的类中显示每个方法。否则,每个类的 UML 将显示其层次结构中所有祖先类的所有方法,这不是很方便。

因此,您绝对应该在子类中重写 calculateMonthlyPay()getStatus(),否则您将无法编译代码。

关于Java:对于UML图中仅存在于父类(super class)中的抽象方法应该做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25107048/

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