Method overloading

class MyClass{
    int height;
    MyClass(){
        System.out.println("无参数构造函数");
        height=4;
    }
    MyClass(int i){
        System.out.println("房子高度为"+i+"米");
        height=i;
    }
    void info(){
        System.out.println("房子高度为"+height+"米");
    }
    void info(String s){
        System.out.println(s+":房子高度为"+height+"米");
    }
}

public class method_overload {
    public static void main(String[] args) {
        MyClass t=new MyClass(3);
        t.info();
        t.info("重载方法");
        //重载构造函数
        new MyClass();
    }
}

The output result is:

The house height is 3M, and the house height is 3M. Overload method: the house height is 3M, and there is no parameter constructor

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>