• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用toString()方法打印

用户头像
it1352
帮助1

问题说明

class Box
{
    // Instance Variables
    double length ,ipsos ;
    double width ,mikos ;
    double height ,platos;
    // Constructors
    public Box ( double side )
    {
        width = side ;
        height = side ;
        length = side ;
    }
    public Box ( double x , double y , double z)
    {
        platos = y ;
        ipsos = z;
        mikos = x ;
    }

    // Methods
    double calculate(double praksi)
    {
        return 2 * ( width * height  
        width * length  
        height * length ) ;
    }
    double volume(double emvadon)
    {
        return platos*ipsos*mikos ;
    }

}

在上面的代码中,如何制作toString()方法,所以我可以返回volume的值并计算??? 我是Java的新手,所以请尽可能简单

In the upper code, how can I make a toString() method, so I can return the values of volume and calculate ??? Im new with java so be as simple as you can please

正确答案

#1

将此方法添加到您的类中(以覆盖toString()方法):

Add this method to your class (to override the toString() method):

@Override
public String toString() {
    return "Volume: "   volume(1)   "\n Calculate: "   calculate(1);
}

注意:@Override是可选的:它只是通信,如果错拼了覆盖的方法(toString()),请确保收到错误.

Note: @Override is optional: it's just communication and making sure you receive an error if you misspell the overridden method (toString()).

此外,为什么不使用volumecalculate的参数?这就是为什么我将诸如1之类的随机数传递给他们的原因,因为它们显然无关紧要.

Also, why do you have parameters for volume and calculate if you don't use them? That's why I passed them a random number like 1 because it apparently doesn't matter what they are.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfkakj
系列文章
更多 icon
同类精品
更多 icon
继续加载