定义一个animal类要求如下:包括成员变量:名称name,体重weight,颜色color;包括动态方法run,输出run fastly,动态方法eat,输出I love bone;定义重载方法run(String name),输出谁跑得快;在main方法里新建一个对象a,用new调用构造方法,然后打印输出a的成员变量,再调用run和eat方法。
时间: 2023-12-06 17:44:15 浏览: 70
go-fastly:Golang Fastly API客户端库
以下是Animal类的定义和main方法的实现:
```java
public class Animal {
private String name;
private double weight;
private String color;
public Animal(String name, double weight, String color) {
this.name = name;
this.weight = weight;
this.color = color;
}
public void run() {
System.out.println("Run fastly");
}
public void eat() {
System.out.println("I love bone");
}
public void run(String name) {
System.out.println(name + " runs fastly");
}
public String getName() {
return name;
}
public double getWeight() {
return weight;
}
public String getColor() {
return color;
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Animal("Dog", 15.5, "Brown");
System.out.println("Name: " + a.getName());
System.out.println("Weight: " + a.getWeight());
System.out.println("Color: " + a.getColor());
a.run();
a.eat();
a.run("Rabbit");
}
}
```
输出结果为:
```
Name: Dog
Weight: 15.5
Color: Brown
Run fastly
I love bone
Rabbit runs fastly
```
阅读全文