Main Content

本页的翻译已过时。点击此处可查看最新英文版本。

子类定义

子类的语法和设计

了解如何实现子类、从子类调用超类方法以及控制哪些类可以从超类派生。

工具

类图查看器创建图来调查类细节和层次结构

函数

superclasses超类的名称

matlab.diagram.ClassViewer在类图查看器中创建类图
matlab.mixin.Heterogeneous异构数组构造的超类

主题

类的层次结构

Hierarchies of Classes — Concepts

Organizing classes into hierarchies facilitates the reuse of code.

Designing Heterogeneous Class Hierarchies

Heterogeneous arrays can contain objects of different class, but all objects in the array must derive from a common superclass.

创建子类

子类语法

在 classdef 行中的类名后指定超类。

设计子类构造函数

在类文件的 classdef 行中指定超类。

对子类对象调用超类方法

子类方法可以覆盖继承的超类方法。子类方法可以调用它们覆盖的超类方法。

Control Sequence of Constructor Calls

Control the order in which MATLAB® constructs objects in class hierarchy.

修改继承的方法

子类可以修改继承的方法。

Modify Inherited Properties

Subclasses can modify inherited properties that are abstract or private.

Subclassing Multiple Classes

Multiple inheritance can lead to conflicting definitions. All superclasses must be free of conflicts in definition.

Specify Allowed Subclasses

Classes can restrict the classes that can derive from it.

句柄类兼容性

Handle Compatible Classes

Enable both handle and value classes to derive from a specific class.

How to Define Handle-Compatible Classes

Handle compatible classes have applications for mixin and abstract superclasses.

Methods for Handle Compatible Classes

Handle compatible class methods must work with both handle and value objects.

Handle-Compatible Classes and Heterogeneous Arrays

Heterogeneous class hierarchies impose certain restrictions when classes are handle compatible.

控制对类成员的访问

类成员访问

确定属性、方法和事件允许的访问级别。