重写:子类对父类方法的重写编写(方法名,参数类型,个数,返回值相同)。
重载:当前类中相同方法名,不用参数类型或个数的多个方法。
静态分派:发生在编译期,在编译器决定调用哪个具体的方法,直接查看字节码就知道调用的方法是哪个。
动态分派:发生在运行期,在运行期决定调用哪个具体的方法。
例子一:重载
public class TestOverLoad {
static class Phone{
}
static class Huawei extends Phone{
}
public void print(Phone phone){
System.out.println("phone");
}
public void print(Huawei huawei){
System.out.println("huawei");
}
public static void main(String[] args) {
Phone phone = new Phone();
Phone huawei = new Huawei();
TestOverLoad testOverLoad = new TestOverLoad();
testOverLoad.print(phone);//1.1,phone
testOverLoad.print(huawei);//1.2,phone
huawei = (Huawei)huawei;
testOverLoad.print(huawei);//1.3,phone
testOverLoad.print((Huawei)huawei);//1.4,phone
}
}
0: new #6 // class com/test/jvm/TestOverLoad$Phone
3: dup
4: invokespecial #7 // Method com/test/jvm/TestOverLoad$Phone."<init>":()V
7:<