静态方法重载错误
Static method Overloading Error
我假设我的重载(或覆盖)有问题。每次我用我创建的 driver class 调用时,它都是 returns ("Cannot make a static reference to the non-static method duty(int) from the type Doctor")。通过医生调用时如何使用值班方法object.
医生Class
public class Doctor extends Employee {
private int patientsSeen;
public Doctor(int patientsSeen) {
super(150000,45);
this.patientsSeen = patientsSeen;
}
public void duty(int patientsSeen) {
if(patientsSeen < 0) {
System.out.println("Another Patient goes home happy!");
this.patientsSeen = patientsSeen;
System.out.println(patientsSeen + " patients sent home happy!");
}
else
System.out.println("Patients are waiting, get back to work!");
}
public String toString() {
super.toString();
return patientsSeen + " ";
}
}
Driver
public class Driver {
public static void main(String[] args) {
Employee doctor = new Doctor(0);
Employee surgeon = new Surgeon(0);
Janitor janitor = new Janitor(0);
Janitor.duty(0);
Doctor.duty(0);
}
}
您正在尝试调用 class 上的方法,而不是实例。
应该是:
janitor.duty(0);
doctor.duty(0);
您正在尝试将非静态方法 'duty(int)' 作为静态方法调用。将其更改为:
janitor.duty(0);
doctor.duty(0);
并且代码应该可以工作。您必须从 'Doctor' 的实例(或首次实现 'duty' 的超类)或其子类之一调用它们。
Janitor 是您 class 的名字。
janitor 是您的 class Janitor.
实例的名称
当您尝试调用特定于您的实例的方法时,您需要引用您的实例管理员而不是您的 class 管理员.
您要找的是:
janitor.duty(0);
doctor.duty(0);
您是否一直在使用:
Janitor.duty(0);
Doctor.duty(0);
你会这样称呼:
public static void duty(int patientsSeen) { ... }
所有给定的答案还有一件更重要的事情。从您的代码看来,它既没有覆盖也没有超载。现在注意几点:
如果您 Employee
class 有一个 non static
和 非私有 duty(int )
方法然后在子 class Doctor
它将是 duty(int )
方法
的覆盖版本
如果您的 Employee
class 有静态 duty(int ) 方法,那么 duty(int)
方法不能继承给它的子方法 class Doctor
因此不能被覆盖。
现在考虑你 Employee
class 没有 duty(int )
方法。但是在您的 Doctor
class 处,您有两个具有这些签名的 duty()
方法 - duty(int )
和 duty(float )
。然后在 OOP
中说 duty()
方法已为 int
和 float
重载
我假设我的重载(或覆盖)有问题。每次我用我创建的 driver class 调用时,它都是 returns ("Cannot make a static reference to the non-static method duty(int) from the type Doctor")。通过医生调用时如何使用值班方法object.
医生Class
public class Doctor extends Employee {
private int patientsSeen;
public Doctor(int patientsSeen) {
super(150000,45);
this.patientsSeen = patientsSeen;
}
public void duty(int patientsSeen) {
if(patientsSeen < 0) {
System.out.println("Another Patient goes home happy!");
this.patientsSeen = patientsSeen;
System.out.println(patientsSeen + " patients sent home happy!");
}
else
System.out.println("Patients are waiting, get back to work!");
}
public String toString() {
super.toString();
return patientsSeen + " ";
}
}
Driver
public class Driver {
public static void main(String[] args) {
Employee doctor = new Doctor(0);
Employee surgeon = new Surgeon(0);
Janitor janitor = new Janitor(0);
Janitor.duty(0);
Doctor.duty(0);
}
}
您正在尝试调用 class 上的方法,而不是实例。 应该是:
janitor.duty(0);
doctor.duty(0);
您正在尝试将非静态方法 'duty(int)' 作为静态方法调用。将其更改为:
janitor.duty(0);
doctor.duty(0);
并且代码应该可以工作。您必须从 'Doctor' 的实例(或首次实现 'duty' 的超类)或其子类之一调用它们。
Janitor 是您 class 的名字。 janitor 是您的 class Janitor.
实例的名称当您尝试调用特定于您的实例的方法时,您需要引用您的实例管理员而不是您的 class 管理员.
您要找的是:
janitor.duty(0);
doctor.duty(0);
您是否一直在使用:
Janitor.duty(0);
Doctor.duty(0);
你会这样称呼:
public static void duty(int patientsSeen) { ... }
所有给定的答案还有一件更重要的事情。从您的代码看来,它既没有覆盖也没有超载。现在注意几点:
如果您
Employee
class 有一个non static
和 非私有duty(int )
方法然后在子 classDoctor
它将是duty(int )
方法 的覆盖版本
如果您的
Employee
class 有静态 duty(int ) 方法,那么duty(int)
方法不能继承给它的子方法 classDoctor
因此不能被覆盖。现在考虑你
Employee
class 没有duty(int )
方法。但是在您的Doctor
class 处,您有两个具有这些签名的duty()
方法 -duty(int )
和duty(float )
。然后在OOP
中说duty()
方法已为int
和float
重载