1
A
回答
2
metaclass
返回meta.class
對象,其中包含有關被查詢的類信息。此meta.class
對象的實用屬性是PropertyList
,其中包含有關該類的所有屬性(包括DefiningClass
)的信息。
使用下面的類定義爲例:
classdef asuperclass
properties
thesuperproperty
end
end
和
classdef aclass < asuperclass
properties
theclassproperty
end
end
現在,我們可以查詢的aclass
的屬性,以確定他們來自何處:
tmp = ?aclass;
fprintf('Class Properties: %s, %s\n', tmp.PropertyList.Name)
fprintf('''theclassproperty'' defined by: %s\n', tmp.PropertyList(1).DefiningClass.Name)
fprintf('''thesuperproperty'' defined by: %s\n', tmp.PropertyList(2).DefiningClass.Name)
其中返回:
Class Properties: theclassproperty, thesuperproperty
'theclassproperty' defined by: aclass
'thesuperproperty' defined by: asuperclass
你可以把它包裝成一個簡單的幫助函數。例如:
function classStr = definedby(obj, queryproperty)
tmp = metaclass(obj);
idx = find(ismember({tmp.PropertyList.Name}, queryproperty), 1); % Only find first match
% Simple error check
if idx
classStr = tmp.PropertyList(idx).DefiningClass.Name;
else
error('Property ''%s'' is not a valid property of %s', queryproperty, tmp.Name)
end
end
相關問題
- 1. 繼承的屬性檢查
- 2. 在C#中繼承的方法屬性?
- 3. 搜索由繼承者使用的繼承類的方法/屬性
- 4. 抽象類繼承另一個類的方法和屬性
- 5. 類圖 - 屬性繼承
- 6. 繼承類屬性(python)
- 7. Python類 - 屬性繼承
- 8. Python類屬性繼承
- 9. Python - 繼承類方法來操作類屬性
- 10. 繼承屬性
- 11. 更改繼承屬性的類型(到繼承類型)
- 12. C#中虛擬基類方法/成員的屬性繼承
- 13. Zend文檔:如何查看繼承的方法和屬性
- 14. 在matlab中查找直方圖屬性
- 15. Rails - Citier - 兩級繼承,子類不繼承中間類屬性
- 16. 查看繼承的元素的繼承CSS屬性
- 17. 從基類繼承Grails域類屬性
- 18. C#繼承父類的屬性
- 19. ES6中的屬性繼承JS類
- 20. 如何獲取屬性的繼承類
- 21. Ruby類中的可繼承屬性
- 22. CSS繼承不父類的屬性
- 23. getStaticProperties返回繼承的類屬性
- 24. 繼承的類不具有屬性?
- 25. 基類和繼承的靜態屬性
- 26. 繼承css屬性
- 27. 爲繼承屬性
- 28. 繼承CSS屬性
- 29. JPQL:訪問繼承屬性的查詢
- 30. 使用ActiveRecord在派生類中查找方法 - 單表繼承