필터 지우기
필터 지우기

Different class definitions for value classes and a handle classes?

조회 수: 1 (최근 30일)
David Devoy
David Devoy 2017년 3월 23일
댓글: Adam 2017년 3월 23일
Is there a difference in the class definition header for handle classes and value classes?
Also I have heard that the behaviour of handle classes is easier to understand for those familiar with Java. Is this true?

채택된 답변

Adam
Adam 2017년 3월 23일
편집: Steven Lord 2017년 3월 23일
classdef MyClass < handle
is the obvious difference, but also your methods which make changes to the object of the class must return the object as an output argument if you are programming a value class (the default) e.g.
function obj = doSomething( obj )
and you must reassign back to the object.
I'm not a java programmer so I can't really answer that part, but handle classes work on a 'pass by reference' paradigm, value classes require this reassignment behaviour because they use more Matlab standard approach of copy by value. As a C++ programmer originally I certainly miss the lack of things like const references. A handle class passes around always like a non-const reference - as far as I am aware there is nothing that can change that, though obviously making properties private helps limit the possibilities for bugs as it always does.
There are other differences, handle classes can use events, for example, but these are covered in the documentation and the huge pdf that you can learn as you go along.
I use far more handle classes than value classes, but both can sting you if you don't use them correctly.
[SL: fixed typo. The definition of doSomething had an extra = between function and obj.]

추가 답변 (1개)

David Devoy
David Devoy 2017년 3월 23일
Thanks for you answer.
Yes, having just about got my head round OO in Java I found it weird that "methods which make changes to the object of the class must return the object as an output argument" in Value classes.
I have done quite a bit of C++ but never got into OO in C++. However it does seem similar to Java. I am finding my Java experience to be quite useless, in fact almost a hindrance tith Matlab OO.
  댓글 수: 1
Adam
Adam 2017년 3월 23일
Yes, Matlab OOP is very much its own beast. Obviously there are high level similarities with other languages (probably other scripting languages like Python are closer, but again I don't program in Python and have only looked at small amounts of code in it).
When I first learned OOP I used that bug pdf document a lot and started out with a nicely self-contained problem where I was clear what class(es) I wanted to help me familiarise.
Having to return obj is logical, but not intuitive for a C++/Java programmer. There is no reason why you have to assign this returned obj back to the one you made the call on - it is copy-by-value after all so you can assign it to a totally new copy if you wish to keep the original prior to the function call for some reason. I do this occasionally though quite rarely and mostly for functions that are explicitly spawning a copy of the object.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Java Package Integration에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by