定义一个昆虫类Insert,要求包括属性name,size,color.定义其有参构造方法。生成成员变量的get和set方法。定义run方法void run(Insect a)打印输出谁在跑,接收参数为Insect类型。在main函数测试该类new一个实例i ,调用run方法然后打印输出i 的name,size,color
时间: 2024-02-09 08:11:37 浏览: 27
好的,我明白了。以下是代码实现:
```java
public class Insect {
private String name;
private int size;
private String color;
// 有参构造方法
public Insect(String name, int size, String color) {
this.name = name;
this.size = size;
this.color = color;
}
// get方法
public String getName() {
return name;
}
public int getSize() {
return size;
}
public String getColor() {
return color;
}
// set方法
public void setName(String name) {
this.name = name;
}
public void setSize(int size) {
this.size = size;
}
public void setColor(String color) {
this.color = color;
}
// run方法
public void run(Insect a) {
System.out.println(a.getName() + "在跑");
}
public static void main(String[] args) {
Insect i = new Insect("蜻蜓", 10, "红色");
i.run(i);
System.out.println("昆虫名称:" + i.getName() + ",大小:" + i.getSize() + ",颜色:" + i.getColor());
}
}
```
运行结果如下:
```
蜻蜓在跑
昆虫名称:蜻蜓,大小:10,颜色:红色
```