使用toString()方法打印
问题说明
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
正确答案
将此方法添加到您的类中(以覆盖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()
).
此外,为什么不使用volume
和calculate
的参数?这就是为什么我将诸如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
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20