필터 지우기
필터 지우기

How to use an object property in other functions?

조회 수: 4 (최근 30일)
ana take
ana take 2018년 3월 9일
댓글: Guillaume 2018년 3월 11일
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!

채택된 답변

Guillaume
Guillaume 2018년 3월 9일
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
ana take
ana take 2018년 3월 10일
편집: ana take 2018년 3월 10일
_ 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 2018년 3월 11일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 3월 9일
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
ana take
ana take 2018년 3월 9일
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 2018년 3월 9일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Class Introspection and Metadata에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by