产品方法重载 [英] Product Method Overloading

查看:63
本文介绍了产品方法重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在CodeHS上解决这个问题,然后我被困了很长时间,所以决定在这里提问.

so I was working on this problem on CodeHS, then I was stuck for so long so decided to ask here.

练习是重载乘积方法,以允许将其他类型的值相乘:

The exercise is to overload the product method to allow for multiplying together other types of values:

  1. 两个双打
  2. int和double
  3. 双精度和整数
  4. 三个整数
  5. 三双

  1. two doubles
  2. an int and a double
  3. a double and an int
  4. three ints
  5. three doubles

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(product2);

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

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

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

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

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

谢谢

推荐答案

请注意,重载意味着方法的名称和返回类型与原始方法/初始方法相同.

be aware that overloading means, the name of the method and the return type remains the same as the original/ initial method.

然后您需要定义/实现那些方法>

then you need to define/ implement those methods>

2个双打将是:

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

一个int和一个double将是

an int and a double would be

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

这篇关于产品方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆