它是一个不好的做法,通过"本"作为参数? [英] Is it a bad practice to pass "this" as an argument?

查看:129
本文介绍了它是一个不好的做法,通过"本"作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我想写以下内容:

public class Class1() 
{
    public Class1() 
    {
        MyProperty = new Class2(this);
    }

    public Class2 MyProperty { get; private set; }
}

public class Class2() 
{
    public Class2(Class1 class1) 
    {
        ParentClass1 = class1;
    }

    public Class1 ParentClass1 { get; set; }
}



时路过这个作为参数设计问题的迹象?这将是一个更好的办法?

Is passing "this" as an argument a sign of a design problem? What would be a better approach?

推荐答案

没有,有没有基本的设计问题,通过这个。显然,这可以被滥用(创建耦合,通过具有相关类太紧取决于存储在您的实例时,其自身的价值会被要求,例如值),但没有一般问题的。

No, there's no fundamental design problem with passing this. Obviously, this can be misused (creating coupling that's too tight by having a related class depend on values stored in your instance when values of its own would be called for, for example), but there's no general problem with it.

这篇关于它是一个不好的做法,通过"本"作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆