modify an object of a specific class within another class
4 次查看(过去 30 天)
显示 更早的评论
Hello, I have the Cryptocurrency class having two simple properties and described as follows:
% Cryptocurrency is a class whose objects represent different
% cryptocurrencies.
classdef Cryptocurrency
% Property of the Cryptocurrency class.
% Only the market class can modify the attribute.
properties (GetAccess = public, SetAccess = {?Market})
price (1,1) double {mustBeNonnegative} = 0
end
% The initialSupply property is a constant and therefore can only
% be modified by the constructor.
properties (GetAccess = public, SetAccess = immutable)
initialSupply (1,1) double {mustBePositive} = 1
end
% methods of the Cryptocurrency class.
methods
% Constructor of the Cryptocurrency class. Takes the initial price
% and the inizial supply of the cryptocurrency as input.
function obj = Cryptocurrency(inputPrice, inputSupply)
obj.price = inputPrice;
obj.initialSupply = inputSupply;
end
end
end
Then, I have the Market class which has the task of changing the price of the two cryptocurrencies following a sale. The Market class takes as input two objects of the Cryptocurrency class :
% this class simulates the price dynamics of two cryptocurrencies based on
% the rules of Automated Market Makers.
% The class allows you to buy token A by paying a certain amount of
% token B and vice versa.
classdef Market
% These properties can only be changed by the constructor.
% - assetA and assetB represent the two cryptocurrencies whose price
% dynamics are simulated by this class.
% - supplyA and supplyB indicate the quantity of the two tokens
% in the liquidity pool.
properties (GetAccess = public, SetAccess = private)
assetA (1,1) Cryptocurrency = Cryptocurrency(1,1)
assetB (1,1) Cryptocurrency = Cryptocurrency(1,1)
supplyA (1,1) double {mustBeNonnegative} = 0
supplyB (1,1) double {mustBeNonnegative} = 0
end
% - k is the constant used in AMMs based on the law k = x * y, where x
% is the initial amount of token A and y is the initial amount of
% token B. The value of k must remain constant.
properties (GetAccess = public, SetAccess = immutable)
k (1,1) double {mustBePositive} = 1
end
methods
% Constructor of the Market class. It takes as input two objects
% of Cryptocurrency class
function obj = Market(cryptoA, cryptoB)
arguments (Input)
cryptoA (1,1) Cryptocurrency
cryptoB (1,1) Cryptocurrency
end
obj.assetA = cryptoA;
obj.assetB = cryptoB;
% Formula used in AMMs. k must remain constant.
obj.k = cryptoA.initialSupply * cryptoB.initialSupply;
% the supply in the AMM is not the same as that of the crypto
% in the market.
obj.supplyA = cryptoA.initialSupply;
obj.supplyB = cryptoB.initialSupply;
end
% method that allows you to buy assetA by providing assetB in exchange.
% To buy a quantity of assetA, a quantity of assetB must be
% deposited in the pool.
% In this way the price of assetA increases as there is less
% availability in the pool and the price of assetB decreases as
% there is a greater quantity.
% The method takes as input the quantity of token A to be purchased.
function obj = buyCryptoA(obj, transactionVolume)
arguments (Input)
obj (1,1) Market
transactionVolume (1,1) double {mustBeNonnegative}
end
% An amount of tokenA greater than the amount of tokenA in the
% pool cannot be purchased.
if transactionVolume >= obj.supplyA
disp("The transaction cannot be executed.")
else
% The amount of token A in the pool decreases.
obj.supplyA = obj.supplyA - transactionVolume;
% Amount of tokenB sold to get the amount of tokenA
% specified by transactionVolume
tokenBsold = abs(obj.supplyB - (obj.k / obj.supplyA));
% The amount of token B varies so that k remains constant.
obj.supplyB = obj.k / obj.supplyA;
% The price of token A rises as it becomes scarcer.
obj.assetA.price = (tokenBsold * obj.assetB.price) ...
/ transactionVolume;
% The price of the token B drops as there is more of it
obj.assetB.price = (obj.assetA.price * obj.supplyA) ...
/ obj.supplyB;
end
end
% method that allows you to buy assetB by providing assetA in exchange.
% To buy a quantity of assetB, a quantity of assetA must be
% deposited in the pool (sold).
% In this way the price of assetB increases as there is less
% availability in the pool and the price of assetA decreases as
% there is a greater quantity.
% The method takes as input the quantity of token A to be sold.
function obj = sellCryptoA(obj, transactionVolume)
arguments (Input)
obj (1,1) Market
transactionVolume (1,1) double {mustBeNonnegative}
end
% % The amount of token A in the pool increases.
obj.supplyA = obj.supplyA + transactionVolume;
% Amount of tokenB obtained by selling the amount of tokenA
% specified by transactionVolume
tokenBobtained = abs(obj.supplyB - (obj.k / obj.supplyA));
% The amount of token B varies so that k remains constant.
obj.supplyB = obj.k / obj.supplyA;
% The price of token B rises as it becomes scarcer.
obj.assetB.price = (transactionVolume * obj.assetA.price) / tokenBobtained;
% The price of the token A drops as there is more of it.
obj.assetA.price = (obj.assetB.price * obj.supplyB) / obj.supplyA;
end
end
end
The price of the two properties inside the Market class assetA and assetB is modified correctly.
But how do I change the price property of the two objects that I pass to the Market constructor? For example :
a = Cryptocurrency(1, 1000);
b = Cryptocurrency(0.5, 2000);
m = Market(a, b);
m = m.sellCryptoA(100);
In this example, I'd like the price of item A to change, but it doesn't. So the constructor is passed a copy of a and b? How do I pass the reference of objects a and b so that any modification of them inside the Market class is reflected on the two objects passed to the constructor?
0 个评论
回答(1 个)
Harsh Mahalwar
2023-5-26
编辑:Harsh Mahalwar
2023-5-26
Hi Francesco,
One way of changing the value of a and b whenever the value of m.assestA and m.assestB is changed can be to create this function inside the class:
function res = func(obj)
res.a = obj.assestA;
res.b = obj.assestB;
end
Then this function can be called to excitly change the values of a and b respectively by:
res = func(obj);
a = res.a;
b = res.b;
Hope this helps! Here are some resources for reference:
Thanks and regards,
Harsh Mahalwar
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Methods 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!