フィルターのクリア

Can I Dynamically Overload A Class Method?

13 ビュー (過去 30 日間)
Matthew
Matthew 2019 年 7 月 22 日
編集済み: Matthew 2019 年 7 月 29 日
Hi All,
Context: I'm attempting to overload a method of class without touching the class definition or explicitly declaring a subclass.
Question: Is it possible to dynamically overload a class method either by directly overloading, dynamically creating a subclass, or intercepting method calls?
Prior Work:
The odd error message produced by the following example gives me a dim hope that its possible directly . . .
Here's an example class
classdef Hello
methods
function sayHi(obj)
fprintf('Hi\n')
end
end
end
I then attempt to dynamically override the sayHi function, producing an error as follows:
helloObj = Hello;
sayHola = @(obj) fprintf('Hola\n')
helloObj.sayHi = sayHola;
Assignment not supported because the result of method 'sayHi' is a temporary value.
Is there a way to make "sayHi" produce a non temporary value? Or is that saying that @helloObj.sayHi is itself a temporary value?

採用された回答

Vimal Rathod
Vimal Rathod 2019 年 7 月 29 日
A class function cannot be dynamically overloaded without creating subclass. The class protects its methods from getting modified or overloaded from outside as that changes the class definition.
In the given code,
helloObj = Hello;
sayHola = @(obj) fprintf('Hola\n')
helloObj.sayHi = sayHola;
HelloObj.sayHi is a temporary value as it is just an instance of the class and was not defined in the class declaration and thus it gets its own temporary copy of all properties and cannot overload its class functions.
  3 件のコメント
Guillaume
Guillaume 2019 年 7 月 29 日
I'm surprise that javascript lets you do that. Doesn't improve my opinion of the language.
The whole idea is completely antithetical to encapsulation in OOP, where the class is in control of its own method, so expect the fact you can't do that to be the norm rather than the exception, in languages that implement OOP.
Matthew
Matthew 2019 年 7 月 29 日
編集済み: Matthew 2019 年 7 月 29 日
Its frowned upon for javascript too, but interceptors are actually a fairly standard way to implement AOP. Normally you would set yourself up for this though rather than trying to jury rig something after the fact.
Edit: Also you don't need to touch the prototype in javascript to create interceptors.

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2019 年 7 月 29 日
編集済み: Matt J 2019 年 7 月 29 日
You can't dynamically overload a class method, but you can get the same effect by making function handle properties that dictate the method's behavior. For example,
classdef Hello
properties
greeting=@() fprintf('Hi\n');
end
methods
function sayHi(obj)
obj.greeting();
end
end
end
and now you can make dynamic changes, like
>> helloObj = Hello; helloObj.sayHi
Hi
>> helloObj.greeting = @()fprintf('Hola\n'); helloObj.sayHi
Hola
  4 件のコメント
Matt J
Matt J 2019 年 7 月 29 日
編集済み: Matt J 2019 年 7 月 29 日
Too bad, but maybe if you elaborate on the constraints of the situation, better recommendations can be made. Why exactly can't the class be modified or subclassed? What about a container class:
classdef HelloContainer
properties
HelloObj
end
methods
function obj = HelloContainer(HelloObj)
obj.HelloObj=HelloObj;
end
function sayHi(obj)
fprintf('Hola\n')
end
end
end
Matthew
Matthew 2019 年 7 月 29 日
編集済み: Matthew 2019 年 7 月 29 日
Long story short, the constraints are/were non-technical, but require that:
1) The changes need to be made in the code where the object is initialized.
2) Only inline code changes can be made.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by