问题
I want to inherit the DevExpress ComboBoxEdit control, and am wondering if naming my class the same as the DevExpress class is bad practice.
Here's the derived class declaration:
using System;
using System.Collections.Generic;
using System.Text;
namespace MyApplication.Components
{
public class ComboBoxEdit : DevExpress.XtraEditors.ComboBoxEdit
{
}
}
Should I rename MyApplication.Components.ComboBoxEdit to something else like MyComboBoxEdit?
回答1:
There's must be a reason why you're creating your own ComboBoxEdit. I would use that reason in the name of the class.
回答2:
It's a good idea only if you want the maintenance dude to hate you with an intensity of a thousand suns.
回答3:
Use common sense.
1) If both your and the DevExpress combo boxes are used throughout the application name it something different (but still something that reveals its identity).
2) If you have a well defined convention that you will inherit all components from a certain namespace and use only your own then I see no problem with doing this.
回答4:
Well, it's legal to do it, but I would advise against it because it would only cause confusion... Why not name it "MyComboBoxEdit" or "SuperComboBoxEdit" or anything you like ?
回答5:
Technically, your class has a different name already. Your class is named "MyApplication.Components.ComboBoxEdit" and the DevExpress class is named "DevExpress.XtraEditors.ComboBoxEdit".
That said, your class likely has different behavior to the DevExpress class. For this reason you should probably give your new class a more descriptive name.
回答6:
If it's not confusing for you (and your coworkers / other people that will work on the application) then it's perfectly legitimate to use the same name - that's why namespaces exist, after all.
Otherwise you might want to name your class after the reason you're extending the base one, e.g. ComboBoxEditThatAlsoDoesThisAndThat, rather than using the generic My prefix...
回答7:
I too would advise against a naming collision. With respect to the name, if you have a reason to create an inherited control, then you should be able to give a useful name.
The My prefix is severely limiting and has little value to newcomers reading the code. You also risk running into MyComboBox2 when you need a new one.
回答8:
There is nothing illegal about it, but if you end up with a using statement like this in a different file, you'll run into problems.
using DevExpress.XtraEditors;
Then you end up burying the class name in intellisense (since two classes have the same name); so then you'll have to explicitly specify which class you are using.
I'm assuming... I've never used Dev Express. I don't know if XtraEditors is a namespace.
来源:https://stackoverflow.com/questions/1394793/class-inheritance-naming