aardio 继承与多态

面向对象编程中的继承与多态

今天开始学习面向对象编程的核心概念------继承与多态,发现这两个概念和现实生活的现象很像,学起来挺有意思的。

一、继承

1. 简单继承的实现

先试着写了一个父类Animal和子类Dog的例子,发现继承真的像"遗传"一样:

  • 父类定义了动物的基本属性(名字)和行为(发声)
  • 子类Dog不用重复写这些代码,直接通过..Animal(...)继承父类的功能
  • 子类还能重写父类的方法,比如让狗发出"汪汪"声而不是通用的"发出声音"
javascript 复制代码
// 定义父类Animal
class Animal {
    ctor(name) {
        this.name = name;
    }
    speak = function() {
        return this.name + " makes a sound.";
    }
}

// Dog类继承Animal
class Dog {
    ctor(...) {
        this = ..Animal(...);  // 调用父类构造函数
    }
    // 重写speak方法
    speak = function() {
        return this.name + " barks.";
    }
}

import console;
var myDog = Dog("Buddy");
console.log(myDog.speak());  // 输出:Buddy barks.
console.pause();
null

2. 多层继承的理解

又试了三层继承(Animal→Dog→Puppy),发现子类可以层层继承上层的所有功能:

  • Puppy不仅能用Animal的name属性,还能复用Dog的构造逻辑
  • 只需要在Puppy里写自己特有的逻辑(比如"小狗轻轻叫")
javascript 复制代码
import console;

class Animal {
    ctor(name) { 
        this.name = name; 
        }
    speak = function() { 
        return this.name + " makes a sound."; 
    }
}

class Dog {
    ctor(...) { 
        this = ..Animal(...); 
    }
    speak = function() {
        return this.name + " barks."; 
    }
}

class Puppy {
    ctor(...) { 
        this = ..Dog(...); 
    }
    speak = function() {
        return this.name + " puppy barks softly."; 
    }
}

var myPuppy = Puppy("Tommy");
console.log(myPuppy.speak());  // 输出:Tommy puppy barks softly.
console.pause();
null

二、多态

1. 多态的实际应用

写一个猫狗的例子理解多态,发现它的核心是"同一个方法名,不同对象有不同表现":

  • Dog和Cat都继承Animal,但各自的speak方法不一样
  • 定义一个通用函数makeSound,不管传入的是狗还是猫,都能正确调用对应的发声方法
javascript 复制代码
class Animal {
    ctor(name) { 
        this.name = name; 
        }
    speak = function() {
        return this.name + " makes a sound."; 
    }
}

class Dog {
    ctor(...) { 
        this = ..Animal(...); 
    }
    speak = function() { 
        return this.name + " barks."; 
    }
}

class Cat {
    ctor(...) { 
        this = ..Animal(...); 
    }
    speak = function() { 
        return this.name + " meows."; 
    }
}

function makeSound(animal) {
    console.log(animal.speak());
}

import console;
var myDog = Dog("Buddy");
var myCat = Cat("Whiskers");
makeSound(myDog);  // 输出:Buddy barks.
makeSound(myCat);  // 输出:Whiskers meows.
console.pause();
null

2. 数组中的多态实践

把不同动物对象放到数组里统一处理,更能体现多态的便利:

  • 不需要区分数组里的对象是狗还是猫
  • 直接循环调用speak方法,多态会自动根据对象类型执行对应的逻辑
javascript 复制代码
class Animal {
    ctor(name) { 
        this.name = name; 
    }
    speak = function() { 
        return this.name + " makes a sound."; 
    }
}

class Dog {
    ctor(...) { 
        this = ..Animal(...); 
    }
    speak = function() { 
        return this.name + " barks."; 
    }
}

class Cat {
    ctor(...) { 
        this = ..Animal(...); 
    }
    speak = function() { 
        return this.name + " meows."; 
    }
}

import console;
var animals = {Dog("Buddy"), Cat("Whiskers")};
for(k, animal in animals) {
    console.log(animal.speak());
}
// 输出:
// Buddy barks.
// Whiskers meows.
console.pause();
null

三、挑战:用继承和多态计算图形面积

1. 要求

  • 定义Shape父类,包含计算面积的area方法
  • 定义Circle和Rectangle子类,重写area方法
  • 用数组存储不同图形对象,遍历输出面积

2. 代码实现

kotlin 复制代码
import math;
import console;

class Shape {
    area = function() { return 0; }  // 父类默认返回0
}

class Circle {
    ctor(radius, ...) {
        this = ..Shape(...);
        this.radius = radius;
    }
    area = function() {
        return ..math.pi * this.radius * this.radius;
    }
}

class Rectangle {
    ctor(width, height, ...) {
        this = ..Shape(...);
        this.width = width;
        this.height = height;
    }
    area = function() {
        return this.width * this.height;
    }
}

var shapes = {Circle(5), Rectangle(4, 6)};
for(k, shape in shapes) {
    console.log("面积: " + shape.area());
}
// 输出:
// 面积: 78.539816339745
// 面积: 24
console.pause();
null

四、总结

  1. 继承的核心 :子类复用父类的属性和方法,通过..父类名(...)实现构造继承,避免重复代码。
  2. 多态的核心:相同方法名在不同对象上有不同实现,依赖方法重写,让代码能"灵活应变"。
  3. 两者的结合:继承解决代码复用,多态解决功能扩展,一起用能让程序结构更清晰、更好维护。
相关推荐
superman超哥3 小时前
Rust 异步性能的黑盒与透视:Tokio 监控与调优实战
开发语言·后端·rust·编程语言·rust异步性能·rust黑盒与透视·tokio监控与调优
冬奇Lab4 小时前
【Kotlin系列04】类与对象基础:从Java Bean到Data Class的优雅蜕变
android·kotlin·编程语言
superman超哥6 小时前
Rust 异步编程的终极考验:Tokio 资源管理与清理
开发语言·rust·编程语言·rust异步编程·tokio资源管理与清理
superman超哥1 天前
Rust 异步时间管理核心:Tokio 定时器实现机制深度剖析
开发语言·rust·编程语言·rust异步时间管理核心·tokio定时器实现机制·tokio定时器
superman超哥1 天前
Rust 异步递归的解决方案
开发语言·后端·rust·编程语言·rust异步递归
superman超哥1 天前
Rust 异步并发核心:tokio::spawn 与任务派发机制深度解析
开发语言·rust·编程语言·rust异步并发核心·rust任务派发机制
superman超哥1 天前
Rust 异步并发基石:异步锁(Mutex、RwLock)的设计与深度实践
开发语言·后端·rust·编程语言·rust异步并发·rust异步锁·rust mutex
superman超哥1 天前
Rust 异步错误处理最佳实践
开发语言·rust·编程语言·rust异步错误处理·rust最佳实践
冬奇Lab2 天前
【Kotlin系列03】控制流与函数:从if表达式到Lambda的进化之路
android·kotlin·编程语言
冬奇Lab2 天前
【Kotlin系列02】变量与数据类型:从val/var到空安全的第一课
android·kotlin·编程语言