从标准小部件继承并在 C# 中设置我自己的默认值是个好主意吗?
在 Visual Studio (C#) 中设计应用程序时,如果我知道我将拥有一定数量的 DataGridView,它们都具有相同的属性(例如宽度、高度、颜色、一些其他属性,例如:禁用直接编辑行的选项等)可以制作我自己的类(“myDataGridView”)来继承 DataGridView 类并在那里进行所有调整,然后稍后在代码中实例化该类吗?就像这样:
//my class:
class myDataGridView : DataGridView
{
this.BorderStyle = <someValue>
this.ColumnCount = <someValue>
//etc.
public void method1()
{
//some code...
}
public void method2()
{
//some code...
}
}
//instantiate it somewhere:
myDataGridView dgv1 = new myDataGridView();
myDataGridView dgv2 = new myDataGridView();
myDataGridView dgv3 = new myDataGridView();
关于 OO 原则可以吗?我的朋友说,将代码放入
this.BorderStyle = <someValue>
myDataGridView 类中是不好的做法,因为调整这样的属性会超出 dataGridView 的属性,而其他开发人员可能会在 Visual Studio 中直观地调整这些属性,如果您明白我的意思的话。这有关系吗?我的意思是,如果我想将 DataGridView 视为一个对象,那么它可以拥有其属性和行为,对吧?在我的类中拥有调整 DataGridView 属性的代码是可以的、可读的,并且想要更改某些属性的每个其他开发人员都可以在 myDataGridView 类中更改它。这种做法是不好的还是错误的?如果是,当您知道您的应用程序将有许多具有相同属性/行为的 DataGridView 时,最佳实践是什么?谢谢。
When designing app in Visual Studio (C#), if I know that I will have a certain number of DataGridViews that all have same properties (like width, height, color, some other properties like: disable option to directly edit rows, etc.) is it ok to make my own class ("myDataGridView") that inherits DataGridView class and make all adjustments there, and then just instantiate that class later in code? Like this:
//my class:
class myDataGridView : DataGridView
{
this.BorderStyle = <someValue>
this.ColumnCount = <someValue>
//etc.
public void method1()
{
//some code...
}
public void method2()
{
//some code...
}
}
//instantiate it somewhere:
myDataGridView dgv1 = new myDataGridView();
myDataGridView dgv2 = new myDataGridView();
myDataGridView dgv3 = new myDataGridView();
Is that ok regarding OO principles? My friend says that putting code like
this.BorderStyle = <someValue>
in myDataGridView class is bad practice because adjusting properties like that will overrun properties of dataGridView which some other developer may adjusted visually in Visual Studio, if you know what I mean. Does that matter? I mean, if I want to treat my DataGridView as an object then it can have its properties and behavior, right? And having code that adjusts properties of DataGridView in my class is ok, readable, and every other developer who wants to change some properties can change it in myDataGridView class. It that kind of practice bad or wrong? If it is, what is the best practice when you know your app will have many DataGridViews which have same properties/behaviour? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,这不是正确的做法。仅仅为了少数属性修改而继承控件是没有意义的。您不会在控件中引入任何其他行为,而是尝试编写一些初始化辅助方法来执行此操作。
In my opinion, this is not the right approach. Inheriting controls just for few property modifications does not make sense. You're not introducing any additional behavior in the control, Rather,try writing some initialization helper methods to do this.
如果您担心对属性的修改会被开发人员的视觉设置覆盖,您可以使用 [Browsable(false)] 属性隐藏它们。
然而,从 OOP 的角度来看,除非确实必要,否则不鼓励继承。有一条黄金法则:“优先考虑组合而不是继承”。就如这个。 __curious_geek 表示,继承控件以这种方式设置属性并不是一个好主意,但始终为您的业务类和屏幕使用通用基类是一个好习惯。您可以在所有屏幕的基类中创建一个函数,以按照您想要的方式正确调整网格。如果将其设为虚拟,则可以在派生类中重写它,以根据需要自定义每种情况的行为。
If you are worried that your modifications to the properties get overwritten by a visual setting by the developer you may hide them using [Browsable(false)] attribute.
However, from OOP perspective inheritance is not encouraged unless really necessary. There is a golden rule : "Favor composition over inheritance". As this. __curious_geek said it's not a good idea to inherit controls to set properties this way but it's good practice to always have common base class for your business classes and screens. You may make a function in the base class of all screens to properly adjust the grids for you the way you want. If you make it virtual, you may override it in derived classes to customize the behavior per case as needed.