JAVA建一个Dog类。属性name、breed、color。定义输出基本信息的方法和喊叫方法。并建立测试类测试
时间: 2024-05-24 13:10:56 浏览: 130
public class Dog {
private String name;
private String breed;
private String color;
public Dog(String name, String breed, String color) {
this.name = name;
this.breed = breed;
this.color = color;
}
public void bark() {
System.out.println(name + " is barking!");
}
public void printInfo() {
System.out.println("Name: " + name);
System.out.println("Breed: " + breed);
System.out.println("Color: " + color);
}
}
public class DogTest {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", "Golden Retriever", "Golden");
myDog.bark();
myDog.printInfo();
}
}
相关问题
用Java写一个程序,定义猫类Cat。属性:毛的颜色color,品种breed。行为:吃饭eat(),抓老鼠catchMouse() 定义狗类Dog。属性:毛的颜色color,品种breed。行为:吃饭(),看家lookHome() 要求: 1.按照以上要求定义Cat类和Dog类,属性要私有,生成空参、有参构造,set和get方法 2.定义测试类,在main方法中创建该类的对象并给属性赋值(set方法或有参构造方法) 3.调用成员方法,打印格式如下: 花色的波斯猫正在吃鱼..... 花色的波
代码如下:
Cat类:
```java
public class Cat {
private String color;
private String breed;
public Cat() {
}
public Cat(String color, String breed) {
this.color = color;
this.breed = breed;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setBreed(String breed) {
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void eat() {
System.out.println(color + "的" + breed + "正在吃鱼.....");
}
public void catchMouse() {
System.out.println(color + "的" + breed + "正在抓老鼠.....");
}
}
```
Dog类:
```java
public class Dog {
private String color;
private String breed;
public Dog() {
}
public Dog(String color, String breed) {
this.color = color;
this.breed = breed;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setBreed(String breed) {
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void eat() {
System.out.println(color + "的" + breed + "正在吃饭.....");
}
public void lookHome() {
System.out.println(color + "的" + breed + "正在看家.....");
}
}
```
测试类:
```java
public class Test {
public static void main(String[] args) {
Cat cat = new Cat();
cat.setColor("花色");
cat.setBreed("波斯猫");
cat.eat();
cat.catchMouse();
Dog dog = new Dog();
dog.setColor("黑色");
dog.setBreed("藏獒");
dog.eat();
dog.lookHome();
}
}
```
输出结果:
```
花色的波斯猫正在吃鱼.....
花色的波斯猫正在抓老鼠.....
黑色的藏獒正在吃饭.....
黑色的藏獒正在看家.....
```
这个题目要求完成父类和子类的定义,并完成测试。(总共20分) (1)定义动物类animal,在类中定义属性和方法: 我拥有属性sex,表示性别,数据类型为boolean; 我拥有属性age,表示年龄,数据类型为整数; (2)定义子类cat和dog,它们继承于animal类,重写一些属性和方法: cat类: 我是猫的子类,我拥有属性name,表示名字,数据类型为字符串,在子类中定义; 我拥有属性weight,表示体重,数据类型为小数,在子类中定义; 我重写了方法show,该方法用于显示猫的信息,包括introduce()的基本信息和子类特有的属性信息。 dog类: 我是狗的子类,我拥有属性breed,表示品种,数据类型为字符串,在子类中定义; 我拥有属性color,表示颜色,数据类型为字符串,在子类中定义; 我重写了方法show,该方法用于显示狗的信息,包括introduce()的基本信息和子类特有的属性信息。 (3)完成测试: 测试代码中,实例化一个猫对象和一个狗对象,分别调用它们的introduce()和show()方法,以检查定义的类是否正确。
好的,我知道了。下面是代码实现和测试:
```python
class Animal:
def __init__(self, sex, age):
self.sex = sex
self.age = age
def introduce(self):
print(f"I am an animal. My sex is {self.sex} and my age is {self.age}.")
class Cat(Animal):
def __init__(self, sex, age, name, weight):
super().__init__(sex, age)
self.name = name
self.weight = weight
def show(self):
print(f"My name is {self.name}. My weight is {self.weight}.")
self.introduce()
class Dog(Animal):
def __init__(self, sex, age, breed, color):
super().__init__(sex, age)
self.breed = breed
self.color = color
def show(self):
print(f"My breed is {self.breed}. My color is {self.color}.")
self.introduce()
cat = Cat(True, 3, "Kitty", 4.5)
dog = Dog(False, 5, "Golden Retriever", "Golden")
cat.introduce()
cat.show()
dog.introduce()
dog.show()
```
输出结果如下:
```
I am an animal. My sex is True and my age is 3.
My name is Kitty. My weight is 4.5.
I am an animal. My sex is True and my age is 3.
I am an animal. My sex is False and my age is 5.
My breed is Golden Retriever. My color is Golden.
I am an animal. My sex is False and my age is 5.
```
阅读全文