`
talin2010
  • 浏览: 520762 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

17.5.4 Override methods

阅读更多
When an instance method declaration includes an override modifier, the
method is said to be an override
method. An override method overrides an inherited virtual method with the
same signature. Whereas a virtual
method declaration introduces a new method, an override method declaration
specializes an existing inherited
virtual method by providing a new implementation of that method.
The method overridden by an override declaration is known as the overridden
base method. For an override
method M declared in a class C, the overridden base method is determined by
examining each base class of C,
starting with the direct base class of C and continuing with each
successive direct base class, until an accessible
method with the same signature as M is located. For the purposes of
locating the overridden base method, a
method is considered accessible if it is public, if it is protected, if it
is protected internal, or if it is
internal and declared in the same program as C.
A compile-time error occurs unless all of the following are true for an
override declaration:
? An overridden base method can be located as described above.
? The overridden base method is a virtual, abstract, or override method. In
other words, the overridden base
method cannot be static or non-virtual.
? The overridden base method is not a sealed method.
? The override declaration and the overridden base method have the same
return type.
? The override declaration and the overridden base method have the same
declared accessibility. In other
words, an override declaration cannot change the accessibility of the
virtual method.
An override declaration can access the overridden base method using a
base-access (§14.5.8). [Example: In the
example
class A
{
int x;
public virtual void PrintFields() {
Console.WriteLine("x = {0}", x);
}
}
C# LANGUAGE SPECIFICATION
236
class B: A
{
int y;
public override void PrintFields() {
base.PrintFields();
Console.WriteLine("y = {0}", y);
}
}
the base.PrintFields() invocation in B invokes the PrintFields method
declared in A. A base-access
disables the virtual invocation mechanism and simply treats the base method
as a non-virtual method. Had the
invocation in B been written ((A)this).PrintFields(), it would recursively
invoke the PrintFields
method declared in B, not the one declared in A, since PrintFields is
virtual and the run-time type of
((A)this) is B. end example]
Only by including an override modifier can a method override another
method. In all other cases, a method
with the same signature as an inherited method simply hides the inherited
method. [Example: In the example
class A
{
public virtual void F() {}
}
class B: A
{
public virtual void F() {} // Warning, hiding inherited F()
}
the F method in B does not include an override modifier and therefore does
not override the F method in A.
Rather, the F method in B hides the method in A, and a warning is reported
because the declaration does not
include a new modifier. end example]
[Example: In the example
class A
{
public virtual void F() {}
}
class B: A
{
new private void F() {} // Hides A.F within B
}
class C: B
{
public override void F() {} // Ok, overrides A.F
}
the F method in B hides the virtual F method inherited from A. Since the
new F in B has private access, its scope
only includes the class body of B and does not extend to C. Therefore, the
declaration of F in C is permitted to
override the F inherited from A. end example]
分享到:
评论

相关推荐

    annogen-0.1.0.jar

    org.codehaus.annogen.generate.Annogen.class org.codehaus.annogen.generate.AnnogenInfo.class org.codehaus.annogen.generate.AnnogenTask.class ...org.codehaus.annogen.override.ElementId.class

    opreator-override.zip_override+c++_运算符重载

    "opreator-override.zip_override+c++_运算符重载"这个压缩包文件,显然包含了关于C++运算符重载的详细信息,特别是如何通过`override`关键字来更精确地控制重载。 首先,运算符重载并不是创建新的运算符,而是给已...

    Lua 类的继承与override

    本文将深入探讨 Lua 中如何实现类的继承以及方法的重写(override)。 首先,我们来理解 Lua 中的“类”。在 Lua 中,类通常被表示为一个表(table),这个表包含了类的一些属性和方法。创建一个类,我们可以定义一...

    Android下拉刷新上拉加载源码

    @Override public void onRefresh() { mHandler.postDelayed(new Runnable() { @Override public void run() { start = ++refreshCnt; items.clear(); geneItems(); // mAdapter....

    ext js 4.1apply && override

    ### Ext JS 4.1中的`Ext.apply`与`Ext.override` #### 一、Ext.apply详解 `Ext.apply`是Ext JS框架中的一个核心方法,主要用于对象的属性合并。此方法能够将一个或多个对象的属性复制到目标对象中,从而实现对象...

    Android代码-FragmentAnimation

    FragmentAnimations [![API](https://img.shields.io/badge/API-4+-yellow.svg?style=flat)](https://android-arsenal.com/api?level=4) [!...Animation examples ...@Override public Animation onCreateAnimation(in

    android @override 报错解决方案

    原因: Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 can be annotated with @Override, but in Java 1.5 can only be applied to methods ...

    new,override,virtual 示例

    4. 示例: 假设我们有一个基类`Animal`,其中有一个`MakeSound()`方法,然后有一个派生类`Dog`。如果我们想让`Dog`发出特别的声音,可以这样做: ```csharp public class Animal { public virtual void ...

    JavaOnClickListener用法.pdf

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD); ...

    java第一次实验报告.docx

    4. **重写(Override)**:子类可以重写父类的同名方法,提供自己的实现。`@Override`注解确保方法是明确地覆盖父类的方法。 5. **接口(Interface)**:接口是Java中定义行为的抽象类型,它包含一组抽象方法。类...

    Override错误已解决

    ### Override 错误详解与解决方案 #### 一、概述 在进行 Java 开发的过程中,特别是在使用 Eclipse 这样的集成开发环境(IDE)时,开发者可能会遇到一个常见的问题:`Override` 错误。这种错误通常发生在尝试覆盖...

    Delphi面向对象:overload与override[文].pdf

    Delphi面向对象编程之overload与override Delphi面向对象编程中,overload和override是两个非常重要的概念,它们都是面向对象编程的基础概念。本文将详细介绍overload和override的概念、应用场景及实现方法。 一、...

    实现一个view的监听的四种方法

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取 Button 的 id 并设置监听 Button button01 = (Button) ...

    建造者模式实例

    builder.setWheels(4); builder.setBody("Sedan"); return builder.build(); } } ``` 客户端代码可以通过导演类来创建大众CC汽车实例: ```java public class Client { public static void main(String[] ...

    Covariant-Return-Types-and-Smart-Pointers:协变返回类型和智能指针

    Square* clone () const override // return type is Square* instead of Figure* (Covariant Return Type) { return new Square (* this ); } // ... other methods }; 它编译并运行。 返回类

    MockDialogInterface.rar_All You Need

    A mock {@link android.content.DialogInterface} class. All methods are non-functional and throw {@link java.lang.UnsupportedOperationException}. Override it to provide the operations that you need.

    一个轻量好用的Android开发屏幕适配工具库(一个轻量好用的Android开发屏幕适配工具库).zip

    效果展示 快速开始添加 implementation 'me.yatoooon:screenadaptation:1.1.1'初始化工具类 (1.)创建自己的应用程序继承Applicationpublic class App extends Application { @Override public void onCreate() { ...

    Typescript-Docker-React:开发和生产的一项命令

    需要将docker-compose.override.sample.yml docker-compose.override.yml重命名docker-compose.override.sample.yml docker-compose.override.yml和docker-compose.override.yml docker-compose up并转到 ...

    override abstract virtual interface比较

    C# 中 override、abstract、virtual、interface 的比较 在 C# 编程语言中,override、abstract、virtual、interface 是四个关键字,它们之间存在着紧密的联系,我们经常会遇到 override、abstract、virtual 这三个...

    ExpandableListView的使用

    ### 4. 动态加载数据 在某些情况下,我们可能需要动态加载数据,比如从网络获取。这时,可以在获取到数据后,更新适配器的数据源,然后调用`notifyDataSetChanged()`方法,使`ExpandableListView`自动更新视图。 #...

Global site tag (gtag.js) - Google Analytics