Java Product方法重载(上次询问的是同一个人)

Java Product方法重载(上次询问的是同一个人),java,overloading,Java,Overloading,我当时正在研究这个问题,但无法做到这一点,甚至不确定自己是否理解方法重载的真正含义。有人能解释一下是什么(因为我在网上找到的资源不是很好),并可能修复我犯的错误吗 问题是:重载乘积方法以允许将其他类型的值相乘: 两双 一个int和一个double 一个double和一个int 三个整数 三双 我得到的错误是: -从double到int的可能有损转换(第13、16、19行) -产品2/产品3已定义 -方法应为int,int类型,并且给出了int,int,int类型(第17行) 谢谢大家方法重载就是

我当时正在研究这个问题,但无法做到这一点,甚至不确定自己是否理解方法重载的真正含义。有人能解释一下是什么(因为我在网上找到的资源不是很好),并可能修复我犯的错误吗

问题是:重载乘积方法以允许将其他类型的值相乘:

两双 一个int和一个double 一个double和一个int 三个整数 三双

我得到的错误是: -从double到int的可能有损转换(第13、16、19行) -产品2/产品3已定义 -方法应为int,int类型,并且给出了int,int,int类型(第17行)


谢谢大家

方法重载就是当你想用同一个类给方法命名时,但是签名是不同的

当我说签名不同时,参数类型可能不同,例如:

public int multiplyProduct(int one, int two) {}

//We are overloading right here as we already defined a multiplyProduct above

public int multiplyProduct(long one, long two){}
或者可能只是参数的数量不同:

public int multiplyProduct(long one, long two, long three){}
在上面的示例中,您有一个名为Product的类:

class Product {

  public int multiplyProduct(int one, int two) {}

  public int multiplyProduct(int one, long two) {}

  public int multiplyProduct(long one, long two){}

  public int multiplyProduct(long one, long two, long three){}

  public int multiplyProduct(double one, double two) {}

}
因此,正如您现在所做的调用方法:

//To call the multiply(int one, int two)
new Product().multiply(1,1)

//To call the multiply(int one, long two)
new Product().multiply(1,1L)

//To call the multiply(long one, long two, long three)
new Product().multiply(1L,1L,1L)

//To call the multiply(double one, double two)
new Product().multiply(1.0,1.0)
关于此错误,您只需创建一个新方法,该方法接受两个double并返回一个double:

public void run() {

    int intValue = 5;
    double doubleValue = 2.5;

    int product1 = product(intValue, intValue);
    System.out.println(product1);

    // Use method overloading to define methods
    // for each of the following method calls
    double product2 = product(doubleValue, doubleValue);
    System.out.println(product1);

    int product3 = product(intValue * intValue, intValue);
    System.out.println(product1);

    double product4 = product(intValue, doubleValue);
    System.out.println(product1);

    double product5 = product(doubleValue, intValue);
    System.out.println(product1);

    double product6 = product(doubleValue * doubleValue, doubleValue);
    System.out.println(product1);
}

public int product(int one, int two) {
    return one * two;
}

public double product(double one, double two) {
    return one * two;
}

方法重载就是当你想用相同的类给方法命名,但是签名不同。 不同的签名意味着参数的类型不同,或者 参数的顺序不同或参数的数量不同

给定的代码显示了所有类型的方法重载:

    public class Product extends ConsoleProgram {

    public void run(){

        int intValue = 5;
        double doubleValue = 2.5;

        int product1 = product(intValue, intValue);
        System.out.println(product1);

        // Use method overloading to define methods 
        // for each of the following method calls

        double product1 = product(doubleValue, doubleValue);
        System.out.println(product1);

        int product3 = product(intValue, intValue, intValue);
        System.out.println(product3);

        double product2 = product(intValue, doubleValue);
        System.out.println(product2);

        double product2 = product(doubleValue, intValue);
        System.out.println(product2);

        double product3 = product(doubleValue, doubleValue, doubleValue);
        System.out.println(product3);


        public int product(int one, int two)
        {
            return one * two;
        }

        //different types of parameters

        public double product(double one, double two)
        {
            return one * two;
        }

        //different number of parameters

        public int product(int one, int two, int three)
        {
            return one * two * three;
        }

        public double product(int one, double two)
        {
            return one * two;
        }

        //different order of parameters

        public double product(double one, int two)
        {
            return one * two;
        }

        public double product(double one, double two, double three)
        {
            return one * two * three;
        }
    }
    }

谢谢

基本上,如果我想重载int product1=product(intValue,intValue),我必须写int product 1=product(doubleValue,doubleValue)??这样错误意味着您试图将一个double转换为int。精度将丢失/截断,例如:2.1将变为2。我猜乘积(doubleValue,doubleValue)返回一个int,但您试图将其分配给double=product2。确保产品的返回值(doubleValue,doubleValue)为双精度。例如:public double multiplyProduct(double-one,double-two){}我更新了我的示例,你只需要创建一个新的方法,接受两个double并返回一个double。谢谢你,你是最好的。是的,@Ian,刚才非常感谢你,我很欣赏itokay,所以我现在知道了什么是方法重载,但是我就是找不到我的编码有什么问题。。你能给我举个例子吗?谢谢好的,请看下面的代码:double Bill(int item,int price){double total=item*price;return total;}double Bill(int item,double price){double total=item*price;return total;}/*我们通过更改参数*/double Bill(int项,双价,双税){double total=item*price*tax;return total;}/*我们通过更改参数*/double Bill的顺序来重载Bill函数(双倍价格,双倍税,整数项){double total=item*price*tax;return total;}哦,好的,谢谢
    public class Product extends ConsoleProgram {

    public void run(){

        int intValue = 5;
        double doubleValue = 2.5;

        int product1 = product(intValue, intValue);
        System.out.println(product1);

        // Use method overloading to define methods 
        // for each of the following method calls

        double product1 = product(doubleValue, doubleValue);
        System.out.println(product1);

        int product3 = product(intValue, intValue, intValue);
        System.out.println(product3);

        double product2 = product(intValue, doubleValue);
        System.out.println(product2);

        double product2 = product(doubleValue, intValue);
        System.out.println(product2);

        double product3 = product(doubleValue, doubleValue, doubleValue);
        System.out.println(product3);


        public int product(int one, int two)
        {
            return one * two;
        }

        //different types of parameters

        public double product(double one, double two)
        {
            return one * two;
        }

        //different number of parameters

        public int product(int one, int two, int three)
        {
            return one * two * three;
        }

        public double product(int one, double two)
        {
            return one * two;
        }

        //different order of parameters

        public double product(double one, int two)
        {
            return one * two;
        }

        public double product(double one, double two, double three)
        {
            return one * two * three;
        }
    }
    }