Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
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 commentaires
Steven Lord
Steven Lord le 11 Déc 2017
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.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 11 Déc 2017
Modifié(e) : Matt J le 11 Déc 2017
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 commentaire
Yves
Yves le 11 Déc 2017
Modifié(e) : Matt J le 11 Déc 2017
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"?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by