0
点赞
收藏
分享

微信扫一扫

【Java每日一题】— —第二十四题:编程定义一个长方形类Rectangle(2023.10.08)

一叶轻舟okok 2023-10-08 阅读 18

🎯问题:

🎯 结果:

public class Java2{
		private int height;
		private int width;
		public Java2() {//无参构造方法
		}
		public Java2(int heingt,int width) {//有参构造方法
			this.height=heingt;
			this.width=width;
		}
		public int getHeight() {
			return height;
		}
		public void setHeight(int height) {
			this.height = height;
		}
		public int getWidth() {
			return width;
		}
		public void setWidth(int width) {
			this.width = width;
		}
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "Rectangle [ width="+width+",height="+height;
		}
		public int zhouChang() {
			return 2*(height+width);
		}
		public int area() {
			return height*width;
		}
		public static void main(String[] args) {
			 // 创建无参构造方法的对象
	        Java2 rectangle1 = new Java2();
	        rectangle1.setHeight(10);
	        rectangle1.setWidth(8);
	        System.out.println(rectangle1.toString());
	        System.out.println("周长为:" + rectangle1.zhouChang());
	        System.out.println("面积为:" + rectangle1.area());

	        // 创建带参构造方法的对象
	        Java2 rectangle2 = new Java2(12, 9);
	        System.out.println(rectangle2.toString());
	        System.out.println("周长为:" + rectangle2.zhouChang());
	        System.out.println("面积为:" + rectangle2.area());
	    }
	}
举报

相关推荐

0 条评论