Java面向对象继承体系的示例


以下是一个简单的Java面向对象继承体系的示例:

// 定义一个动物类
public class Animal {
    protected int age;
    protected double weight;

    public Animal(int age, double weight) {
        this.age = age;
        this.weight = weight;
    }

    public void eat() {
        System.out.println("Animal is eating.");
    }

    public void sleep() {
        System.out.println("Animal is sleeping.");
    }
}

// 定义一个狗类,继承自动物类
public class Dog extends Animal {
    private String breed;

    public Dog(int age, double weight, String breed) {
        super(age, weight);
        this.breed = breed;
    }

    public void bark() {
        System.out.println("Dog is barking.");
    }

    @Override
    public void eat() {
        System.out.println("Dog is eating.");
    }
}

// 定义一个猫类,继承自动物类
public class Cat extends Animal {
    private String color;

    public Cat(int age, double weight, String color) {
        super(age, weight);
        this.color = color;
    }

    public void meow() {
        System.out.println("Cat is meowing.");
    }

    @Override
    public void sleep() {
        System.out.println("Cat is sleeping soundly.");
    }
}

// 测试程序
public class Main {
    public static void main(String[] args) {
        Animal animal1 = new Animal(5, 10.5);
        Dog dog1 = new Dog(3, 8.2, "Golden Retriever");
        Cat cat1 = new Cat(2, 4.6, "Black");

        animal1.eat(); // Animal is eating.
        dog1.eat(); // Dog is eating.
        cat1.sleep(); // Cat is sleeping soundly.
    }
}

在上面的示例中,Animal类是一个父类,它包含了所有动物通用的属性和方法。Dog类和Cat类分别继承自Animal类,并添加了它们自己的属性和方法。在Main类中,我们创建了一个Animal对象、一个Dog对象和一个Cat对象,并测试了它们各自的行为。

通过这个简单的示例,我们可以看到Java面向对象继承体系的设计的基本思想,即父类定义通用的属性和方法,子类继承并添加自己的属性和方法。同时,我们还使用了多态的特性,使得同一个方法调用可以产生不同的行为。

另一个更复杂的示例,涉及到抽象类、接口和访问修饰符的使用,如下:

// 定义一个抽象类Animal
public abstract class Animal {
    protected int age;
    protected double weight;

    public Animal(int age, double weight) {
        this.age = age;
        this.weight = weight;
    }

    public abstract void eat(); // 抽象方法

    public void sleep() {
        System.out.println("Animal is sleeping.");
    }

    // protected方法,子类可访问
    protected void grow(int years) {
        this.age += years;
        this.weight *= 1.1 * years;
    }
}

// 定义一个接口Swimable
public interface Swimable {
    public void swim();
}

// 定义一个类Dog,实现Swimable接口
public class Dog extends Animal implements Swimable {
    private String breed;

    public Dog(int age, double weight, String breed) {
        super(age, weight);
        this.breed = breed;
    }

    public void bark() {
        System.out.println("Dog is barking.");
    }

    @Override
    public void eat() {
        System.out.println("Dog is eating.");
    }

    @Override
    public void swim() {
        System.out.println("Dog is swimming.");
    }
}

// 定义一个类Fish,实现Swimable接口
public class Fish implements Swimable {
    private String name;
    private double length;

    public Fish(String name, double length) {
        this.name = name;
        this.length = length;
    }

    public void swim() {
        System.out.println("Fish is swimming.");
    }
}

// 测试程序
public class Main {
    public static void main(String[] args) {
        Dog dog1 = new Dog(3, 8.2, "Golden Retriever");
        Fish fish1 = new Fish("Nemo", 2.3);

        dog1.eat(); // Dog is eating.
        fish1.swim(); // Fish is swimming.
    }
}

在上面的示例中,Animal类是一个抽象类,其中包含了一个抽象方法eat(),子类必须实现这个方法。Swimable接口定义了一个swim()方法,Dog类和Fish类实现了这个接口,并实现了各自的swim()方法。在Main类中,我们创建了一个Dog对象和一个Fish对象,并测试了它们各自的行为。

通过这个示例,我们可以看到Java面向对象继承体系的设计的一些高级特性,包括抽象类和接口的使用,以及访问修饰符的应用。这些特性可以使代码更加灵活和可扩展,从而满足更复杂的需求。


原文链接:codingdict.net