开发工具:
文件大小: 2kb
下载次数: 0
上传时间: 2019-04-03
详细说明: 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 引用的对象的面积 }
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
相关搜索: