public class Square { private double side; //成员变量side,类型double public void setSide(double a) { side = a; // 设置正方形的边长,无返回值 } public double getSide() { return side; // 返回正方形自身边长 } public double getPerimeter() { return 4*side; // 返回正方形周长 } public double getArea() { return side*side; //返回正方形面积 } } class SquareTest{ public static void main (String[] args) { Square object1,object2; //引用型变量 object1,object2 object1=new Square(); //构造对象,对象返回值给object1 object2=new Square(); //构造对象,对象返回值给object2 object1.setSide(1); //设置边长为1 object2.setSide(10); //设置边长为10 System.out.println("object1 side ="+object1.getSide()); //计算并输出正方形的边长 System.out.println("object1 Perimeter="+object1.getPerimeter()); //计算并输出正方形的周长 System.out.println("object1 Area="+object1.getArea()); //计算并输出正方形的面积 System.out.println("object2 side ="+object2.getSide()); //计算并输出正方形的边长 System.out.println("object2 Perimeter="+object2.getPerimeter()); //计算并输出正方形的周长 System.out.println("object2 Area="+object2.getArea()); //计算并输出正方形的面积 } }
It's Java(1)
原文地址:https://www.cnblogs.com/Keepcharm/p/12559318.html
时间: 2024-10-26 00:15:56