句柄和值类的区别 [英] Difference of handle and values classes

查看:67
本文介绍了句柄和值类的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些C ++背景,想在Matlab中使用类.

I have some C++ background and want to use classes in Matlab.

句柄和值类有什么区别?

What is the difference between a handle and a value class?

我知道,如果我想使用重载运算符(例如"+"和"*")定义矩阵类,则会使用值类.但是,有时候当我选择一个handle类时,事情似乎只对我有用.

I know that I would use a value class if I wanted to define a matrix class with overloaded operators such as "+" and "*". However, sometimes it seems that things only work for me as I want when I pick a handle class.

推荐答案

MathWorks在其 Matt Dunham )是MATLAB中OOP的出色入门.

The MathWorks provide some information on which to be used for which purpose in their help and also in their PDF documentation. I've also found this article from the Yagtom Project (originally by Matt Dunham) to be a great introduction to OOP in MATLAB.

在帮助中的解释如下(强调我的意思):

In the help this is explained as follows (emphasis mine):

MATLAB支持两种类-处理类和值类.您使用的类的类型取决于类实例的所需行为以及要使用的功能.

MATLAB support two kinds of classes — handle classes and value classes. The kind of class you use depends on the desired behavior of the class instances and what features you want to use.

要创建对包含在该类对象中的数据的引用,并且不希望该对象的副本成为该对象数据的副本时,请使用句柄类.例如,使用句柄类可实现包含电话簿条目信息的对象.多个应用程序可以访问特定的电话簿条目,但是只能有一组基础数据.

Use a handle class when you want to create a reference to the data contained in an object of the class, and do not want copies of the object to make copies of the object data. For example, use a handle class to implement an object that contains information for a phone book entry. Multiple application programs can access a particular phone book entry, but there can be only one set of underlying data.

句柄的引用行为使这些类可以支持事件,侦听器和动态属性等功能.

The reference behavior of handles enables these classes to support features like events, listeners, and dynamic properties.

使用值类表示不需要唯一的实体,例如数值.例如,使用值类实现多项式数据类型.您可以复制

Use value classes to represent entities that do not need to be unique, like numeric values. For example, use a value class to implement a polynomial data type. You can copy

在马特·邓纳姆(Matt Dunham)的文章中,还提出了区分两种类型的另一个优点:

In the article by Matt Dunham, another good point is made to distinguish both types of classes:

我们之前提到过,Matlab中的对象(默认情况下)是通过值传递 的,这意味着完整的副本将在方法调用中来回传递.但是,Matlab图形对象通过引用被传递(通过句柄).如果我们将内置句柄类 [...] 子类化,则我们类的对象也将通过引用而不是值进行传递.

We previously mentioned that objects in Matlab are, (by default) passed by value, meaning that full copies are passed back and forth in method calls. Matlab graphics objects, however, are passed by reference, (via handles). If we subclass the built in handle class [...] then objects of our class will be passed by reference too, not value.

我个人倾向于使用句柄类来获取一些Java常用的语法: 对于句柄类,可以让对象存储所有信息,这样就可以执行类似sort(a)(或a.sort())的操作. 对于值类,等效项为a = sort(a)(或a = a.sort()).对于运算符,第一种语法完全没有意义,但是第二种显然很常用: a = a + b(等效于a = plus(a,b)a = a.plus(b)).

I personally tend to use handle classes to get some of the syntax I'm used to with Java: for a handle class, you can have the object store all information, such that you can have operations like sort(a) (or a.sort()) be performed in place. For value classes the equivalent of this is a = sort(a) (or a = a.sort()). For operators the first syntax makes no sense at all, but the second is obviously in general use: e.g. a = a + b (this is equivalent to a = plus(a,b) and a = a.plus(b)).

  • 如果类的主要关注点是存储某种(数字)值并能够执行操作,则值类很可能是您可以采用的方法.
  • 如果要存储状态,将相关值分组,将不同的对象连接在一起(链接列表,图形等),则可以使用handle类.

或者至少是我倾向于用来区别.

Or at least, that's what I tend to use to make the distinction.

这篇关于句柄和值类的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆