在下面的代码中,将私有成员 ,_field
作为class Foo
外部方法参数 ( Bar.DoSomething(_field)
) 传递是否违反了SOLID 编程实践中的打开/关闭原则?
在面向对象编程中,开放/封闭原则指出“软件实体(类、模块、函数等)应该对扩展开放,对修改关闭”;也就是说,这样的实体可以允许其行为被扩展而无需修改其源代码。
据我了解,实体应该对扩展开放,但对修改关闭。但是,在这种情况下,在and is_field
的构造函数中设置一次。将私有成员传递给外部方法的参数是否违反了开放/封闭原则或其他一些最佳实践?Foo
readonly
public class Foo
{
private readonly int _field;
public Foo(int input)
{
_field = input;
}
private void FooDoSomething()
{
Bar.BarDoSomething(_field); //Breaking Open/Closed Principle?
}
}
public static class Bar
{
public static void BarDoSomething(int input)
{
//Something happens
}
}