A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 伪善者。 中级黑马   /  2014-4-27 21:13  /  1308 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 伪善者。 于 2014-4-28 00:34 编辑

看视频中讲到 重写父类的方法的时候 一种是在子类方法的 访问修饰符后加 new
另一种是声明父类的虚方法 再用override重写
请问这两种方法有何不同?优劣何在?谢谢

2 个回复

倒序浏览
一个override 是重写
一个new 是显示隐藏基类

如果基类的方法是virtual 的。
所以子类必须重写基类的方法,也就是必须隐藏基类的方法。
给你看下区别的代码
  1. using System ;
  2. public class MyBase
  3. {
  4.      public static int x = 55 ;
  5.      public static int y = 22 ;
  6. }
  7. public class MyDerived : MyBase
  8. {
  9.      new public static int x = 100; // 利用new 隐藏基类的x
  10.      public static void Main()
  11.     {
  12.      // 打印x:
  13.      Console.WriteLine(x);
  14.      //访问隐藏基类的 x:
  15.      Console.WriteLine(MyBase.x);
  16.      //打印不隐藏的y:
  17.      Console.WriteLine(y);
  18.      }
  19. }

  20. //这个是调用父类的方法
  21. using System;
  22. public class Parent
  23. {
  24.     string parentString;
  25.     public Parent()
  26.     { Console.WriteLine("Parent Constructor."); }
  27.     public Parent(string myString)
  28.     {
  29.         parentString = myString;
  30.         Console.WriteLine(parentString);
  31.     }
  32.     public void print()
  33.     { Console.WriteLine("I'm a Parent Class."); }
  34. }
  35. public class Child : Parent
  36. {
  37.     public Child()
  38.         : base("From Derived")
  39.     { Console.WriteLine("Child Constructor."); }
  40.     public void print()
  41.     {
  42.         base.print();
  43.         Console.WriteLine("I'm a Child Class.");
  44.     }
  45.     public static void Main()
  46.     {
  47.         Child child = new Child();
  48.         child.print();
  49.         ((Parent)child).print();
  50.     }
  51. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
本帖最后由 小狸 于 2014-4-28 01:07 编辑

     当子类从基类继承时,它会获得基类的所有方法、字段、属性和事件。若要更改基类的数据和行为,有两种方法:
    1.override 重写虚拟的基类方法;
    2.new 使用新的派生成员替换基类的方法;
    两者的区别在于。override 会直接“覆盖掉基类的方法”;而new后,没有使用到基类的方法,而是直接重新定义了子类的方法。
    例如:
  1. using System;
  2. using System.Text;

  3. namespace over
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             BaseClass bc = new BaseClass();  
  10.             DerivedClass dc = new DerivedClass();
  11.             BaseClass bcdc = new DerivedClass();//使用子类的对象实例化基类的对象;
  12.             bc.Method1();
  13.             bc.Method2();

  14.             dc.Method1();
  15.             dc.Method2();

  16.             bcdc.Method1();
  17.             bcdc.Method2();
  18.             Console.ReadLine();
  19.         }
  20.     }

  21.     class BaseClass
  22.     {
  23.         public virtual void Method1()  //基类虚方法1
  24.         {
  25.             Console.WriteLine("基类.Method1");
  26.         }

  27.         public virtual void Method2()   //基类虚方法2
  28.         {
  29.             Console.WriteLine("基类.Method2");
  30.         }
  31.     }

  32.     class DerivedClass : BaseClass
  33.     {
  34.         public override void Method1() //override
  35.         {
  36.             Console.WriteLine("子类.Method1");
  37.         }

  38.         public new void Method2()  //new
  39.         {
  40.             Console.WriteLine("子类.Method2");
  41.         }
  42.     }
  43. }
复制代码


         结果为:基类.Method1
                       基类.Method2
                       子类.Method1
                       子类.Method2
                       子类.Method1
                       基类.Method2

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马