Filter löschen
Filter löschen

How to use an object property in other functions?

4 Ansichten (letzte 30 Tage)
ana take
ana take am 9 Mär. 2018
Kommentiert: Guillaume am 11 Mär. 2018
Hi all! I have a property named _ techn_ which is a Technology object(Technology is a class in the package datatypes).In the class constructor PathLossCalculator(which take as parameter two objects: Technology and Area object) I have asigned the value of techn object as shown in the figures below.
The property techn_ is used in the function getMaxPathLoss() to call the methods of the Technology class.My question is: Is the techn property used ok to call the Technology methods(techn.getFrequency()) or should I use: obj.techn.getFrequency() and give the _obj as the parameter of the function getMaxPathLoss(obj,bs,rxSNR,ip)?
Note:Attached figures can help to ilustrate the explanation of the problem Thanks in advance!

Akzeptierte Antwort

Guillaume
Guillaume am 9 Mär. 2018
If geMaxPathLoss is a method of the class PathLossCalculator (and it has to be in order to access a private property), then the proper definition is indeed:
function MaxPathLoss = getMaxPathLoss(this, bs, rxNR, ip)
vpl = 0;
if this.getUserspeed > 0
if this.techn.getFrequency < 750, vpl = 14.7;
elseif ...
replace this by obj if you prefer.
Note: Prefer to post code as text rather than image as it makes our life harder if it's an image. We can't copy/paste code out of an image
Note 2: matlab is not C. You don't need to enclose if conditions in () and the proper way to write an if condition on one line is as I've done above.
Note 3: In any case, a better way to write this if ... elseif ... would be:
vplvalues = [14.7, 12.4, 8.7, 13.7];
vpl = vplvalues(discretize(this.techn.getFrequency, [-Inf, 750, 1200, 2000, Inf]));
  2 Kommentare
ana take
ana take am 10 Mär. 2018
Bearbeitet: ana take am 10 Mär. 2018
_ Thank you for your answer and for your suggestions.I am a java user. this is not a keyword in this case?
So whenever I have a property which is an object of other class and this property is asigned in thee constructor of the class being used, I should use an object of that class as a parameter of every function that uses that object property.Right?
I have read the matlab documentation for OOP but can you suggest any book for matlab OOP?
Guillaume
Guillaume am 11 Mär. 2018
In C++, and I'm fairly certain in Java as well, the object a class method acts on is implicitly the first argument of the method. This is added for you by the compiler. The language does provide a keyword for you to access that argument, the this keyword.
Matlab does not do this for you, so you have to explicitly have the object as the first argument. You can name it any way you want. I like to use this because that's what I'm used to in C++ but you can name it anything you want, but this is not a keyword.
I'm not sure I understand "whenever I have a property which is an object of other class and this property is asigned in thee constructor of the class being used". This as nothing to do with properties or constructor. It's simply that (non-static) member functions must have an argument that is the class instance.
I learnt all my matlab OOP from the documentation but had years of experience programming in C++, then C#. I think the OOP doc is badly organised, many related topics are scattered through several pages, but it does contain all the required information.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 9 Mär. 2018
I'm not sure if techn is a Technology object, like you said first, or a property like you said in the second paragraph.
I believe if the methods line declares it static, you can use it without instantiating an object:
methods(Static)
To call a static method, prefix the method with the class name:
theSpeed = Technology.getUserspeed();
freq = Technology.getFrequency();
You don't need an instantiated object to call them. Otherwise you'll have to pass an instantiated object to whatever function, like getMaxPathLoss(), that you want to use it in.
  2 Kommentare
ana take
ana take am 9 Mär. 2018
It's a property but unlike ordinary properties it's not of type double but it's of type Technology.The constructor is something like:PathLossCalculator(Technology techn, Area area) so when I asign obj.techn=techn the property becomes a Technology Object.
Guillaume
Guillaume am 9 Mär. 2018
The method cannot be static as it accesses an object property. It has to be a standard method of the class in order to access object properties.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Class Introspection and Metadata 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!

Translated by