Java Constructor Overloading

前端 未结 5 500
慢半拍i
慢半拍i 2021-01-29 16:13

I\'m new with Java and I\'m having trouble understanding the constructor issue, I have looked at many tutorials and still I\'m having difficult to understand why we use construc

5条回答
  •  无人及你
    2021-01-29 16:54

    Yes a constructor has the same name as the Class.

    As long as the constructors have different signatures you can have as many as you want. The signature is what distinguishes one constructor from another...

    public MyClass()
    {
    }
    
    public MyClass(int a)
    {
    }
    
    public MyClass(int a, int b)
    {
    }
    
    public MyClass(String a)
    {
    }
    
    public MyClass(int a, String b)
    {
    }
    

    Those are all different because they have different signatures.

提交回复
热议问题