【发布时间】:2016-04-04 11:53:14
【问题描述】:
我提供如下两个类:
家电类
public class Appliance {
void start(Appliance t){
System.out.println("Start Appliance");
}
}
烤面包机班p>
public class Toaster extends Appliance {
void start(Toaster t){
System.out.println("Start Toaster");
}
}
RunAppliance 类
public class RunAppliance {
public static void main(String[] args) {
Appliance appliance = new Toaster();
Toaster toaster = new Toaster();
appliance.start(toaster);
}
}
作为一个新手,它让我在方法重载和覆盖之间感到困惑,以及参数如何在一种参数类型中受到影响是另一种参数类型的子类。因此,我提出了 6 个相同的条件:
1)Appliance Class : void start(Appliance t) ; 烤面包机类 : void start(Appliance t)
2)Appliance Class : void start(Toaster t) ; Toaster 类 : void start(Toaster t)
3)Appliance Class : void start(Appliance t) ; Toaster 类 : void start(Toaster t)
4)Appliance Class : void start(Toaster t) ; 烤面包机类 : void start(Appliance t))
5)Appliance Class : void start(Appliance t) & void start(Toaster t) ; Toaster 类 : void start(Toaster t)
6)Appliance Class : void start(Appliance t) ; Toaster Class : void start(Appliance t) & void start(Toaster t))
谁能给我建议一个必要的规则。
【问题讨论】:
-
你到底在问什么?这“6 个条件”是什么?
-
什么是必要规则?
-
这些是具有不同参数类型的同一方法的不同版本,粗体是在上面的代码部分中调用它们的类名。
-
必要规则是指在什么基础上可以预测输出以及如何发生重载和覆盖
标签: java inheritance polymorphism overloading overriding