Java创建一个类包含角色位置,大小等属性
时间: 2024-01-14 11:02:11 浏览: 32
你可以使用如下代码创建一个具有角色位置、大小等属性的Java类:
```
public class Role {
private int x; // 角色横向位置
private int y; // 角色纵向位置
private int width; // 角色宽度
private int height; // 角色高度
public Role(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// Getter and Setter methods for the above attributes
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
```
在上述代码中,我们创建了一个名为Role的类,并声明了四个私有的属性:x、y、width和height,分别表示角色的横向位置、纵向位置、宽度和高度。同时,我们提供了一个构造函数,用于初始化角色属性。此外,我们还提供了Getter和Setter方法,用于获取和设置角色属性。你可以根据需要在这个类中添加其他的属性和方法。