Info

此问题已关闭。 请重新打开它进行编辑或回答。

How can i acess to created objects out of the class

1 次查看(过去 30 天)
Hello. I have a simple Class:
classdef Packung
properties
preis
menge
l
end
methods
function this = Packung(p,m,l)
this.preis = 0;
this.menge = 0;
this.l=l;
%this = this.setPreisMenge(p,m);
end
function this = setPreisEinkaufswagen(this, p)
xxx = this.preis;
display(xxx);
% Objekt2 = setPreisMenge(Objekt2, 2, p);
end
function [this,objekt] = setPreisMenge(this,objekt, p,m)
this.preis = p;
this.menge = m;
objekt.preis = objekt.preis + 100;
end
end
end
now i have another function, in which i want to make a for loop, going through all the objects created from my Class Packung. How to do this?
I tired following: Function PriceProducts is NOT in the Class. Before i started the Function, i made a object from the class: Milch = Packung (1,2,3)
function PriceProducts()
for i=1:10
Product_buying= Milch;
...
end
Though i get an error, Undefined function or variable 'Milch'.
How to manage this? I'd like to predifine all the Objects that need to be used, and then i want to loop them.

回答(2 个)

Erik S.
Erik S. 2015-2-8
Don't you have to pass the "Milch" variable to the function PriceProducts? I don't see how the function gets this value otherwise?

Fil Dim
Fil Dim 2015-2-8
编辑:Fil Dim 2015-2-8
Hi Erik. Thanks for your responce.
I want to make a loop through all the Packung Objects. If i make:
PriceProducts(Milk)
for i=1:10
if i = 1
Product_buying= Milk;
elseif i=2
Product_buying = Banana;
...
end
I still have 200 More Products in there. I mean, i can make my function having 200 variables, but i guess there needs to be a smarter way?

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by