如果某个部分类继承自某个类,那么所有其他具有相同名称的部分类也应该继承相同的基类?

时间:2022-09-25 10:32:40

I have a class in Model in my MVC project like this.

我在我的MVC项目中有一个Model类。

public partial class Manager : Employee
{
    public string Name {get;set;}
    public int Age {get;set;}
}

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?

而这个类我在同一个项目中的App_Code文件夹中。现在我想知道我的这个类是否还需要从Employee类继承或不是?

public partial class Manager 
{
    public void SaveEmployee();
}

I have to do this because my client want me to move all the methods in App_Code folder which are dealing with database.

我必须这样做,因为我的客户端要我移动App_Code文件夹中处理数据库的所有方法。

And yes both these classes are sharing the same namespace.

是的,这两个类共享相同的命名空间。

2 个解决方案

#1


19  

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 {  }

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":

但是,如果您尝试以下操作,则会生成编译器错误“部分声明'Bar'不能指定不同的基类”:

public class Foo { }

public partial class Bar : Foo { }

public partial class Bar : object {  }

#2


1  

Yes, the other part of the partial class is still the same class so it does inherit from Employee.

是的,部分类的另一部分仍然是同一个类,所以它继承自Employee。

#1


19  

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 {  }

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":

但是,如果您尝试以下操作,则会生成编译器错误“部分声明'Bar'不能指定不同的基类”:

public class Foo { }

public partial class Bar : Foo { }

public partial class Bar : object {  }

#2


1  

Yes, the other part of the partial class is still the same class so it does inherit from Employee.

是的,部分类的另一部分仍然是同一个类,所以它继承自Employee。