get set 用法

本文介绍了使用Java实现矩形和直角三角形图形绘制的基本原理与代码实现,包括类继承、属性设置与图形绘制方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class Arist {
public void drawShape(Shape x) {
x.draw();
}


public static void main(String[] args) {
Arist a = new Arist();
a.drawShape(new Rectangle(8,4));
a.drawShape(new RightTriangle(8,4));
}


}

public class Rectangle extends Shape {

private int width;

private int height;


public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}


public int getWidth() {
return this.width;


}


public void setWidth(int width) {
if (width >= 1 && width <= 15) {
this.width = width;
}


}


public int getHeight() {
return this.height;
}


public void setHeight(int height) {
if (height > 15 || height < 1) {
return;
}
this.height = height;
}


public void draw() {


for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width; j++) {
if (i == 0 || i == this.height - 1 || j == 0
|| j == this.width - 1) {
System.out.println("*");
} else {
System.out.print(" ");


}
System.out.println();
}


}
}


}

public class RightTriangle extends Shape{


private  int base;


private  int height;


public RightTriangle(int base,int height){
this.base=base;
this.height=height;
}
public int getbase(){
return this.base;
}
public void setbase(int base){
if(base>=1 && base<=20){
this.base=base;
}


}
public int getheight(){
return this.height;
}
public void setheight(int height){
if(height<1||height>20){
return;
}
this.height=height;
}
public void draw() {
System.out.println("打印一个直角三角形");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值