简体   繁体   English

是否可以覆盖密封类的方法?

[英]Is it possible to override method of sealed class?

In WinRT (C#, XAML), ScrollViewer is a sealed class, and I can't extend it, but I need to overwrite some methods (for example: ScrollToHorizontalOffset ).在 WinRT(C#、XAML)中, ScrollViewer是一个密封类,我无法扩展它,但我需要覆盖一些方法(例如: ScrollToHorizontalOffset )。

Is it possible to override methods of a sealed class?是否可以覆盖密封类的方法?

No - in order to override a method, you have to derive from it, which you can't do when the class is sealed.不 - 为了覆盖一个方法,你必须从它派生,当类被密封时你不能这样做。

Basically, you need to revisit your design to avoid this requirement...基本上,您需要重新审视您的设计以避免此要求......

You can't inherit from a sealed class, so no inheritance, no override.你不能从密封类继承,所以没有继承,没有覆盖。

See: override C#请参阅:覆盖 C#

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.需要覆盖修饰符来扩展或修改继承的方法、属性、索引器或事件的抽象或虚拟实现。

See: sealed C#请参阅:密封的 C#

When applied to a class, the sealed modifier prevents other classes from inheriting from it.当应用于一个类时,sealed 修饰符会阻止其他类从它继承。

As the word "sealed" itself indicates that , it is protected from being inherited or overridden正如"sealed"一词本身表明的那样,它受到保护,不会被继承或覆盖

So, No inheritance implies no Overriding.所以,没有继承意味着没有覆盖。

please find this link to find more details about sealed class in detail.请找到链接以详细了解有关密封类的更多详细信息。

密封本身意味着类不能被继承,那些不能被继承的类意味着不能在派生类中访问该类的任何属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM