Java面向对象编程有哪些基础练习可以学习?
- 内容介绍
- 文章标签
- 相关推荐
本文共计216个文字,预计阅读时间需要1分钟。
定义立方体类,具有属性边长和颜色,包含+方法设置颜色和计算体积,创建一个立方体对象,将边长设置为3,颜色设置为green,输出该立方体的体积。
1定义立方体类Cube,具有属性边长和颜色,具有 方法设置颜色和计算体积,在该类的主方法中创 建一个立方体对象,将该对象的边长设置为3, 颜色设置为“green”,输出该立方体的体积和颜色。ublic class Cube { float width; String color; public float getWidth() { return width; } public void setWidth(float width) { this.width = width; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } float v(){ return width*width*width; } } package gh; public class text1 { public static void main(String[] args) { Cube myCube= new Cube(); myCube.width=3; myCube.color="green"; System.out.println(myCube.color); System.out.println(myCube.v()); } }
本文共计216个文字,预计阅读时间需要1分钟。
定义立方体类,具有属性边长和颜色,包含+方法设置颜色和计算体积,创建一个立方体对象,将边长设置为3,颜色设置为green,输出该立方体的体积。
1定义立方体类Cube,具有属性边长和颜色,具有 方法设置颜色和计算体积,在该类的主方法中创 建一个立方体对象,将该对象的边长设置为3, 颜色设置为“green”,输出该立方体的体积和颜色。ublic class Cube { float width; String color; public float getWidth() { return width; } public void setWidth(float width) { this.width = width; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } float v(){ return width*width*width; } } package gh; public class text1 { public static void main(String[] args) { Cube myCube= new Cube(); myCube.width=3; myCube.color="green"; System.out.println(myCube.color); System.out.println(myCube.v()); } }

