Is constructor method the only method that creates an object in a class definition?

Another way to ask this question is: how do I create a value class object as the output of a handle class ordinary method? For example,
classdef handleClassA < handle
properties
end
methods
function obj = handleClassA(varargin)
...
end
function ValueClassobj2 = unknownmethod(handleClassAobj, ValueClassobj1)
ValueClassobj2.property1 = ...
some_sort_function(handleClassAobj.property3, ValueClassobj1.property1);
...
end
end
end
If I do it the above way, the output is a struct with field property1, etc., not a object of a class.

6 件のコメント

Adam
Adam 2017 年 12 月 11 日
Any function can create an object, the constructor is a special case that creates the current object. You can create new objects in the function of an existing object, you just obviously can't create the object itself, which already exists and can only be created in the constructor.
As per MattJ's answer, you still need to create an actual object as you would wherever you are doing it, if you want an object to be the result. The fact you are in a method of one class rather than a standalone function makes no difference to the creation of a 2nd class of a totally different type (or even the same type).
Simply assigning something to a variable that does not exist yet using . (dot) notation will create always create a struct, with the one exception, of within a class constructor where the thing being returned will automatically be an object of the class and thus can be assigned to without being explicitly created (which would be very circular, in a constructor!).
Yves
Yves 2017 年 12 月 11 日
I see. Are there other ways than "cloning", to create the output object of a new class, in the current class definition?
Sure. Just have that method call the constructor for a different class (or a function that returns an instance of the class) and return the class instance. For instance if you had a method createDigraph in a class myHandleClass:
classdef myHandleClass < handle
methods
function y = createDigraph(obj)
% Requires release R2015b or later
y = digraph(bucky);
end
end
end
That's perfectly legal, though you'd probably want to use one or more of the properties of obj in constructing the digraph object. Otherwise what would be the point of making this a method of the class myHandleClass?
q = myHandleClass;
d = createDigraph(q);
class(d)
Yves
Yves 2017 年 12 月 11 日
Yeah, I got your point. Let me make sure I understand you correctly, if you don't mind to confirm that: in your example, "digraph" is a constructor method for the class "y" to be an object. The method "createDigraph" is a method/function wrap around "digraph". Correct?
Steven Lord
Steven Lord 2017 年 12 月 11 日
digraph is the constructor for the digraph class included in MATLAB since release R2015b.
In this simple example createDigraph only calls the constructor for the digraph class with a sample matrix returned from the bucky function (also included in MATLAB.)
In a "real world" scenario the myHandleClass object would probably have properties that the createDigraph method would use to compute the matrix with which you call the digraph constructor.
Yves
Yves 2017 年 12 月 11 日
Thx, Steven.

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2017 年 12 月 11 日
編集済み: Matt J 2017 年 12 月 11 日
I think you are trying to do,
function ValueClassobj2 = unknownmethod(handleClassAobj, ValueClassobj1)
ValueClassobj2 = ValueClassobj1; %clone of obj1
ValueClassobj2.property1 = ...
some_sort_function(handleClassAobj.property3, ValueClassobj1.property1);
...
end

1 件のコメント

Yves
Yves 2017 年 12 月 11 日
編集済み: Matt J 2017 年 12 月 11 日
Matt, if I understand you correctly, the statement
ValueClassobj2 = ValueClassobj1;
creates the object "ValueClassobj2", in the same class as "ValueClassobj1". Correct?
What if I don't have a same class object as "ValueClassobj2" that I wanted to create, but an array of input variables? Will I still be able to define "unknownmethod" as a method of currect class "handleClassAobj", to create the object "ValueClassobj2"?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeClass Introspection and Metadata についてさらに検索

質問済み:

2017 年 12 月 11 日

コメント済み:

2017 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by