如果部分类继承自某个类,那么所有其他具有相同名称的部分类也应该继承相同的基类吗? [英] If a partial class inherits from a class then all other partial classes with the same name should also inherit the same base class?
问题描述
我在MVC项目中的Model中有一个这样的类。
I have a class in Model in my MVC project like this.
public partial class Manager : Employee
{
public string Name {get;set;}
public int Age {get;set;}
}
这是我在同一项目的App_Code文件夹中的此类。现在我想知道我的此类是否也需要从 Employee
类继承吗?
And this class I have in App_Code folder in the same project. Now I want to know whether my this class is also need to get inherit from the Employee
class or Not?
public partial class Manager
{
public void SaveEmployee();
}
我必须这样做,因为我的客户希望我将所有方法移入 App_Code
文件夹正在处理数据库。
I have to do this because my client want me to move all the methods in App_Code
folder which are dealing with database.
是的,这两个类都共享同一个名称空间。
And yes both these classes are sharing the same namespace.
推荐答案
这是跨多个声明定义的单个类,而不是两个不同的类。您只需要在单个声明中定义继承模型,例如:
That's a single class defined across multiple declarations, not two different classes. You only need to define the inheritance model in a single declaration, e.g.:
public class Foo { }
//Bar extends Foo
public partial class Bar : Foo { }
public partial class Bar { }
但是,如果尝试以下操作,则会生成编译器错误:'Bar'的部分声明不能指定不同的基类:
However, if you were to try the following, you'd generate a compiler error of "Partial declarations of 'Bar' must not specify different base classes":
public class Foo { }
public partial class Bar : Foo { }
public partial class Bar : object { }
这篇关于如果部分类继承自某个类,那么所有其他具有相同名称的部分类也应该继承相同的基类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!