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());
    }
}
相关推荐
疯狂的喵2 分钟前
C++编译期多态实现
开发语言·c++·算法
2301_7657031412 分钟前
C++中的协程编程
开发语言·c++·算法
m0_7487080513 分钟前
实时数据压缩库
开发语言·c++·算法
lly2024061 小时前
jQuery Mobile 表格
开发语言
惊讶的猫1 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
jmxwzy1 小时前
Spring全家桶
java·spring·rpc
Halo_tjn1 小时前
基于封装的专项 知识点
java·前端·python·算法
m0_748233172 小时前
30秒掌握C++核心精髓
开发语言·c++
Fleshy数模2 小时前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言
Duang007_2 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python