实验二 Java简单类与对象
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
1.
package 实验报告2; class Rectangle { private double width,height; private String color; public Rectangle(String color,double width,double height) { this.setWidth(width); this.setHeight(height); this.setColor(color); } public void getArea() { System.out.println("面积为"+getWidth()*getHeight()); } public void getLength() { System.out.println("周长为"+(getWidth()+getHeight())*2); } public void getColor2() { System.out.println("颜色为"+getColor()); } public double getWidth() { return width; } public void setWidth(double a) { width=a; } public double getHeight() { return height; } public void setHeight(double b) { height=b; } public String getColor() { return color; } public void setColor(String n) { color=n; } public static void main(String [] args) { Rectangle per=new Rectangle("红色",15.00,3.00); per.getLength(); per.getArea(); per.getColor2(); } }
原文:https://www.cnblogs.com/xzhxzh/p/11545929.html