首页 > 编程 > .NET > 正文

c#.net中类的覆写(OverRide)

2024-07-10 13:07:19
字体:
来源:转载
供稿:网友

c#.net中类的覆写(override)

 

public class mybase
...{
   public virtual string meth1()
   ...{
      return "mybase-meth1";
   }
   public virtual string meth2()
   ...{
      return "mybase-meth2";
   }
   public virtual string meth3()
   ...{
      return "mybase-meth3";
   }
}

class myderived : mybase
...{
   // overrides the virtual method meth1 using the override keyword:
   public override string meth1()
   ...{
      return "myderived-meth1";
   }
   // explicitly hide the virtual method meth2 using the new
   // keyword:
   public new string meth2()
   ...{
      return "myderived-meth2";
   }
   // because no keyword is specified in the following declaration
   // a warning will be issued to alert the programmer that
   // the method hides the inherited member mybase.meth3():
   public string meth3()
   ...{
      return "myderived-meth3";
   }

   public static void main()
   ...{
      myderived md = new myderived();
      mybase mb = (mybase) md;

      system.console.writeline(mb.meth1());
      system.console.writeline(mb.meth2());
      system.console.writeline(mb.meth3());
   }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表