JAVA类和对象练习

设计一个矩形类Rectangle,包含私有的数据成员宽度(Width)和高度(Height);公有的方法double getArea()返回矩形的面积,公有的方法double getPerimeter()返回矩形的周长。

例如:

测试 Result
Rectangle rect=new Rectangle(8,5); System.out.printf("The Area is %.2f,Perimeter is %.2f\n", rect.getArea(),rect.getPerimeter()); The Area is 40.00,Perimeter is 26.00
java 复制代码
class Rectangle{
    private double Width;
    private double Height;

    public Rectangle(double width, double height) {
        Width = width;
        Height = height;
    }
    public double getArea() {
        return Width * Height;
    }
    public double getPerimeter(){
        return (Width + Height) * 2;
    }
}
public class Test{
    public static void main(String[] args) {
        Rectangle rect=new Rectangle(8,5);
        System.out.printf("The Area is %.2f,Perimeter is %.2f\n", rect.getArea(),rect.getPerimeter());
    }
}
相关推荐
lee_curry44 分钟前
第四章 jvm中的垃圾回收器
java·jvm·垃圾收集器
九转成圣2 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
SmartRadio2 小时前
ESP32-S3 双模式切换实现:兼顾手机_路由器连接与WiFi长距离通信
开发语言·网络·智能手机·esp32·长距离wifi
laowangpython2 小时前
Rust 入门:GitHub 热门内存安全编程语言
开发语言·其他·rust·github
我叫汪枫2 小时前
在后台管理系统中,如何递归和选择保留的思路来过滤菜单
开发语言·javascript·node.js·ecmascript
_.Switch2 小时前
东方财富股票数据JS逆向:secids字段和AES加密实战
开发语言·前端·javascript·网络·爬虫·python·ecmascript
软件技术NINI2 小时前
webkit简介及工作流程
开发语言·前端·javascript·udp·ecmascript·webkit·yarn
Brendan_0012 小时前
JavaScript的Stomp.over
开发语言·javascript·ecmascript
念2342 小时前
f5 shape分析
开发语言·javascript·ecmascript
苍穹之跃2 小时前
某量JS逆向
开发语言·javascript·ecmascript