When using overloading with type promotion, why method calling is ambiguous?

前端 未结 5 1456
一个人的身影
一个人的身影 2021-01-05 08:26
public class aman {
    void m(double a , int b, int c) {
        System.out.println(\"second\");
    }
    void m(float a , int b, double c) {
        System.out.pr         


        
5条回答
  •  -上瘾入骨i
    2021-01-05 08:56

    here , method will be ambiguous becs you are filling all parameters as integer values, then the compiler will confuse (for automatic type cast). thus you need to define something like this suffix for your code:

    public class aman {
        void m(double a , int b, int c) {
            System.out.println("second");
        }
        void m(float a , int b, double c) {
            System.out.println("first");
        }
        public static void main(String[] args) {
            aman obj = new aman();
    
            obj.m(20d, 30, 40);//for calling first method 
            obj.m(23f, 12, 1d);//for calling second method.
        }
    }
    

提交回复
热议问题