new Modifier (C# Reference)
https://msdn.microsoft.com/en-us/library/435f1dw2.aspx
When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class.
When you hide an inherited member, the derived version of the member replaces the base class version.
Although you can hide members without using the new modifier, you get a compiler warning.
If you use new to explicitly hide a member, it suppresses this warning.
To hide an inherited member, declare it in the derived class by using the same member name, and modify it with the new keyword.
For example:
public class BaseC
{
public int x;
public void Invoke() { }
}
public class DerivedC : BaseC
{
new public void Invoke() { }
}
In this example, BaseC.Invoke is hidden by DerivedC.Invoke.
The field x is not affected because it is not hidden by a similar name.
Name hiding through inheritance takes one of the following forms:
Generally, a constant, field, property, or type that is introduced in a class or struct hides all base class members that share its name.
There are special cases.
For example, if you declare a new field with name N to have a type that is not invocable, and a base type declares N to be a method, the new field does not hide the base declaration in invocation syntax.
See the C# language specification for details (see section "Member Lookup" in section "Expressions").
It is an error to use both new and override on the same member, because the two modifiers have mutually exclusive meanings. The new modifier creates a new member with the same name and causes the original member to become hidden. The override modifier extends the implementation for an inherited member.
Using the new modifier in a declaration that does not hide an inherited member generates a warning.
问题:
I'm working with the .NET framework and I really want to be able to make a custom type of page that all of my website uses.
The problem comes when I am trying to access the page from a control.
I want to be able to return my specific type of page instead of the default page.
Is there any way to do this?
public class MyPage : Page
{
// My own logic
} public class MyControl : Control
{
public MyPage Page { get; set; }
}
答案:
It sounds like what you want is return type covariance. C# does not support return type covariance.
Return type covariance is where you override a base class method that returns a less-specific type with one that returns a more specific type:
class Animal
{
} class Fish : Animal
{
} abstract class Enclosure
{
public abstract Animal Contents();
}
class Aquarium : Enclosure
{
public new Fish Contents() { return new Fish(); }
}
This is safe because consumers of Contents via Enclosure expect an Animal, and Aquarium promises to not only fulfill that requirement, but moreover, to make a more strict promise: that the animal is always a fish.
//上面的代码会报错,因为返回类型不一致
This kind of covariance is not supported in C#, and is unlikely to ever be supported.
It is not supported by the CLR. (It is supported by C++, and by the C++/CLI implementation on the CLR; it does so by generating magical helper methods of the sort I suggest below.)
(Some languages support formal parameter type contravariance as well -- that you can override a method that takes a Fish with a method that takes an Animal. Again, the contract is fulfilled; the base class requires that any Fish be handled, and the derived class promises to not only handle fish, but any animal. Similarly, C# and the CLR do not support formal parameter type contravariance.)
The way you can work around this limitation is to do something like:
class Animal
{
} class Fish : Animal
{
} abstract class Enclosure
{
protected abstract Animal GetContents(); //子类通过重写这个方法,来返回Fish实例 public Animal Contents()
{
return this.GetContents();
}
}
class Aquarium : Enclosure
{
protected override Animal GetContents()
{
return this.Contents();
} public new Fish Contents()
{
return new Fish();
}
}
Now you get both the benefits of overriding a virtual method, and getting stronger typing when using something of compile-time type Aquarium.
Enclosure enclosure = new Aquarium();
Animal animal = enclosure.Contents();
Console.WriteLine(animal.GetType());
===2017年06月30日===更新
https://dotnetfiddle.net/tXdyiG
using System; namespace PolymorphismApplication
{
public abstract class Shape
{
protected int width, height;
public Shape(int a = , int b = )
{
width = a;
height = b;
} public virtual int area()
{
Console.WriteLine("Parent class area :");
return ;
} public virtual int func()
{
return ;
}
} public class Rectangle : Shape
{
public Rectangle(int a = , int b = ): base (a, b)
{
} public override int area()
{
Console.WriteLine("Rectangle class area :");
return (width * height);
} public new int func()
// if this class is implemented by an object declared on the base class
// e.g Shape newShape = new Rectancle(5, 10), then the object (newShape)
// will NOT inherit this method. It will stick to the one in Shape.
// Look at the implementation of Caller and the Program/Main to see example.
{
return ;
}
} public class Triangle : Shape
{
public Triangle(int a = , int b = ): base (a, b)
{
} public override int area()
{
Console.WriteLine("Triangle class area :");
return (width * height / );
} public override int func()
{
return ;
}
} public class Caller
{
public void CallArea(Shape sh)
{
int a;
a = sh.area();
Console.WriteLine("Area: {0}", a);
int c = sh.func();
Console.WriteLine("func: {0}", c);
}
public void CallArea2 (Rectangle rect)
{
int a;
a = rect.area();
Console.WriteLine("Area: {0}", a);
int c = rect.func();
Console.WriteLine("func: {0}", c);
}
} public class Program
{
public void Main(string[] args)
{
Caller c = new Caller();
c.CallArea(new Rectangle(, )); // a rectangle is being passed to CallArea's SHAPE object-variable sh
c.CallArea(new Triangle(, ));
c.CallArea2(new Rectangle(, )); // a rectangle is being passed to CallArea2's RECTANGLE object-variable rect
// Takeaway: When you declare a rectangle as a reactangle, it will act differently from when you declare it as a shape.
// And this is because of the use of 'new' for the func method, which is only given to rects declared as Rectangles
// }
}
}
https://github.com/kerryjiang/IrvineCSharpCourses/blob/master/Example11/Program.cs
new Modifier (C# Reference)的更多相关文章
- Knowing When to Use Override and New Keywords (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173153.aspx In C#, a method in a derived class can have t ...
- 【转载】#273 - Parameter Modifier Summary
Here's a summary of the different parameter modifiers and how the behavior changes for each, when us ...
- 解决Deprecated: preg_replace(): The /e modifier is deprecated, use
使用php5.5运行ecshop的时候出现如下错误Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace ...
- Java Interview Reference Guide--reference
Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, ...
- Sphinx 2.2.11-release reference manual
1. Introduction 1.1. About 1.2. Sphinx features 1.3. Where to get Sphinx 1.4. License 1.5. Credits 1 ...
- C# to IL 6 Reference and Value Types(引用类型和值类型)
An interface is a reference type, in spite of the fact that it has no code at all. Thus, wecannot in ...
- What the difference between __weak and __block reference?
近日遇到一个非常细的知识点,关于block的循环引用问题.相比非常多人都遇到了.也能顺利攻克了,至于block方面的技术文章.那就很多其它了.这里不再赘述,可是有这样一个问题: What the di ...
- abstract (C# Reference)
https://msdn.microsoft.com/en-us/library/sf985hc5.aspx The abstract modifier indicates that the thin ...
- protected (C# Reference)
https://msdn.microsoft.com/en-us/library/bcd5672a.aspx The protected keyword is a member access modi ...
随机推荐
- JavaScipt30(第一个案例)(主要知识点:键盘事件以及transitionend)
今天得到一个github练习项目,是30个原生js写成的小例子,麻雀虽小五脏俱全,现在记录一下第一个. 第一个是键盘按键时页面上对应的键高亮,同时播放音频,松开后不再高亮. 我自己实现了一下,然后查看 ...
- SAS,SATA普及文档
目前所能见到的硬盘接口类型主要有IDE.SATA.SCSI.SAS.FC等等. IDE是俗称的并口,SATA是俗称的串口,这两种硬盘是个人电脑和低端服务器常见的硬盘.SCSI是"小型计算机系 ...
- JPQL 的基本使用
一.概念 JPQL 语言,即 Java Persistence Query Language 的简称.JPQL 和 HQL 是非常类似的,支持以面向对象的方式来写 SQL 语句,当然也支持本地的 SQ ...
- 个人Linux(ubuntu)使用记录——更换软件源
说明:记录自己的linux使用过程,并不打算把它当作一个教程,仅仅只是记录下自己使用过程中的一些命令,配置等东西,这样方便自己查阅,也就不用到处去网上搜索了,所以文章毫无章法可言,甚至会记录得很乱 s ...
- css--小白入门篇2
一.css基础选择器 html负责结构,css负责样式,js负责行为. css写在head标签里面,容器style标签. 先写选择器,然后写大括号,大括号里面是样式. 1 <style type ...
- centos7进入救援模式,修复错误配置
因某些修改操作,导致系统重启后无法正常启动,此时可进入救援模式,修复错误配置即可. OS:centos 7 1.重启系统后,进入grup引导页面,选中第一项然后按“e” 进入编辑模式: 2.通过↓键找 ...
- 59.关系型与document类型数据模型对比
现假设有如下两个类: class Department(object): def __init__(self, dept_id, name, desc, employees=[]): self.dep ...
- ubuntu下手动配置apache2.4.12
(apache2也可以使用 sudo apt-get install apache2来安装,下面来讲解下如何手动安装配置apache2) 在安装apache2之前,先要安装apache2的依赖项,ap ...
- stall and flow separation on airfoil or blade
stall stall and flow separation Table of Contents 1. Stall and flow separation 1.1. Separation of Bo ...
- [spoj1182][Sorted Bit Sequence] (数位dp)
Description Let's consider the 32 bit representation of all integers i from m up to n inclusive (m ≤ ...