产品方法超载(这是上次询问的同一个人)
我正在研究这个问题,不能这样做,并不确定是否我甚至不明白什么方法超载真的是。任何人都可以解释它是什么(因为我在网上找到的资源不是很好),并且可能修复我所犯的错误?产品方法超载(这是上次询问的同一个人)
的问题是:重载产品的方法,以允许其他类型的值相乘:
两个双打 一个int和双 一个双键和一个int 3整数 三项双打
我收到错误说: - 可能有损从双倍转换为int(第13,16,19) - 产品2 /产品3已被定义 - 方法期望类型int,int和类型int,int,int (第17行)
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 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;
}
}
谢谢你们
方法重载只是当你想命名方法相同withing同一类,但签名是不同的。
当我说的签名不同,它可能是,参数类型是不同的,例如:
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){}
在你上面的示例中您有一个类叫做产品:
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) {}
}
所以当你现在打电话给我thods你这样做:
//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)
有关错误的,你只需要创建一个接受两个双的新方法,并返回一个双:
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;
}
}
}
谢谢
没关系,所以我现在明白什么是重载的方法,但我无法找到问题是与我的编码..你能给我一个例子吗?谢谢 – CodingBat
行,所以看下面的代码: –
双比尔(int item,int price){ double total = item * price;总回报: ; } double Bill(int item,double price){ double total = item * price;总回报: ; } /*我们通过改变参数的*/ 双比尔(INT项,双价,双税)的数量{ 双总=项重载比尔功能 *价格*税;总回报: ; } –
所以基本上,如果我想重载INT产品1 =产品(的intValue,的intValue); ,我将不得不写int product 1 = product(doubleValue,doubleValue)?? – CodingBat
那种,我会编辑我的例子,以便通过一个例子来清楚! – Ian
谢谢,你能解释一下“从double到int的可能的有损转换”吗? double product2 = product(doubleValue,doubleValue);这是我写的代码,它向我展示了这个错误消息。 – CodingBat