How can I determine if one class is superior or inferior to another?

4 vues (au cours des 30 derniers jours)
Matt Bauman
Matt Bauman le 4 Oct 2013
Commenté : Matt J le 8 Oct 2013
Matlab uses class precedence to determine which methods are called. Given two class names, is it possible to determine the class precedence relationship between the two? The new metaclass structure contains some of this information for classdef classes, but lacks information about older-style classes and fundamental types.
I'd love to find (or be able to write) a generic utility that simply asks is 'class_a' superior to 'class_b'? Is this possible?

Réponse acceptée

Matt J
Matt J le 4 Oct 2013
Modifié(e) : Matt J le 4 Oct 2013
Instead of having 2 class names, you can do it if you have 2 objects of the class using something like the function below. Moreover, if your class constructors all support the no-argument case, you could generate an object from a class name as follows
obj=feval(className)
but I don't know if this would always be true for you.
function tf=issuperior(A,B)
%True if class(A) superior to class(B). Otherwise false.
try
stupidname_neverused(A,B);
catch ME
end
name=strrep(ME.message,'Undefined function ''stupidname_neverused'' for input arguments of type ','');
name=strrep(name,'''','');
name(end)='';
tf=strcmp(name,class(A));
  6 commentaires
Matt Bauman
Matt Bauman le 8 Oct 2013
Yes, I was simply clarifying that the precedence is a function of both class and argument position. I was surprised by how many of the built-in types aren't at equal precedence... I don't think this is documented anywhere.
Matt J
Matt J le 8 Oct 2013
I'm not sure what's led you to conclude that the built-ins aren't of equal precedence, but maybe the docs your looking for are
and also maybe

Connectez-vous pour commenter.

Plus de réponses (1)

Cedric
Cedric le 4 Oct 2013
Modifié(e) : Cedric le 5 Oct 2013
You could use property SuperclassList from the output of METACLASS recursively ..
Here is an example:
function tf = isSubclass( classObjMeta, superclassName )
tf = true ;
if ischar( classObjMeta ), classObjMeta = feval( classObjMeta ) ; end
if ~isa( classObjMeta, 'meta.class' )
classObjMeta = metaclass( classObjMeta ) ;
end
if strcmp( classObjMeta.Name, superclassName ), return ; end
for scId = 1 : length( classObjMeta.SuperclassList )
if isSubclass( classObjMeta.SuperclassList(scId), superclassName )
return
end
end
tf = false ;
end
Note that I just took 5 minutes to write that, so you'll want to stabilize or fine tune it. In particular, I leave it to you to modify it if you don't want the output to be true when you test a class with itself, .. or to ask users to pass an object as 1st arg if class doesn't support no-argument constructor (that should be tested in try/catch statement I guess).
To test it, define e.g.
classdef ClassA < handle
end
classdef ClassB < ClassA
end
and you'll have
>> isSubclass( 'ClassB', 'handle' )
ans =
1
>> isSubclass( 'ClassA', 'handle' )
ans =
1
>> isSubclass( 'ClassB', 'ClassA' )
ans =
1
>> isSubclass( 'ClassA', 'ClassB' )
ans =
0
  2 commentaires
Matt Bauman
Matt Bauman le 8 Oct 2013
Thanks for the answer, but I'm after class precedence, not subclass hierarchy.
Cedric
Cedric le 8 Oct 2013
Well, I should have taken 6 minutes instead of 5, and read completely the question!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by