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

4 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Matt J
Matt J am 28 Aug. 2023
Bearbeitet: Matt J am 28 Aug. 2023
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 Kommentare
fa wu
fa wu am 30 Aug. 2023
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Subclass Definition finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by