object orianted programing (OOP ) cant understand
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
hello I started to learn progrmaing withh oop becouse me coude became too long , now i have problems understandg how to use it correctly for exmpale i have this code :
classdef main_code_oop
properties
f;
U;C;C1;L2;L1;R_C;R_C1;R1;R2;R_diode;V_diode;Duty_cycle;alpha;
end
methods
function variable=main_code_oop()
variable.U=12;
variable.f=30e3;
variable.C=2200e-6;
variable.C1=470e-6;
variable.L1=1e-3;
variable.L2=1e-3;
variable.R_C=0.05;
variable.R_C1=0.1;
variable.R1=0.1;
variable.R2=0.1;
variable.R_diode=0.00000000000001;
variable.V_diode=0.3;
variable.Duty_cycle=62.5;
Duty_cycle=variable.Duty_cycle;
end
function [alpha]=Calculating_alpha(Duty_cycle)
alpha=(1-(Duty_cycle/100))/(Duty_cycle/100);
end
end
how can i see alpha in the command space ? and to cheak if i got the right answer ?
[SL: edited to apply code formatting]
0 Kommentare
Antworten (2)
Steven Lord
am 13 Dez. 2017
The Calculating_alpha method of your main_code_oop object must accept an instance of the object main_code_oop, not a numeric value like the value of one of the object's properties.
function alpha = Calculating_alpha(myObject)
end
Now inside the method you can access properties of the object.
function alpha = Calculating_alpha(myObject)
alpha=(1-(myObject.Duty_cycle/100))/(myObject.Duty_cycle/100);
end
If you want you can extract a property you're going to use repeatedly into a variable, so you don't have to fetch it from the object over and over again.
function alpha = Calculating_alpha(myObject)
DC = myObject.Duty_cycle;
alpha=(1-(DC/100))/(DC/100);
end
Now to call that method, instantiate (create) an instance of the object and pass it into the method.
q = main_code_oop;
A = Calculating_alpha(q)
0 Kommentare
OCDER
am 13 Dez. 2017
Here are some suggestions for improving the code. See the comments in %.
classdef main_code_oop < handle %make handle the superclass to allow you to modify properties like obj.alpha = 2.
properties (GetAccess = public) %public means outside command CAN modify these variables
f; %Write description
U; %Write description
C; %Write description
C1; %Write description
L2; %Write description
L1; %Write description
R_C; %Write description
R_C1; %Write description
R1; %Write description
R2; %Write description
R_diode; %Write description
V_diode; %Write description
Duty_cycle; %Write description
end
properties (GetAccess = private) %private means outside commands cannot modify alpha
alpha; %Write description
end
methods
function variable = main_code_oop()
variable.U=12;
variable.f=30e3;
variable.C=2200e-6;
variable.C1=470e-6;
variable.L1=1e-3;
variable.L2=1e-3;
variable.R_C=0.05;
variable.R_C1=0.1;
variable.R1=0.1;
variable.R2=0.1;
variable.R_diode=0.00000000000001;
variable.V_diode=0.3;
variable.Duty_cycle=62.5;
%Duty_cycle=variable.Duty_cycle; %This is unused
end
%- Instead of using LONG words like "Calculating_alpha", use short verb + Description.
% Makes life easier and it's the standard in OOP.
% EX: calcAlpha, getAlpha, setAlpha
%- Pass the object as input, not Duty_cycle
%function [alpha] = Calculating_alpha(Duty_cycle)
function [alpha] = calcAlpha(obj)
alpha = (1-(obj.Duty_cycle/100))/(obj.Duty_cycle/100);
obj.alpha = alpha; %Updates private property, alpha
end
%In case you want to get access to a private property
function [alpha] = getAlpha(obj)
alpha = obj.alpha;
end
end
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!