本文目录导读:

在Java中,继承是通过extends关键字实现的,我来给你详细讲解几个实际案例。
基础继承案例
// 父类:动物
class Animal {
protected String name;
protected int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println(name + "正在吃东西");
}
public void sleep() {
System.out.println(name + "正在睡觉");
}
}
// 子类:狗
class Dog extends Animal {
private String breed; // 品种
public Dog(String name, int age, String breed) {
super(name, age); // 调用父类构造器
this.breed = breed;
}
// 子类特有方法
public void bark() {
System.out.println(name + "汪汪叫");
}
// 方法重写
@Override
public void eat() {
System.out.println(name + "正在啃骨头");
}
}
// 子类:猫
class Cat extends Animal {
private String color;
public Cat(String name, int age, String color) {
super(name, age);
this.color = color;
}
public void meow() {
System.out.println(name + "喵喵叫");
}
@Override
public void eat() {
System.out.println(name + "正在吃鱼");
}
}
// 测试类
public class InheritanceDemo {
public static void main(String[] args) {
Dog dog = new Dog("旺财", 3, "金毛");
Cat cat = new Cat("咪咪", 2, "橘色");
// 调用继承的方法
dog.sleep(); // 继承自父类
dog.eat(); // 重写的方法
dog.bark(); // 子类特有方法
cat.sleep(); // 继承自父类
cat.eat(); // 重写的方法
cat.meow(); // 子类特有方法
}
}
多层继承案例
// 顶层父类
class Vehicle {
protected String brand;
protected String model;
public Vehicle(String brand, String model) {
this.brand = brand;
this.model = model;
}
public void start() {
System.out.println(brand + " " + model + " 启动");
}
public void stop() {
System.out.println(brand + " " + model + " 停止");
}
}
// 中间层:汽车
class Car extends Vehicle {
protected int doors;
public Car(String brand, String model, int doors) {
super(brand, model);
this.doors = doors;
}
public void honk() {
System.out.println(brand + " " + model + " 鸣笛");
}
}
// 最底层:电动车
class ElectricCar extends Car {
private int batteryCapacity; // 电池容量
public ElectricCar(String brand, String model, int doors, int batteryCapacity) {
super(brand, model, doors);
this.batteryCapacity = batteryCapacity;
}
public void charge() {
System.out.println(brand + " " + model + " 正在充电,电池容量:" + batteryCapacity + "kWh");
}
@Override
public void start() {
System.out.println(brand + " " + model + " 电动启动(静音)");
}
}
public class MultiLevelInheritance {
public static void main(String[] args) {
ElectricCar tesla = new ElectricCar("特斯拉", "Model 3", 4, 75);
tesla.start(); // 重写的方法
tesla.honk(); // 继承自Car
tesla.stop(); // 继承自Vehicle
tesla.charge(); // 子类特有方法
}
}
抽象类继承案例
// 抽象父类
abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
// 抽象方法:必须由子类实现
public abstract double getArea();
// 具体方法
public String getColor() {
return color;
}
public void display() {
System.out.println("这是一个" + color + "色的图形,面积:" + getArea());
}
}
// 子类:圆形
class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
// 子类:矩形
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
}
public class AbstractInheritance {
public static void main(String[] args) {
Shape circle = new Circle("红色", 5.0);
Shape rectangle = new Rectangle("蓝色", 4.0, 6.0);
// 多态调用
circle.display();
rectangle.display();
// 无法实例化抽象类
// Shape shape = new Shape("黑色"); // 编译错误
}
}
使用final关键字的继承限制
// final类:不能被继承
final class FinalClass {
public void method() {
System.out.println("final类的方法");
}
}
// 无法继承FinalClass
// class SubClass extends FinalClass {} // 编译错误
class Base {
// final方法:子类不能重写
public final void finalMethod() {
System.out.println("这是final方法");
}
public void normalMethod() {
System.out.println("这是普通方法");
}
}
class Sub extends Base {
// 可以重写普通方法
@Override
public void normalMethod() {
System.out.println("重写了普通方法");
}
// 不能重写final方法
// @Override
// public void finalMethod() {} // 编译错误
}
接口继承(多重继承)
// 接口1
interface Swimmable {
void swim();
}
// 接口2
interface Flyable {
void fly();
}
// 类可以继承一个类并实现多个接口
class Bird extends Animal implements Flyable {
public Bird(String name, int age) {
super(name, age);
}
@Override
public void fly() {
System.out.println(name + "在飞翔");
}
}
class Duck extends Animal implements Swimmable, Flyable {
public Duck(String name, int age) {
super(name, age);
}
@Override
public void swim() {
System.out.println(name + "在游泳");
}
@Override
public void fly() {
System.out.println(name + "在飞行");
}
}
继承的关键点总结:
- 使用
extends关键字:class Subclass extends Superclass - 构造器链:子类构造器必须调用父类构造器(使用
super()) - 方法重写:子类可以重写父类的方法(使用
@Override注解) - 访问修饰符:
private:不能继承protected:可以被子类和同包类访问public:对所有类可见
- Java只支持单继承:一个类只能有一个直接父类
- 可以通过接口实现多重继承:一个类可以实现多个接口
这些案例涵盖了Java继承的主要用法,可以根据实际需求选择合适的继承方式。