Can I find out if a class is an abstract class before trying to instantiate it?
7 次查看(过去 30 天)
显示 更早的评论
Can i somehow say:
if isAbstract(className)
% do something
else
% do something else
end
0 个评论
采纳的回答
per isakson
2012-7-17
编辑:per isakson
2012-7-17
A class is abstract if any of its properties or methods is abstract.
It's possible to write a function, is_abstract. Start:
mco = ?my_class
mco.PropertyList
mco.MethodList
any([mco.MethodList.Abstract])
.
or better
mcls = meta.class.fromName('ClassName')
3 个评论
Daniel Shub
2012-7-18
@Walter, but that "hidden" abstract method would be abstract and eventually caught. The metaclass object has access to all methods and properties (even private ones), so I do not think it needs to be recursive.
更多回答(4 个)
Image Analyst
2012-7-17
I don't think so. But you should know just by looking at the code, or just try it and see. Why would you ever not know what you're calling and have to use the test you you described? Worst case, you could wrap your method call in a try/catch to do what you want, but I would recommend knowing beforehand than doing that.
0 个评论
Daniel Shub
2012-7-18
It might be quicker to try and create the object in a try-catch and deal with errors when they happen.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Class Introspection and Metadata 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!