Creation of multiple objects in same class

If any class is defined then how I can create different objects of the same class. Answers with suiable examples are appreciated. Thanks

 Respuesta aceptada

It depends. Is that class a handle class or a value class? If value, just call the constructor or do whatever you need to instantiate an instance. In this example A and B are both table arrays but modifying one doesn't modify the other. Even after I change one of the elements of B the corresponding element of A is unchanged.
A = array2table(magic(4))
A = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
B = A;
B{2, 'Var3'} = NaN
B = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 NaN 8 9 7 6 12 4 14 15 1
A
A = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
If a handle class, it can be more difficult unless the class wants you to be able to copy it.

3 comentarios

VIJAYKUMAR KAMBLE
VIJAYKUMAR KAMBLE el 27 de Abr. de 2021
Sir I am very much thankful to you.
Sir If Suppose I have created a class of Round Pin and now I want to create the different objects in it like
CLM_60_RP ,CLM_80_RP and on till CLM_200_RP then how should I create ??
I request you to please create two objects, for rest i will follow the same procedure as you have given.
Thanking for your support !.
If you're asking if you can create variables named CLM_60_RP, CLM_80_RP, etc. you can do this but you shouldn't.
I'd make a struct array or a table array to contain your objects instead.
pins.CLM_60_RP = 42 % Using a hard-coded field name
pins = struct with fields:
CLM_60_RP: 42
pinname = 'CLM_80_RP';
pins.(pinname) = -99 % Using a dynamic field name
pins = struct with fields:
CLM_60_RP: 42 CLM_80_RP: -99
y = pins.CLM_60_RP % Retrieve data the same way
y = 42
z = pins.(pinname)
z = -99
VIJAYKUMAR KAMBLE
VIJAYKUMAR KAMBLE el 27 de Abr. de 2021
Thank You very much sir . I got my answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 27 de Abr. de 2021

Comentada:

el 27 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by