zoukankan      html  css  js  c++  java
  • 抽象类 new修饰符 override virtual

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace TestNewCreatNewMethod
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             test1 t1=new test1();
    12             test2 t2=new test2();
    13             test3 t3=new test3();
    14             t1.prinf();
    15             t1.printf();
    16 
    17             t2.prinf();
    18             t2.printf();
    19 
    20             t3.prinf();
    21             t3.printf();
    22             Console.ReadKey();
    23         }
    24     }
    25 
    26     public abstract class book //抽象类中可以没有抽象方法,但抽象类不能被实例化,只能被继承,另外不能用sealed关键字,密封类不能被继承
    27     {
    28         public virtual void prinf()//虚方法用virtual关键字修饰
    29         {
    30             Console.WriteLine("this virtual method");
    31         } 
    32         public void printf()
    33         {
    34             Console.WriteLine("this real method");
    35         }
    36     }
    37 
    38     public abstract class book1
    39     {
    40         public virtual void prinf()
    41         {
    42             Console.WriteLine("this virtual method");
    43         } 
    44         public abstract void printf();
    45     }
    46 
    47     public class test1:book
    48     {
    49         public new void prinf()//类中的virtual方法可以由override实现也可以用new关键字隐藏基类中的虚方法
    50         {
    51             base.prinf();
    52             Console.WriteLine("book new method prinf");
    53         }
    54         public new void printf()//类中的实方法可以由new关键字隐藏基类中的虚方法
    55         {
    56             Console.WriteLine("book new method printf");
    57         }
    58     }
    59 
    60     public class test2:book1
    61     {
    62         public override void prinf()//抽象类中的virtual方法可以由override实现也可以用new关键字隐藏基类中的虚方法
    63         {
    64             Console.WriteLine("book1 override method prinf");
    65             base.prinf();
    66         }
    67         public override void printf()//抽象类中的抽象方法只能由override实现
    68         {
    69             Console.WriteLine("book1 override method printf");
    70         }
    71     }
    72 
    73     public class test3:book1
    74     {
    75         public override void printf()   //抽象类中的抽象方法只能由override实现
    76         {
    77             Console.WriteLine("book1  abstruct method printf");
    78         }
    79     }
    80 }
    81 
  • 相关阅读:
    foreach的使用(c#)
    ssl详解
    c#的html格式化输出(看php10012视频时想到的)
    PHP句法规则详解(转载)
    C#中Bitmap类实现对图像操作的一些方法(Asp.net(C#)放大缩小图片尺寸)
    c# 窗体最小化到托盘
    C# 中的委托和事件
    C# StringBuilder和String浅析
    c#值类型和引用类型
    后台关闭页面
  • 原文地址:https://www.cnblogs.com/xFight/p/1886322.html
Copyright © 2011-2022 走看看