Item 33: Use the new Modifier Only to React to Base Class Updates(Effective C#)
2011-02-22 21:43
686 查看
code 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EffectiveCSharpItem33
7 {
8 class Base
9 {
public void MagicMethod()
{
Console.WriteLine("magic method from base.");
}
}
class Derived : Base
{
public new void MagicMethod()
{
Console.WriteLine("magic method from derived.");
}
}
class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
d.MagicMethod();
Base b = d as Derived;
b.MagicMethod();
}
}
}
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EffectiveCSharpItem33
7 {
8 class Base
9 {
public void MagicMethod()
{
Console.WriteLine("magic method from base.");
}
}
class Derived : Base
{
public new void MagicMethod()
{
Console.WriteLine("magic method from derived.");
}
}
class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
d.MagicMethod();
Base b = d as Derived;
b.MagicMethod();
}
}
}
相关文章推荐
- Effective C#之Item 29:Use the new Modifier Only When Base Class Updates Mandate It
- Effective C# Item 29: Use the new Modifier Only When Base Class Updates Mandate it
- Question 33: How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particu
- Item 39: Use Dynamic to Leverage the Runtime Type of Generic Type Parameters(Effective C#)
- How To Use The New Multivalue Field In Access 2007/2010 Sunday, June 19th, 2011
- 提交时提示错误This Bundle is invalid.New apps and app updates submitted to the App Store must be built wit
- Use the Profile class to store and set the UI culture
- How to use the class in assembly in XAML
- Warning: bind(): React component methods may only be bound to the component instance. See ...
- Use the ItemDataBound event to customize the DataGrid control.
- 提交时提示错误This Bundle is invalid.New apps and app updates submitted to the App Store must be built with
- SSH错误:An association from the table orderitem refers to an unmapped class
- How to use the xsd generator generate the class
- How to use the pointer of function in a class?
- use ItemCreated to set the page style of DataGrid
- The only thing I can think of is to use something like "http://www.dyndns.org/cgi-bin/check_ip.cgi" others may have a be
- 修改ListItem时出现Error: Invalid data has been used to update the list item. The field you are trying to update may be read only.
- [EffectiveC++]item16:Use the same form in corresponding uses of new and delete
- Use P/Invoke to Develop a .NET Base Class Library for Serial Device Communications
- How to use the xsd generator generate the class