定义一个Animal类要求如下:(2)包括成员变量:名称name,体重weight,颜色color (3)包括动态方法run()(输出“run fastly”);eat()(输出“I love bone”) (4)定义重载方法run(String name),输出谁跑得快 (5)包括有参构造方法(利用Eclipse的Generate Constructor using Fileds方法) (6)在main方法里新建一个对象a(new调用构造方法),然后打印输出a的成员变量,再调用run和eat方法 再定义一个Dog类,要求如下 (7)继承父类Animal,增加新的成员变量:种类category (8)定义有参构造方法 (9)重写父类的run()方法(输出“dog run fastly”);eat()(输出“dog love bone”) (10)在main方法里新建一个对象d1(new调用构造方法,Dog d1 = new ….),然后打印输出d1的run和eat方法
时间: 2023-12-27 12:04:09 浏览: 239
下面是Animal类和Dog类的实现:
Animal类:
```
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 fast");
}
// 成员变量的get方法
public String getName() {
return name;
}
public double getWeight() {
return weight;
}
public String getColor() {
return color;
}
}
```
Dog类:
```
public class Dog extends Animal {
// 新增成员变量
private String category;
// 有参构造方法
public Dog(String name, double weight, String color, String category) {
super(name, weight, color);
this.category = category;
}
// 重写方法
@Override
public void run() {
System.out.println("dog run fastly");
}
@Override
public void eat() {
System.out.println("dog love bone");
}
// 成员变量的get方法
public String getCategory() {
return category;
}
}
```
在main方法中测试:
```
public static void main(String[] args) {
// 新建Animal对象
Animal a = new Animal("Tom", 10.0, "brown");
System.out.println("Animal: name=" + a.getName() + ", weight=" + a.getWeight() + ", color=" + a.getColor());
a.run();
a.eat();
a.run("Tom");
// 新建Dog对象
Dog d1 = new Dog("Lucy", 8.0, "white", "Golden Retriever");
System.out.println("Dog: name=" + d1.getName() + ", weight=" + d1.getWeight() + ", color=" + d1.getColor() + ", category=" + d1.getCategory());
d1.run();
d1.eat();
}
```
输出:
```
Animal: name=Tom, weight=10.0, color=brown
run fastly
I love bone
Tom runs fast
Dog: name=Lucy, weight=8.0, color=white, category=Golden Retriever
dog run fastly
dog love bone
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)