目录
题目:
代码:
结果:
题目:
定义一个盒子类Box
包括3个私有变量(width,length,height),一个构造方法和showBox()方法。
构造方法用来初始化变量,showBox()方法无参数,用于输出变量(width,length和height)
的值。
代码:
步骤
1.首先定义一个公共类Box
2.在类中定义三个私有变量:width,length和height。
3.定义一个构造方法
Box(double width,double length,double height),用来初始化这三个变量值。
讯享网
4.定义了一个shouBox()方法,用于输出变量
讯享网package test2; public class Box { private double width; private double length; private double height; public Box(double width,double length,double height){ this.width=width; this.length=length; this.height=height; } public void showBox(){ System.out.println("Box dimensions:width="+width+",length="+length+",height="+height); } }
package test2; public class main { public static void main(String[] args) { Box b=new Box(1.5,2.0,3.0); b.showBox(); } }
这段代码创建了一个 Box 对象b,调用了其中的showBox()方法,输出该对象的宽,长和高
结果:


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/127494.html