MatLab与其他面向对象语言的比较
来源:互联网 发布:淘宝网渔具大全价格 编辑:程序博客网 时间:2024/05/21 06:39
Comparing MATLAB with Other OO Languages
Some Differences from C++ and Java Code
Modifying Objects
Common Object-Oriented Techniques
Some Differences from C++ and Java Code
The MATLAB® programming language differs from other object-oriented languages, such as C++ or Java® in some important ways.
Public Properties
Unlike fields in C++ or the Java language, you can use MATLAB properties to define a public interface separate from the implementation of data storage. You can provide public access to properties because you can define set and get access methods that execute automatically when assigning or querying property values. For example, the following statement:
myobj.Material = 'plastic';
assigns the string plastic to the Material property of myobj. Before making the actual assignment, myobj executes a method called set.Material (assuming the class of myobj defines this method), which can perform any necessary operations. See Property Access Methods for more information on property access methods.
You can also control access to properties by setting attributes, which enable public, protected , or private access. See Property Attributes for a full list of property attributes.
No Implicit Parameters
In some languages, one object parameter to a method is always implicit. In MATLAB, objects are explicit parameters to the methods that act on them.
Dispatching
In MATLAB classes, method dispatching is not based on method signature, as it is in C++ and Java code. When the argument list contains objects of equal precedence, MATLAB software uses the left-most object to select the method to call. However, if the class of that argument is superior to the other arguments, MATLAB can dispatch to a method of an argument in any position within an argument list.
See Class Precedence for more information.
Calling Superclass Method
In C++, you call a superclass method using the scoping operator: superclass::method
In Java code, you use: superclass.method
The equivalent MATLAB operation is method@superclass.
Other Differences
In MATLAB classes, there is no equivalent to C++ templates or Java generics. However, MATLAB is weakly typed and it is possible to write functions and classes that work with different types of data.
MATLAB classes do not support overloading functions using different signatures for the same function name.
Modifying Objects
MATLAB classes can define public properties, which you can modify by explicitly assigning values to those properties on a given instance of the class. However, only classes derived from the handle class exhibit reference behavior. Modifying a property value on an instance of a value classes (classes not derived from handle), changes the value only within the context in which the modification is made.
The sections that follow describe this behavior in more detail.
Passing Objects to Functions
MATLAB passes all variables by value. When you pass an object to a function, MATLAB copies the value from the caller into the parameter variable in the called function.
However, MATLAB supports two kinds of classes that behave differently when copied:
Handle classes — a handle class instance variable refers to an object. A copy of a handle class instance variable refers to the same object as the original variable. If a function modifies a handle object passed as an input argument, the modification affects the object referenced by both the original and copied handles.
Value classes — the property data in an instance of a value class are independent of the property data in copies of that instance (although, a value class property could contain a handle). A function can modify a value object that is passed as an input argument, but this modification does not affect the original object.
See Comparing Handle and Value Classes for more information on the behavior and use of both kinds of classes.
Passing Value Objects. When you pass a value object to a function, the function creates a local copy of the argument variable. The function can modify only the copy. If you want to modify the original object, return the modified object and assign it to the original variable name. For example, consider the value class, SimpleClass :
classdef SimpleClass properties Color end methods function obj = SimpleClass(c) if nargin > 0 obj.Color = c; end end end end
Create an instance of SimpleClass, assigning a value of red to its Color property:
obj = SimpleClass('red');
Pass the object to the function g, which assigns blue to the Color property:
function y = g(x) x.Color = 'blue'; y = x;end
y = g(obj);
The function g modifies its copy of the input object and returns that copy, but does not change the original object.
y.Colorans = blueobj.Colorans = red
If the function g did not return a value, the modification of the object Color property would have occurred only on the copy of obj within the function workspace. This copy would have gone out of scope when the function execution ended.
Overwriting the original variable actually replaces it with a new object:
obj = g(obj);
Passing Handle Objects. When you pass a handle to a function, the function makes a copy of the handle variable, just like when passing a value object. However, because a copy of a handle object refers to the same object as the original handle, the function can modify the object without having to return the modified object.
For example, suppose you modify the SimpleClass class definition to make a class derived from the handle class:
classdef SimpleHandleClass < handle properties Color end methods function obj = SimpleHandleClass(c) if nargin > 0 obj.Color = c; end end endend
Create an instance of SimpleHandleClass, assigning a value of red to its Color property:
obj = SimpleHandleClass('red');
Pass the object to the function g, which assigns blue to the Color property:
y = g(obj);
The function g sets the Color property of the object referred to by both the returned handle and the original handle:
y.Colorans =blueobj.Colorans =blue
The variables y and obj refer to the same object:
y.Color = 'yellow';obj.Colorans =yellow
The function g modified the object referred to by the input argument (obj) and returned a handle to that object in y.
MATLAB Passes Handles by Value. A handle variable is a reference to an object. MATLAB passes this reference by value.
Handles do not behave like references in C++. If you pass an object handle to a function and that function assigns a different object to that handle variable, the variable in the caller is not affected. For example, suppose you define a function g2:
function y = g2(x) x = SimpleHandleClass('green'); y = x;end
Pass a handle object to g2:
obj = SimpleHandleClass('red');y = g2(obj);y.Colorans =greenobj.Colorans =red
The function overwrites the handle passed in as an argument, but does not overwrite the object referred to by the handle. The original handle obj still references the original object.
Common Object-Oriented Techniques
This table provides links to sections that discuss object-oriented techniques commonly used by other object-oriented languages.
Not supported. See persistent variables. For the equivalent of Java staticfinal or C++ static const properties, use Constant properties. See Properties with Constant Values
ConstructorClass Constructor MethodsCopy constructorNo direct equivalentReference/reference classesComparing Handle and Value ClassesAbstract class/InterfaceDefining Abstract ClassesGarbage collectionObject LifecycleInstance propertiesDynamic Properties — Adding Properties to an InstanceImporting classesImporting ClassesEvents and ListenersEvents and Listeners — Concepts- MatLab与其他面向对象语言的比较
- C#与其他语言的比较
- PHP与其他语言的比较
- 什么是JavaScript中的面向对象?与其他编程语言的面向对象有什么区别?什么是原型?
- Matlab与其他语言和软件的接口问题
- Visual C++与其他开发语言比较的优势
- LaTeX与其他编程语言的难易比较及分析
- Visual C++与其他开发语言比较的优势
- python中比较运算符与其他语言的差异
- python与其他语言进行比较
- wxWidgets与其他工具库的比较
- TC 与其他编译系统的比较
- TC 与其他编译系统的比较
- wxWidgets与其他工具库的比较
- wxWidgets与其他工具库的比较
- Java3D与其他三维技术的比较
- powerDesigner15与其他建模平台的比较
- wxWidgets与其他工具库的比较
- 云计算设计模式(十四)——实体化视图模式
- 一个C++菜鸟的成长史
- SQLite数据库实际运用
- 分享个测试接口好工具 Dev Http Client
- 截取部分屏幕
- MatLab与其他面向对象语言的比较
- Openstack 中的LoadBalance As A Service
- vim——打开多个文件、同时显示多个文件、在文件之间切换
- php能创建session, 但是不能写 session 的问题
- PLSQL WEBSERVICES 发布
- Substring with Concatenation of All Words
- libevent信号机制
- 【OpenStack】Grizzly中的LoadBalancer初步分析
- Linux内核剖析 之 内存管理