用Java设计一个以animal为父类 feline类giraffe类canine类继承animal类然后lion类tiger类cat类继承feline类同时dog类wolf类继承canine类并且cat类和dog类也继承另一个单独的petable类 的类的结构 ,同时实现可比较,可拷贝,即实现Comparable接口和Cloneable接口。拷贝时候注意测试深拷贝和浅拷贝
时间: 2023-07-16 20:12:26 浏览: 138
动物类的Java程序
下面是Java实现的代码:
```java
public abstract class Animal implements Comparable<Animal>, Cloneable {
protected String name;
protected int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public abstract void makeSound();
@Override
public int compareTo(Animal o) {
if (this.age < o.age) {
return -1;
} else if (this.age > o.age) {
return 1;
} else {
return 0;
}
}
@Override
public Animal clone() throws CloneNotSupportedException {
return (Animal) super.clone();
}
}
public abstract class Feline extends Animal {
public Feline(String name, int age) {
super(name, age);
}
}
public class Giraffe extends Animal {
public Giraffe(String name, int age) {
super(name, age);
}
@Override
public void makeSound() {
System.out.println("Giraffe " + name + " is making sound.");
}
}
public abstract class Canine extends Animal {
public Canine(String name, int age) {
super(name, age);
}
}
public class Lion extends Feline {
public Lion(String name, int age) {
super(name, age);
}
@Override
public void makeSound() {
System.out.println("Lion " + name + " is making sound.");
}
}
public class Tiger extends Feline {
public Tiger(String name, int age) {
super(name, age);
}
@Override
public void makeSound() {
System.out.println("Tiger " + name + " is making sound.");
}
}
public class Cat extends Feline implements Petable {
public Cat(String name, int age) {
super(name, age);
}
@Override
public void makeSound() {
System.out.println("Cat " + name + " is making sound.");
}
@Override
public void play() {
System.out.println("Cat " + name + " is playing.");
}
}
public class Dog extends Canine implements Petable {
public Dog(String name, int age) {
super(name, age);
}
@Override
public void makeSound() {
System.out.println("Dog " + name + " is making sound.");
}
@Override
public void play() {
System.out.println("Dog " + name + " is playing.");
}
}
public class Wolf extends Canine {
public Wolf(String name, int age) {
super(name, age);
}
@Override
public void makeSound() {
System.out.println("Wolf " + name + " is making sound.");
}
}
public interface Petable {
void play();
}
```
要测试深拷贝和浅拷贝,可以在Animal类中实现clone方法,然后在测试中分别进行深拷贝和浅拷贝的测试:
```java
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Animal animal1 = new Cat("Tom", 2);
Animal animal2 = animal1.clone();
System.out.println(animal1 == animal2); // false
System.out.println(animal1.getClass() == animal2.getClass()); // true
System.out.println(animal1.equals(animal2)); // true
Cat cat1 = new Cat("Kitty", 1);
Cat cat2 = cat1.clone();
System.out.println(cat1 == cat2); // false
System.out.println(cat1.getClass() == cat2.getClass()); // true
System.out.println(cat1.equals(cat2)); // true
System.out.println(cat1.name == cat2.name); // true,因为String是不可变对象,浅拷贝后两个对象的name引用指向同一个String对象
}
}
```
阅读全文