matlab 基础知识class < superclass_name

 matlab中的class,在matlab中也能够通过创建类的方式实现面向对象编程。


1)类定义

>> classdef (attribute1 = value,...) classname(< superclass_name)
   properties
         PropName
   end

   methods
         methodName
   end        
   events
        EventName
   end

end

类名必须要与文件名称相同,第一个括号里声明类的属性,类似于java中的public等关键词,更具体的解释可參考:Class Attributes。

第二个括号指明类是否须要继承父类,类似于java中的extendskeyword或者C++中的‘:’。

相同matlab也支持多重继承,更具体的解释可參考:Creating Subclasses —Syntax and Techniques。在实现链表的过程中,我们须要继承handle类。


properties类似于成员变量。更具体的解释可參考:Defining Properties。
methods就是详细的函数实现。有非常多种不同类型的函数。以下简要介绍:

普通函数(Ordinarymethods)。

大部分都是普通函数,它负责完毕该类要完毕的主要任务。
构造函数(Constructormethods)。

和其它高级语言类似。也必须是类名。用来给属性赋值。

唯一不同是它必须返回它创建的对象。

更具体的解释可參考:Class Constructor Methods。


析构函数(Destructormethods)。名字必须是delete,用来释放空间。更具体的解释可參考:Handle Class Delete Methods。
属性訪问函数(Propertyaccess methods)。类似于其它高级语言中的set、get函数。

更具体的解释可參考:Controlling PropertyAccess。
静态函数(Staticmethods)。类似于其它高级语言中的静态函数,通过类对象进行訪问。
转换函数(Conversionmethods)。是对其它类构造函数的重载,能够将自定义类的对象转换成重载构造函数类的对象。更具体的解释可參考:Converting Objects to Another Class。
抽象函数(Abstractmethods)。

无需解释,用来在子类中继承的函数。


样例:定义一个结点类node

>> classdef node < handle
   properties
      data
   end

   properties(SetAccess = public)
      next
   end

   methods
   function node = node(data)
      if nargin > 0
             node.data=data;
       end
      end
   end

end












版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/gcczhongduan/p/4732339.html