Overload rewritable (Override) function and a function (Overload)

The main class

/*
函数的重写(Override)和重载(Overload)
    重写:
        函数的重写是面向对象中子类继承父类,父类中的函数不适用于子类中的函数,所以在子类中对父类中
        的方法进行重写,重写方法时父类与子类的方法名相同,参数类型相同,参数个数相同,不同的就是方
        法所实现的功能不同,即方法的内容不同。可以理解为重新写这个方法。
    重载:
        函数的重载是在同一个类中针对不同类型的参数而对方法进行重载,重载时函数的参数列表不同。
*/
class MethodOverrideOverload{
    public static void main(String[] args){
        //Override
        Old method=new New();
        method.write();
        //Overload
        Overload(1);
        Overload(1,2);
    }
    public static void Overload(int number){
        System.out.println("我是第 "+number+" 个方法");
    }
    public static void Overload(int number1,int number2){
        System.out.println("我是第 "+number2+" 个方法");
    }
}

father

class Old{
    public void write(){
        System.out.println("我是原函数");
    }
}

Heavy-duty class

public class New extends Old{
    @Override
    public void write(){
        System.out.println("我是重写函数");
    }
}

operation result

我是重写函数
我是第 1 个方法
我是第 2 个方法

analysis

For rewriting, the compiler checks Old function class there is a compile-time write, compile. New running a new New objects, assign method, the virtual machine to know is to call the write method New, that is to know which method call at runtime
for heavy load in terms of compiler to call a function based on different arguments

Published 19 original articles · won praise 18 · views 513

Guess you like

Origin blog.csdn.net/weixin_45887924/article/details/104536377