What exactly are Handle Compatible Classes? Are they a combination of handle and non-handle classes?

6 次查看(过去 30 天)
I read Handle Compatible Classes, the page say"Typically, when deriving a MATLAB® class from other classes, all the superclasses are handle classes, or none of them are handle classes. However, there are situations in which a class provides some utility that is used by both handle and nonhandle subclasses. Because it is not legal to combine handle and nonhandle classes, the author of the utility class must implement two distinct versions of the utility.
The solution is to use handle-compatible classes. You can use handle-compatible classes with handle classes when forming sets of superclasses. Designate a class as handle compatible by using the HandleCompatible class attribute."
I know “The HandleCompatible attribute is not inherited.”
I ran the following code,I found that the objects defined by A do not belong to any class! Why is this happening?
ta=A;
ty=isa(ta,'handle')
ty=isa(ta,'value')
ty=isa(ta,'HandleCompatible')
I ran the following code,tb do not belong to any class either! Why?
tb=B;
ty=isa(tb,'handle')
ty=isa(tb,'value')
ty=isa(tb,'HandleCompatible')
I ran the following code.Why can't I define objects for class C? I don't quite understand this error message.
c=C;
Error using C
If a class defines super-classes, all or none must be handle classes.
I have defined three classes, and the code is as follows:
classdef (HandleCompatible=true) A
properties
a
end
end
classdef B < A
properties
b
end
end
classdef C < B & handle
properties
c
end
end

采纳的回答

Matt J
Matt J 2023-8-28
编辑:Matt J 2023-8-28
I found that the objects defined by A do not belong to any class! Why is this happening?
The syntax of of isa() is,
isa(object,classname)
but 'value' and "HandleCompatible' are not the names of classes. You cannot query whether an object is a value or handle compatible class this way.
Why can't I define objects for class C?
The handle compatibility of A is not inherited by B, see Handle Compatibility Rules
  8 个评论
fa wu
fa wu 2023-8-30
B is value class(because “The HandleCompatible attribute is not inherited. )
So C is an illegal class because value classes and handle classes are not compatible. Am I correct in my understanding?
classdef C < B & handle
properties
c
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Subclass Definition 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by