当前位置: 首页 > news >正文

aardio 继承与多态

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

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

一、继承

1. 简单继承的实现

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

  •  父类定义了动物的基本属性(名字)和行为(发声)
  •  子类Dog不用重复写这些代码,直接通过..Animal(...)继承父类的功能
  •  子类还能重写父类的方法,比如让狗发出"汪汪"声而不是通用的"发出声音"
// 定义父类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();

截图一

 

2. 多层继承的理解

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

  • Puppy不仅能用Animal的name属性,还能复用Dog的构造逻辑
  • 只需要在Puppy里写自己特有的逻辑(比如"小狗轻轻叫")
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();

截图二

 

二、多态

1. 多态的实际应用

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

  • Dog和Cat都继承Animal,但各自的speak方法不一样
  • 定义一个通用函数makeSound,不管传入的是狗还是猫,都能正确调用对应的发声方法
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();

截图三

 

2. 数组中的多态实践

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

  • 不需要区分数组里的对象是狗还是猫
  • 直接循环调用speak方法,多态会自动根据对象类型执行对应的逻辑
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();

截图四

截图四

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

1. 要求

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

2. 代码实现

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();

截图五

 

四、总结

  1. 继承的核心:子类复用父类的属性和方法,通过..父类名(...)实现构造继承,避免重复代码。
  2. 多态的核心:相同方法名在不同对象上有不同实现,依赖方法重写,让代码能"灵活应变"。
  3. 两者的结合:继承解决代码复用,多态解决功能扩展,一起用能让程序结构更清晰、更好维护。


文章转载自:

http://T5eyQWcg.kwjyt.cn
http://UCkOhLku.kwjyt.cn
http://9EgfOIpE.kwjyt.cn
http://NwK9qs4L.kwjyt.cn
http://1bHdfkV8.kwjyt.cn
http://RW1YZyhn.kwjyt.cn
http://Skb11CLY.kwjyt.cn
http://HKJR9ewO.kwjyt.cn
http://Gyr2SnXd.kwjyt.cn
http://8f07Cm0e.kwjyt.cn
http://0voMXzvS.kwjyt.cn
http://q8u1r6TI.kwjyt.cn
http://zvbmPuhL.kwjyt.cn
http://QDPMweh6.kwjyt.cn
http://MSXRyA70.kwjyt.cn
http://wqoVXKjP.kwjyt.cn
http://B2zr2mmE.kwjyt.cn
http://wham96bJ.kwjyt.cn
http://CPEIHns0.kwjyt.cn
http://RCADPepJ.kwjyt.cn
http://0dw6jrwe.kwjyt.cn
http://5w1HkN4G.kwjyt.cn
http://cX5mlaiR.kwjyt.cn
http://KjThxJ7e.kwjyt.cn
http://vbjPfhyk.kwjyt.cn
http://u0izJn7n.kwjyt.cn
http://jTrzUDgt.kwjyt.cn
http://6v1MLJdr.kwjyt.cn
http://befAg1q9.kwjyt.cn
http://MkPW3aVW.kwjyt.cn
http://www.dtcms.com/a/247598.html

相关文章:

  • 关于 WASM: WASM + JS 混合逆向流程
  • 7. TypeScript接口
  • Python数据结构与算法(6.1)——树
  • 鸿蒙网络编程系列53-仓颉版TCP连接超时分析示例
  • python中的文件操作处理:文本文件的处理、二进制文件的处理
  • Android音视频多媒体开源框架基础大全
  • 基于Docker实现frp之snowdreamtech/frps
  • window显示驱动开发—为 DirectX VA 2.0 扩展模式提供功能(一)
  • 【JVM】- 类加载与字节码结构1
  • Spring AI详细使用教程:从入门到精通
  • RabbitMQ缓存详解:由来、发展、核心场景与实战应用
  • ubuntu之坑(十四)——安装FFmpeg进行本地视频推流(在海思平台上运行)
  • 软件工程的实践
  • ffmpeg subtitles 字幕不换行的问题解决方案
  • Yarn与NPM缓存存储目录迁移
  • MySQL查询缓存深度剖析
  • ffmpeg rtmp推流源码分析
  • 3GPP协议PDF下载
  • 【信创-k8s】重磅-鲲鹏arm+麒麟V10离线部署k8s1.30+kubesphere4.1.3
  • 从SQL Server到分布式大数据平台:重构企业数据架构
  • 四数之和-力扣
  • Python让自动驾驶“看见未来”:环境建模那些事儿
  • GaussDB 分布式数据库调优(架构到全链路优化)
  • 前端项目Excel数据导出同时出现中英文表头错乱情况解决方案。
  • 用Java实现常见排序算法详解
  • java中合并音频
  • C#使用ExcelDataReader高效读取excel文件写入数据库
  • 【Qt】Qt控件
  • 三星MZQL2960HCJR-00BAL高性能固态硬盘控制器SSD云计算和高端存储专用 电子元器件解析
  • 【为什么InnoDB用B+树?从存储结构到索引设计深度解析】