|
发表于 2020-3-30 14:10:27
|
显示全部楼层
本楼为最佳答案
显然不能,但第二问和第三问可以使用一个公用代码- /**
- * 定义矩形类
- */
- public class Rectangle {
- private double width;
- private double length;
- public double getWidth() {
- return width;
- }
- public void setWidth(double width) {
- this.width = width;
- }
- public double getLength() {
- return length;
- }
- public void setLength(double length) {
- this.length = length;
- }
- public Rectangle(double width, double length) {
- this.width = width;
- this.length = length;
- }
- public Rectangle() {
- }
- double getPerimeter(){
- return (width+length)*2;
- }
- double getArea(){
- return width*length;
- }
- }
复制代码
然后
- /**
- * 使用矩形类
- */
- public class RectDemo {
- public static void main(String[] args) {
- /**
- * 问题二解答
- */
- Rectangle rect1=new Rectangle();
- rect1.setWidth(10);
- rect1.setLength(20);
- System.out.println("矩形的面积是:"+rect1.getArea());
- System.out.println("矩形的周长是:"+rect1.getPerimeter());
- /**
- * 问题三解答
- */
- Rectangle rect2=new Rectangle(1,2);
- System.out.println("矩形的面积是:"+rect2.getArea());
- System.out.println("矩形的周长是:"+rect2.getPerimeter());
- }
- }
复制代码 以上为后两问代码 |
|