我有两门课,class1和class2。Class2继承了class1的所有属性,并具有一些附加属性,例如:
classdef class1
properties
PropA
end
methods
function instance1 = class1(arg1)
...
end
end
end
classdef class2 < class1
properties
PropB
end
methods
function instance2 = class2(arg2)
instance2 = instance2@class1(arg1)
...
end
end我调用class2的构造函数
instance2 = class2(arg2)然后想知道instance2的哪些属性是继承的(即PropA)。
是否可以确定class2的哪些属性是继承的,而不需要创建class1实例并随后比较属性?如果是的话,怎样才能有效地做到这一点呢?
发布于 2015-01-06 17:01:25
您可以通过元类确定此信息。在这种情况下,要查找继承的class2属性(例如,未由class2定义):
mcls = ?class2;
allProperties = mcls.PropertyList;
definedByClass2 = [allProperties.DefiningClass] == mcls;
inheritedProperties = allProperties(~definedByClass2);
propertyNames = {inheritedProperties.Name}请注意,您不需要任何类的实例来确定此信息。
https://stackoverflow.com/questions/27802945
复制相似问题