asp.net:部分类和继承 [英] asp.net: partial classes and inheritance

查看:84
本文介绍了asp.net:部分类和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分为两个文件的类。其中一个是生成的,另一个包含非生成的东西。

I have a class split across two files. One of these is generated, the other contains the non-generated stuff.

我希望我的类继承自基类。我是否需要继承这两个文件?或者,如果任何部分类

I want my class to inherit from a base class. Do I need to have both files inherit? Or will the class inherit from the base class if either partial class

在生成的foo.vb中,该类将继承自基类:

In generated foo.vb:


部分公共类Foo
继承BaseClass

在手动创建的foo.vb中:

In manually-created foo.vb:


部分公共类Foo

这似乎并不重要(根据我在Reflector中看到的,无论如何)。

It doesn't seem to matter (according to what I see in Reflector, anyways).

谁能解释一下这是如何工作的?编译器是否简单地将两者组合在一起?

Can anyone explain how this works? Does the compiler simply combine the two?

推荐答案

两者中只有一个需要继承。

Only one of the two needs to inherit.

部分类只是编译器技巧。在编译之前,您的两个文件将重新拼接在一起。这意味着只能指定一个基类,就像在普通类中一样。

Partial classes are just compiler tricks. Your two files are stitched back together before compiling. This means that only one base class can be specified, just like in normal classes.

你可以这样:

partial class one : base {}
partial class one {}

and this:

partial class one : base {}
partial class one : base {}

但不是这个

partial class one : fu {}
partial class two : bar {}

因为最后一个组合成:

class one : fu, bar {} 

这是非法的。但是,您可以混合和匹配接口,就像在普通类上一样。

which is illegal. You can mix and match interfaces, however, just like on a normal class.

这篇关于asp.net:部分类和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆