Matlab static variable that can be modified by super class and sub class?
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Sohil Shrestha
 el 12 de Oct. de 2021
  
I have following structure where I have a superclass and two subclasses
classdef A 
    properties
        staticvar
    end
end
    And two subclasses of A
classdef B < A
    properties
    end
end
classdef C < A
    properties
    end
end
Is there a way I can use "staticvar" such that the "staticvar" value can be modified by any of the classes and its value is reflected when accessed by any of the class?
0 comentarios
Respuesta aceptada
  Matt J
      
      
 el 13 de Oct. de 2021
        
      Editada: Matt J
      
      
 el 13 de Oct. de 2021
  
      You mean you want all instances of the class and  its subclasses to share the same copy of staticvar? If so, you can write the superclass like this.
classdef A 
    properties(Dependent)
        staticvar
    end
    methods 
        function val=get.staticvar(~)
           val=manage_staticVar;
        end
        function obj=set.staticvar(obj,val)
           manage_staticVar(val);
        end
    end
end
function out=manage_staticVar(val) %Not a class method - can be class-related function
 persistent p
 if nargin, p=val; end
 out=p;
end
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Class Introspection and Metadata en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!