implementing a set method for value classes (non-handle classes) in matlab

Hadi

suppose you have a value classes (and you want it to be a value class and not a handle class) the question is : how to implement a set property in this value class to to do some work in time of setting new value?

suppose we have the following code

classdef value_class
     properties
            x=0
     end
     methods
            function obj=set_x(obj,v)
                 %do the stuff
                 if v>5
                    obj.x=v;
                 end 
            end
     end
 end

if you test this class, will see the following :

>a=value_class;
>a.x=2;
>a
 a = 
 value_class with properties:
 x: 2

which is correct (but our criteria of x>5 not checked) and then

  >a.set_x(3);
  > a
  a = 
  value_class with properties:
  x: 2

which is obviously has no effect on "a"!. in this case matlab sends a copy of "a" to function set_x() thus the actual "a" does not change.

so if you write your own set function to do some stuff before assigning a value to property of a value class then you can not use that function to change the value of its caller object!

then what is the true way of implementing a set function for a value class?

Hadi

Matlab has a default set method which we can use as a set function for our value (or non value) classes

classdef value_class
     properties
            x=0
     end
     methods
            function obj=set.x(obj,v)
                 %do the stuff
                 if v>5
                    obj.x=v;
                 end 
            end
     end
 end

then if we test this class:

> a=value_class
a = 
value_class with properties:
x: 0
> a.x=2
a = 
value_class with properties:
x: 0
> a.x=6
a = 
value_class with properties:
x: 6

in this case when you try to set new value to a , its set function runs and check the value in our criteria. (to overloading other operators)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Override method for a collection of classes implementing an interface

From Dev

Avoid implementing method twice (Classes already extending abstract classes)

From Dev

Understanding clone method for non-final classes

From Dev

Understanding clone method for non-final classes

From Dev

interface method that does not make sense for all implementing classes

From Dev

Implementing Classes Java

From Dev

Implementing Interface and Autoloading Classes

From Dev

Implementing custom classes

From Dev

Implementing Abstract Classes in Scala

From Dev

Matlab - Combining enumeration classes with non-static methods

From Dev

Matlab - Combining enumeration classes with non-static methods

From Dev

What is a smart solution to handle a list of different classes implementing an interface in this Spring Boot application?

From Dev

Implementing classes that should behave as Optional

From Dev

Implementing angularjs directives as classes in Typescript

From Dev

Type Classes When Implementing HFoldr

From Dev

Implementing Singleton as metaclass, but for abstract classes

From Dev

Implementing Singleton as metaclass, but for abstract classes

From Dev

Creating and Implementing Classes in C++

From Dev

Implementing dependency injection with parent classes?

From Dev

In a non static class ,How do I retain the value of a static variable used in a static method called in multiple different classes simultaneously

From Dev

How to assign different labels to a set of classes using Matlab?

From Dev

importing scala classes into matlab?

From Dev

Why can't Java find a suitable method when the input types are interfaces and the parameter types are implementing classes?

From Dev

How do I know at runtime which implementing classes are set for my Spring beans?

From Dev

How to modify method return value in all derived classes?

From Dev

In a generic method, how to pass in classes based on conditions and capture return value

From Dev

Validity Method for Reference Classes

From Dev

Is there a standard method for naming classes?

From Dev

Print method with user classes

Related Related

  1. 1

    Override method for a collection of classes implementing an interface

  2. 2

    Avoid implementing method twice (Classes already extending abstract classes)

  3. 3

    Understanding clone method for non-final classes

  4. 4

    Understanding clone method for non-final classes

  5. 5

    interface method that does not make sense for all implementing classes

  6. 6

    Implementing Classes Java

  7. 7

    Implementing Interface and Autoloading Classes

  8. 8

    Implementing custom classes

  9. 9

    Implementing Abstract Classes in Scala

  10. 10

    Matlab - Combining enumeration classes with non-static methods

  11. 11

    Matlab - Combining enumeration classes with non-static methods

  12. 12

    What is a smart solution to handle a list of different classes implementing an interface in this Spring Boot application?

  13. 13

    Implementing classes that should behave as Optional

  14. 14

    Implementing angularjs directives as classes in Typescript

  15. 15

    Type Classes When Implementing HFoldr

  16. 16

    Implementing Singleton as metaclass, but for abstract classes

  17. 17

    Implementing Singleton as metaclass, but for abstract classes

  18. 18

    Creating and Implementing Classes in C++

  19. 19

    Implementing dependency injection with parent classes?

  20. 20

    In a non static class ,How do I retain the value of a static variable used in a static method called in multiple different classes simultaneously

  21. 21

    How to assign different labels to a set of classes using Matlab?

  22. 22

    importing scala classes into matlab?

  23. 23

    Why can't Java find a suitable method when the input types are interfaces and the parameter types are implementing classes?

  24. 24

    How do I know at runtime which implementing classes are set for my Spring beans?

  25. 25

    How to modify method return value in all derived classes?

  26. 26

    In a generic method, how to pass in classes based on conditions and capture return value

  27. 27

    Validity Method for Reference Classes

  28. 28

    Is there a standard method for naming classes?

  29. 29

    Print method with user classes

HotTag

Archive