• 博客(0)
  • 资源 (1)

空空如也

Java面向对象程序设计(苏健版)

public class Rectangle { //声明矩形类 int xTopLeft; //左上角横坐标 int yTopLeft; //左上角纵坐标 int xBottomRight; //右下角横坐标 int yBottomRight; //右下角纵坐标 int id; //唯一编号 static int nextId; //表示下一个对象唯一编号 void move(int xOffset,int yOffset){//移动:xOffset、yOffset 分别是横向、纵向位移 xTopLeft = xTopLeft + xOffset; yTopLeft = yTopLeft + yOffset; xBottomRight = xBottomRight + xOffset; yBottomRight = yBottomRight + yOffset; } } class Example{ public static void main(String[] args){ Rectangle rect1 = new Rectangle(); //创建第1 个矩形对象 rect1.xTopLeft = 100; //设置左上角横坐标 rect1.yTopLeft = 100; //设置左上角纵坐标 rect1.xBottomRight = 300; //设置右下角横坐标 rect1.yBottomRight = 150; //设置右下角纵坐标 rect1.id = Rectangle.nextId; //设置矩形对象的唯一编号 Rectangle.nextId++; //生成下一个唯一编号 Rectangle rect2 = new Rectangle(); //创建第2 个矩形对象 rect2.xTopLeft = 200; rect2.yTopLeft = 100; rect2.xBottomRight = 500; rect2.yBottomRight = 200; rect2.id = Rectangle.nextId; //设置矩形对象的唯一编号 Rectangle.nextId++; //生成下一个唯一编号 rect1.move(10, 20); //将第一个矩形对象移动:横坐标移动10,纵坐标移动20 System.out.println("编号为"+rect1.id+"的矩形对象的周长与面积分别是"); int width1 = rect1.xBottomRight - rect1.xTopLeft; //第1 个矩形对象的宽度 int height1 = rect1.yBottomRight - rect1.yTopLeft; ///第1 个矩形对象的高度 System.out.println((width1 + height1)*2);//输出rect1 引用对象的周长 System.out.println( width1 * height1 );//输出rect1 引用的对象的面积 System.out.println("编号为"+rect2.id+"的矩形对象的周长与面积分别是"); int width2 = rect2.xBottomRight - rect2.xTopLeft; int height2 = rect2.yBottomRight - rect2.yTopLeft; System.out.println((width2 + height2)*2);//输出rect1 引用对象的周长 System.out.println( width2 * height2 );//输出rect1 引用的对象的面积 }

2019-04-03

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除