类—矩形面积

矩形的类

 1 public class Rec {
 2 
 3     
 4     double length;//
 5     double wide;//
 6     
 7     public double getArea(){
 8         
 9         return (length * wide);
10         
11     }
12     
13     Rec(int length,int wide){
14         
15         this.length = length;
16         this.wide = wide;
17     }
18     
19     Rec(double length,double wide){
20         
21         this.length = length;
22         this.wide = wide;
23     }
24     
25 }

测试类,调用矩形类

 1 public class RecTest {
 2 
 3     public static void main(String[] args) {
 4 
 5         // 调用
 6 
 7         Rec r = new Rec(4, 4);// 构造对象并初始化
 8 
 9         System.out.println("矩形的面积是:" + r.getArea());
10 
11     }
12 }

运行的结果:

矩形的面积是:16.0

原文地址:https://www.cnblogs.com/zhengfengyun/p/5138101.html