Is constructor method the only method that creates an object in a class definition?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Steven Lord
2017년 12월 11일
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.
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!