zoukankan      html  css  js  c++  java
  • knowing abstract,virtual,override,new

    1. If a class has at least one member which modified by "abstract",this class is an abstract class and you have to modify this class using keyword "abstract", while the member is called "abstract member"(say,abstract method or abstract property).
    2. Actual implementation is not allowed in abstract members.
    3. An abstract class can not be instantiated.
    4. When a class inherits an abstract class, it must implement all base class's abstract members(for the abstract modifier can be used with classes, methods, properties, indexers, and events).
    1. The modifier "virtual" is just simply indicate that the method can be overrided in sub classes.Beside this,a virtual member(non-static methods,properties) is as the same as usual members. 
    1. "override" is used to override base class's virtual or abstract members.
    2. "new" is used to cover base class's same-name members,but it won't override base class's anything.

    example:

    using System;
    
    namespace DesignPattern
    {
        public class A
        {
            public virtual void Bar()
            {
                Console.WriteLine("A.Bar");
            }
    
            public void Foo()
            {
                Console.WriteLine("A.Foo");
            }
            
        }
    
        class B:A
        {
            public override void Bar()
            {
                Console.WriteLine("B.Bar");
            }
    
            public new void Foo()
            {
                Console.WriteLine("B.Foo");
            }
    
        }
    
    
    
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("----A1---");
                A A1 = new A();
                A1.Bar();//"A.Bar",for it's A.Bar
                A1.Foo();//"A.Foo",for it's A.Foo
                Console.WriteLine("---A2----");
                A A2 = new B();
                A2.Bar();//"B.Bar",for it has overrided father class's Bar
                A2.Foo();//"A.Foo".in fact i don't really know why.
                Console.WriteLine("---B1----");
                B B1 = new B();
                B1.Bar();//"B.Bar"
                B1.Foo();//"B.Foo"
                
                Console.Read();
            }
        }
    }
  • 相关阅读:
    天轰穿C#教程之C#基础的学习路线
    天轰穿C#教程之大话C#
    天轰穿C#教程之#pragma介绍[原创]
    天轰穿.NET教程之第一个控制台应用程序
    .NET笔试题整理(转)
    天轰穿.NET教程之基类库
    天轰穿C#教程之C#有哪些特点?
    程序猿!?应该有哪些目标?
    浅谈编程程序员应该具备的职业素养 [转载]
    .NET访问MySQL数据库方法(转)
  • 原文地址:https://www.cnblogs.com/lwhkdash/p/3205244.html
Copyright © 2011-2022 走看看