再定义一个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-06 17:44:38 浏览: 75
以下是Dog类的定义:
```java
public class Dog extends Animal {
private String category;
public Dog(String name, int age, String category) {
super(name, age);
this.category = category;
}
@Override
public void run() {
System.out.println("dog run fastly");
}
@Override
public void eat() {
System.out.println("dog love bone");
}
}
```
在main方法中新建一个对象d1,并输出其run和eat方法的结果:
```java
public static void main(String[] args) {
Dog d1 = new Dog("Bobby", 3, "Golden Retriever");
d1.run();
d1.eat();
}
```
输出结果为:
```
dog run fastly
dog love bone
```
相关问题
定义一个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方法
下面是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
```
java定义一个Animal类要求如下: (1)包括成员变量:名称name,体重weight,颜色color (2)包括动态方法run()(输出“run fastly”);eat()(输出“I love bone”) (3)定义重载方法run(String name),输出谁跑得快 (4)包括有参构造方法(利用Eclipse的Generate Constructor using Fileds方法) (5)在main方法里新建一个对象a(new调用构造方法),然后打印输出a的成员变量,再调用run和eat方法再定义一个Dog类,要求如下 (6)继承父类Animal,增加新的成员变量:种类category (7)定义有参构造方法 (8)重写父类的run()方法(输出“dog run fastly”);eat()(输出“dog love bone”) (9)在main方法里新建一个对象d1(new调用构造方法,Dog d1 = new ….),然后打印输出d1的run和eat方法
以下是Java定义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 fastly");
}
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");
}
public String getCategory() {
return category;
}
}
```
在main方法里新建一个Animal对象和一个Dog对象,并输出它们的成员变量、调用它们的run和eat方法:
```
public static void main(String[] args) {
Animal a = new Animal("Animal1", 10.0, "brown");
System.out.println("Animal name: " + a.getName());
System.out.println("Animal weight: " + a.getWeight());
System.out.println("Animal color: " + a.getColor());
a.run();
a.eat();
a.run("Animal1");
Dog d1 = new Dog("Dog1", 8.0, "black", "Golden Retriever");
System.out.println("Dog name: " + d1.getName());
System.out.println("Dog weight: " + d1.getWeight());
System.out.println("Dog color: " + d1.getColor());
System.out.println("Dog category: " + d1.getCategory());
d1.run();
d1.eat();
}
```
输出结果:
```
Animal name: Animal1
Animal weight: 10.0
Animal color: brown
run fastly
I love bone
Animal1 runs fastly
Dog name: Dog1
Dog weight: 8.0
Dog color: black
Dog category: Golden Retriever
dog run fastly
dog love bone
```
阅读全文