首页 >Java >java教程 >通过实例了解Java中的继承

通过实例了解Java中的继承

Mary-Kate Olsen
Mary-Kate Olsen原创
2025-01-03 16:35:42150浏览

通过实例了解Java中的继承

继承是面向对象编程(OOP)的核心概念,它允许一个类获取另一个类的属性(属性和方法)。在Java中,继承是使用extends关键字实现的,并表示“is-a”关系。本文通过一个实际例子解释了Java中的继承。

代码示例

// Defining a class
class Animal {
    // General attributes
    protected String colour;
    protected String breed;
    protected int age;

    // General methods
    public String sleep() {
        return "Both cats and dogs sleep";
    }

    public String eat() {
        return "They also eat";
    }

    // Constructor
    public Animal(String colour, String breed, int age) {
        this.colour = colour;
        this.breed = breed;
        this.age = age;
    }

    // Getters and setters
    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public String getBreed() {
        return breed;
    }

    public void setBreed(String breed) {
        this.breed = breed;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

// Cat class inheriting from Animal class
class Cat extends Animal {
    private String catName;

    public Cat(String colour, String breed, int age, String catName) {
        super(colour, breed, age); // Call the parent class constructor
        this.catName = catName;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public String catSound() {
        return "Cat meows!";
    }
}

// Dog class inheriting from Animal class
class Dog extends Animal {
    private String dogName;

    public Dog(String colour, String breed, int age) {
        super(colour, breed, age);
    }

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public String dogSound() {
        return "Dog barks!";
    }
}

public class Demo {
    public static void main(String[] args) {
        Cat myCat = new Cat("Brown", "Persian", 2, "Tom");
        Dog myDog = new Dog("Black", "Labrador", 3);

        // Display Cat details
        System.out.println("Cat's Name: " + myCat.getCatName());
        System.out.println("Cat's Colour: " + myCat.getColour());
        System.out.println("Cat's Breed: " + myCat.getBreed());
        System.out.println("Cat's Age: " + myCat.getAge());
        System.out.println("Cat Sound: " + myCat.catSound());
        System.out.println("Cat Behavior: " + myCat.eat() + " and " + myCat.sleep());

        // Display Dog details
        System.out.println("Dog's Colour: " + myDog.getColour());
        System.out.println("Dog's Breed: " + myDog.getBreed());
        System.out.println("Dog's Age: " + myDog.getAge());
        System.out.println("Dog Sound: " + myDog.dogSound());
    }
}

代码中的关键概念

父类(动物):

  • 定义所有动物共有的共同属性(颜色、品种、年龄)和方法(睡眠、进食)。
  • 提供构造函数来初始化这些属性。
  • 包括用于封装的 getter 和 setter。

儿童课程(猫和狗):

Understanding Inheritance in Java Through a Practical Example
Understanding Inheritance in Java Through a Practical Example

  • 扩展Animal类并继承其属性和方法。
  • 添加特定属性(catName、dogName)和行为(catSound、dogSound)。
  • 使用super关键字调用父类构造函数并初始化继承的属性。

演示课程:

  • 作为程序的入口点。
  • 演示如何创建 Cat 和 Dog 类的对象并访问它们的属性和方法。

继承的好处

  • 代码可重用性:Cat 和 Dog 类重用 Animal 类中的代码。
  • 可扩展性:可以通过扩展 Animal 类轻松添加新的子类(例如 Bird、Fish)。
  • 多态性:可以在子类中重写 sleep 和 eat 等共享方法,以提供特定的行为。

程序的输出

Cat's Name: Tom
Cat's Colour: Brown
Cat's Breed: Persian
Cat's Age: 2
Cat Sound: Cat meows!
Cat Behavior: They also eat and Both cats and dogs sleep
Dog's Colour: Black
Dog's Breed: Labrador
Dog's Age: 3
Dog Sound: Dog barks!

我的 GitHub
java 仓库

以上是通过实例了解Java中的继承的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn