Constructor parameter with int and long or double and float throwing error

huangapple go评论83阅读模式
英文:

Constructor parameter with int and long or double and float throwing error

问题

  1. 这个问题通常被称为"构造函数重载歧义"。

  2. 解决这个问题的方法是在调用构造函数时提供明确的参数类型,以消除歧义。你可以通过显式地将参数类型进行类型转换,或者修改调用以确保只匹配一个构造函数。

英文:

I am trying to implement constructor overloading by using int and long together and double and float together. I am passing two int values from main method and want to check which constructor gets called A(int, long) or A(long, int). I am passing two double values from main method and want to check which construcotr gets called A(double, float) or A(float, double).

In Line5, I am passing values to the constructors in Line1 and Line2 and in Line6, I am passing values to the constructors in Line3 and Line4. While compiling and executing this code, constructor is ambiguous is showing for Line5 and constructor is undefined is showing for Line6.

CODE:

import java.util.*;

public class Test {
    
    public Test(long i, int f, double d) { //Line1
        System.out.println("L i d");
    }
    
    public Test(int i, long d, double f) { //Line2 
        System.out.println("i L f");
    }
    
    public Test(int i, float f, double d) { //Line3
        System.out.println("i f d");
    }
    
    public Test(int i, double d, float f) { //Line4
        System.out.println("i d f");
    }
    
    public static void main(String[] args) {
        Test ob = new Test(1, 2, 4.14); //showing compilation error //Line5
        Test ob1 = new Test(1, 2.33, 4.14); //showing compilation error //Line6
    }
}

OUTPUT:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The constructor Test(long, int, double) is ambiguous
    The constructor Test(int, double, double) is undefined

Please tell me:

  1. Is there any name that represents this problem
  2. What is the solution to this problem

答案1

得分: 3

你需要在浮点数后面加上"f",在长整数后面加上"l",这样Java才知道你实际想要的构造类型。它无法确定2.33是应该是double还是float,因此在这种情况下你需要明确指定。

这是正确的写法:

Test ob = new Test(1l, 2, 4.14); //已修复 //第5行
Test ob1 = new Test(1, 2.33f, 4.14); //已修复 //第6行
英文:

You need to put an "f" behind a float and an "l" behind a long, so that java knows what type constructor you actually want. It does not know, whether 2.33 is supposed to be a double or a float. Therefore you need to specify it in this case.

This is correct:

Test ob = new Test(1l, 2, 4.14); //fixed //Line5
Test ob1 = new Test(1, 2.33f, 4.14); //fixed //Line6

答案2

得分: 1

以下是已经翻译好的内容:

没有任何后缀的数字被假定为整数。如果它是一个小数,编译器会假定它是一个 double,所以你告诉编译器的是你想调用构造函数 Test(int, int, double),但这个构造函数并不存在。

现在,如果你只有一个构造函数,例如 Test(long, f, double),编译器会自动为你进行类型转换。然而,由于你有多个构造函数,可能存在多个转换的可能性,编译器无法确定选择哪一个,因此会出现错误消息。

现在你必须告诉编译器你的数字实际是哪种数据类型。

使用 L 作为后缀表示 long,使用 F 表示 float,使用 D 表示 double。

因此,例如,你可以调用:

  • new Test(1L, 2, 4.14)
  • new Test(1, 2F, 4.14)
  • new Test(1, 2D, 4.14F)
  • ...

查看原始数据类型文档以获取更多信息。

英文:

Numbers without any postfix are assumed to be integers. If it's a decimal number the compiler assumes it's a double so what you tell the compiler is you want to call the constructor Test(int, int, double) which does not exist.

Now if you were to have only one constructor e.g. Test(long, f, double) the compiler would automatically do the type casts for you. However, since you have many constructors, multiple conversions are possible and the compiler does not know which one to pick, hence the error message.

You now have to tell it which data types your numbers actually are.

Use L as postfix to denote a long, use F to denote a float and D to denote double.

So e.g. you could call:

  • new Test(1L, 2, 4.14) or
  • new Test(1, 2F, 4.14) or
  • new Test(1, 2D, 4.14F)
  • ...

> Have a look at the Docs for Primitive data types.

huangapple
  • 本文由 发表于 2023年5月7日 04:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定