文章目录

  • Comparison of Handle and Value Classes
    • Basic Difference
      • Create a Value Class
      • Create a Handle Class
    • Behavior of MATLAB Built-In Classes
    • User-Defined Value Classes
      • Value Object Behavior
      • Modifying Value Objects in Functions
    • User-Defined Handle Classes
      • Handle Object Behavior
      • Modifying Handle Objects in Functions
      • Deleting Handles
      • Initialize Properties to Contain Handle Objects
    • Determining Equality of Objects
      • Equality of Value Objects
      • Equality of Handle Objects

Comparison of Handle and Value Classes

Basic Difference

A value class constructor returns an object that is associated with the variable to which it is assigned. If you reassign this variable, MATLAB® creates an independent copy of the original object. If you pass this variable to a function to modify it, the function must return the modified object as an output argument. For information on value-class behavior, see Avoid Unnecessary Copies of Data.

A handle class constructor returns a handle object that is a reference to the object created. You can assign the handle object to multiple variables or pass it to functions without causing MATLAB to make a copy of the original object. A function that modifies a handle object passed as an input argument does not need to return the object.

All handle classes are derived from the abstract handle class.

Create a Value Class

By default, MATLAB classes are value classes. The following definition creates a value class named MyValueClass:

classdef MyValueClass ...
end

Create a Handle Class

To create a handle class, derive the class from the handle class.

classdef MyHandleClass < handle...
end

Behavior of MATLAB Built-In Classes

MATLAB fundamental classes are value classes (numeric, logical, char, cell, struct, and function handle). For example, if you create an object of the class int32 and make a copy of this object, the result is two independent objects. When you change the value of a, the value of b does not change. This behavior is typical of classes that represent values.

a = int32(7);
b = a;
a = a^4;
b7

MATLAB graphics objects are implemented as handle objects because they represent visual elements. For example, create a graphics line object and copy its handle to another variable. Both variables refer to the same line object.

x = 1:10; y = sin(x);
l1 = line(x,y);
l2 = l1;

Set the properties of the line object using either copy of the handle.

set(l2,'Color','red')
set(l1,'Color','green')
get(l2,'Color')
ans =0     1     0

Calling the delete function on the l2 handle destroys the line object. If you attempt to set the Color property on the line l1, the set function returns an error.

delete(l2)
set(l1,'Color','blue')
Error using matlab.graphics.primitive.Line/set
Invalid or deleted object.

If you delete the object by deleting any one of the existing handles, all copies are now invalid because you deleted the single object to which all handles refer.

Deleting a handle object is not the same as clearing the handle variable. In the graphics object hierarchy, the parent of the object holds a reference to the object. For example, the parent axes hold a reference to the line object referred to by l1 and l2. If you clear both variables from the workspace, the object still exists.

For more information on the behavior of handle objects, see Handle Object Behavior.

User-Defined Value Classes

MATLAB associates objects of value classes with the variables to which you assign the object. When you copy a value object to another variable or pass a value object to a function, MATLAB creates an independent copy of the object and all the data contained by the object. The new object is independent of changes to the original object. Value objects behave like MATLAB numeric and struct classes. Each property behaves essentially like a MATLAB array.

Value objects are always associated with one workspace or temporary variable. Value objects go out of scope when their variable goes out of scope or is cleared. There are no references to value objects, only copies that are independent objects.

Value Object Behavior

Here is a value class that stores a value in its Number property. The default property value is the number 1.

classdef NumValuepropertiesNumber = 1end
end

Create a NumValue object assigned to the variable a.

a = NumValue
a = NumValue with properties:Number: 1

Assign the value of a to another variable, b.

b = a
b = NumValue with properties:Number: 1

The variables a and b are independent. Changing the value of the Number property of a does not affect the Number property of b.

a.Number = 7
a = NumValue with properties:Number: 7
b
b = NumValue with properties:Number: 1

Modifying Value Objects in Functions

When you pass a value object to a function, MATLAB creates a copy of that object in the function workspace. Because copies of value objects are independent, the function does not modify the object in the caller’s workspace. Therefore, functions that modify value objects must return the modified object to be reassigned in the caller’s workspace.

For more information, see Object Modification.

User-Defined Handle Classes

Instances of classes that derive from the handle class are references to the underlying object data. When you copy a handle object, MATLAB copies the handle, but does not copy the data stored in the object properties. The copy refers to the same object as the original handle. If you change a property value on the original object, the copied handle references the same change.

Handle Object Behavior

Here is a handle class that stores a value in its Number property. The default property value is the number 1.

classdef NumHandle < handlepropertiesNumber = 1end
end

Create a NumHandle objects assigned to the variable a.

a = NumHandle
a = NumHandle with properties:Number: 1

Assign the value of a to another variable, b.

b = a
b = NumHandle with properties:Number: 1

The variables a and b refer to the same underlying object. Changing the value of the Number property of a also changes the Number property of b. That is, a and b refer to the same object.

a.Number = 7
a = NumHandle with properties:Number: 7
b
b = NumHandle with properties:Number: 7

Modifying Handle Objects in Functions

When you pass a handle object to a function, MATLAB creates a copy of the handle in the function workspace. Because copies of handles reference the same underlying object, functions that modify the handle object effectively modify the object in the caller’s workspace as well. Therefore, it is not necessary for functions that modify handle objects passed as input arguments to return the modified object to the caller.

For more information, see Object Modification.

Deleting Handles

You can destroy handle objects by explicitly calling the handle delete method. Deleting the handle of a handle class object makes all handles invalid. For example:

a = NumHandle;
b = a;
delete(a)
b.Number
Invalid or deleted object.

Calling delete on a handle object invokes the destructor function or functions for that object. See Handle Class Destructor for more information.

Initialize Properties to Contain Handle Objects

For information on the differences between initializing properties to default values in the properties block and initializing properties from within the constructor, see Initialize Property Values and Initialize Arrays of Handle Objects.

Determining Equality of Objects

Equality for value objects means that the objects are of the same class and have the same state.

Equality for handle objects means that the handle variables refer to the same object. You also can identify handle variables that refer to different objects of the same class that have the same state.

Equality of Value Objects

To determine if value objects are the same size and their contents are of equal value, use isequal. For example, use the previously defined NumValue class to create two instances and test for equality:

a = NumValue;
b = NumValue;
isequal(a,b)
ans =1

a and b are independent and therefore are not the same object. However each represents the same value.

If you change the value represented by a value object, the objects are no longer equal.

a = NumValue;
b = NumValue;
b.Number = 7;
isequal(a,b)
ans =0

Value classes do not have a default eq method to implement the == operation.

Equality of Handle Objects

Handle objects inherit an eq method from the handle base class. You can use == and isequal to test for two different relationships among handle objects:

  • The handles refer to the same object: == and isequal return true.
  • The handles refer to objects of the same class that have the same values, but are not the same objects — only isequal returns true.

Use the previously defined NumHandle class to create an object and copy the handle.

a = NumHandle;
b = a;

Test for equality using == and isequal.

a == b
ans =1
isequal(a,b)
ans =1

Create two instances of the NumHandle class using the default values.

a = NumHandle;
b = NumHandle;

Determine if a and b refer to the same object.

a == b
ans =0

Determine if a and b have the same values.

isequal(a,b)
ans =1

MATLAB value和handle类的区别相关推荐

  1. “抽象类”的定义及其与“普通类”的区别

    我们都知道在多态中子类要重写父类的方法,执行时也执行子类中的方法,这就显得父类中的方法体有点子虚乌有了, 也就是说可以直接省略方法体,而只定义一个方法就可以了.因此,我们称一个没有方法体的方法为抽象方 ...

  2. android r类 作用,Android 主项目和 Module 中 R 类的区别

    Android 主项目和 Module 中 R 类的区别 我们知道 Android 项目中会通过自动生成一个 R.java 类的方式来保存项目中所有资源文件的标识在主项目中生成的 R.java 中的资 ...

  3. Handle类的用法

    android中Handle类的用法 当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无 ...

  4. matlab ezplot fplot,【转】Matlab plot fplot ezplot用法与区别

    [转]Matlab plot fplot ezplot用法与区别 (2012-04-19 20:26:00) 标签: matlab fplot ezplot 数学函数 曲线 杂谈 函数plot 是绘制 ...

  5. Debug类和Trace类的区别

    Debug类和Trace类的区别 您一定发现了在System.Diagnostics命名空间中还有一个名为Trace的类.它的函数功能和Debug非常相似.为什么要有这样两个功能类似的类呢? 原因是这 ...

  6. java中的stack类和C++中的stack类的区别

    文章目录 1 java中的stack类和C++中的stack类的区别 1.1 java中的stack类 1.2 C++中的stack类 1.3 分析 不经意间想到了这个问题,存到栈中的是对象的引用,还 ...

  7. URI和URLConnection类的区别

    URI和URLConnection类的区别 (1) URI格式 通用资源标志符(Universal Resource Identifier, 简称"URI") 就Android平台 ...

  8. matlab括号区别,matlab中各种括号(),[],与{}的区别与认识

    matlab中各种括号(),[],与{}的区别与认识 发布时间:2018-06-04 10:37, 浏览次数:469 , 标签: matlab 原文 在matlab中,常常会遇到(),[],和{},这 ...

  9. 接口(interface)和抽象(abstract)类的区别

    2019独角兽企业重金招聘Python工程师标准>>> 接口与抽象类的区别: 1 两者表达的概念不一样.抽象类是一类事物的高度聚合,与子类的关系属于"是"的关系: ...

最新文章

  1. windows2003前言
  2. stm32篇--系统初始化
  3. selenium如何解决IE自动填充表单问题
  4. sqlserver 分页_四类数据库分页实现方案总结之PG分页实现
  5. 反手发力动作--乒在民间
  6. IntelliJ IDEA 如何知道项目中的模块数据_如何从项目源中选择模块加入当前项目中(添加模块)_如何移除项目中的模块(移除模块/删除模块)
  7. mysql的安装、启动和基础配置 —— mac版本
  8. HDU 1716 排列2
  9. 使用Swagger辅助开发Fabric Application的Web API
  10. 区域医疗移动医疗影像解决方案1-基于HTML5的PACS
  11. HTML5移动端最新兼容问题解决方案
  12. 我的JdbcUtils类
  13. sqlplus命令支持上、下翻功能
  14. 《Revisiting Self-Supervised Monocular Depth Estimation》论文笔记
  15. pygame鼠标进行拖拽移动图片、缩放、以及按钮响应 案例
  16. ZARA卖床单,线上年增长300%,服饰品牌HOME店成趋势?
  17. 开源究竟有什么魅力?听完这 4 个故事你也许会明白
  18. 什么?CC协议中的“保持一致”是“不许修改”?
  19. 安卓框架,分析项目中surfaceFlinger出现的bug ---queueBuffer: BufferQueue has been abandoned
  20. c语言中的除法符号,C中的逐位有符号除法算法

热门文章

  1. 袋鼠下载IOS用的一款不限速下载工具支持极速下载,在线秒播
  2. ObjectARX常用类和函数
  3. 安装grid问题汇总
  4. Fake news on Twitter during the 2016 U.S.presidential election 原文及翻译
  5. 元宇宙不仅是一个技术手段问题,更是一个方方面面的载体
  6. [生存志] 第119节 刘安编著淮南鸿烈
  7. 目前使用过的各大厂商rtsp取流的url
  8. 跨境电商卖家工具——跨境卫士内容介绍
  9. Git——解决回滚版本后变成游离分支无法提交代码
  10. python将txt文件转为字符串_使用Python将复数转换为文本文件中的单数