重载方法取决于调用实例

我真的是编程新手,因此一直认为我可以编写更简洁更简单的代码。

我想知道,是否有一种方法可以实现不同的行为,具体取决于调用方法的实例?

例如:

param = worker.CreateWindow(elemParams);
LevelCommand呼叫

param = worker.CreateWindow(elemParams);
IntersectionCommand呼叫

CreateWindow调用WPF模型的构造函数,该模型具有 TextBox (ofc还有更多项目,但是只有textbox是相关的),并且取决于命令,它需要显示不同的字符串。(“为{callerclass}选择参数)。

这里是构造函数:

public Model(List<string> attribute,string caller)
        {
            try
            {
                if (attribute.Count == 0)
                    throw new Exception("Нет подходящих параметров");
                Attd = attribute;
                Attw = attribute;
                Caption = "Выберите параметр для \nзаписи "+caller; //Bound to the TextBox,point of my question
                Vis = Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Title",ex.Message);
            }
        }

如您所见,到目前为止,我只是尝试传递一个常量字符串,我认为这不是一个很好的解决方案。

liuxfcumt 回答:重载方法取决于调用实例

尝试使用CallerMemberName属性器:

function config(render_type){
    this.type=render_type;
    this.scale = {
        width: 800,height: 400,mode: phaser.Scale.FIT,parent: "phaser-example",autoCenter: phaser.Scale.CENTER_BOTH,};
    this.physics= {
        default: 'arcade',arcade: {
            gravity: { y: 3000 },debug: false

            }
        };

    this.scene={
        preload: preload,create: create,update: update
    };
};```


Link to MSDN documentation

本文链接:https://www.f2er.com/3110124.html

大家都在问