zoukankan      html  css  js  c++  java
  • C# 语法练习(12): 类[四] 抽象类与抽象成员、密封类与密封成员


    抽象类不能直接实例化:
    using System;
    
    abstract class MyClass
    {
        
    }
    
    class Program
    {
        static void Main()
        {
            /* 抽象类不能直接实例化, 下面这样会出错 */
            MyClass obj = new MyClass();
    
            Console.ReadKey();
        }
    }
    

    但抽象类可以通过子类实例化:
    using System;
    
    abstract class Parent { }
    
    class Child : Parent { }
    
    class Program
    {
        static void Main()
        {
            Parent obj = new Child();
            Console.WriteLine(obj.ToString()); //Child
    
            Console.ReadKey();
        }
    }
    

    抽象方法只能包含在抽象类中:
    using System;
    
    abstract class Parent
    {
        /* 抽象方法是隐式的虚方法, 但不能用 static 或 virtual 修饰 */
        public abstract void Method1();
    
        /* 抽象类可以包含非抽象方法 */
        public void Method2() { Console.WriteLine("Method2"); }
    
        /* 甚至可以包含静态方法 */
        public static void Method3() { Console.WriteLine("Method3"); }
    }
    
    class Child : Parent
    {
        /* 实现抽象方法要使用 override */
        public override void Method1() { Console.WriteLine("Method1"); }
    }
    
    class Program
    {
        static void Main()
        {
            Parent.Method3();          // Method3
            Child.Method3();           // Method3
    
            Parent obj = new Child();
            obj.Method1();             // Method1
            obj.Method2();             // Method2
    
            Console.ReadKey();
        }
    }
    

    派生类要实现父类的抽象方法, 除非它自己也是抽象类:
    using System;
    
    abstract class Parent
    {
        public abstract void Method1();
    }
    
    abstract class Child1 : Parent
    {
    
    }
    
    class Child2 : Child1
    {
        public override void Method1() { Console.WriteLine("Method1"); }
    }
    
    class Program
    {
        static void Main()
        {
            Parent obj = new Child2();
            obj.Method1(); //Method1
    
            Console.ReadKey();
        }
    }
    

    抽象属性:
    using System;
    
    abstract class Shape
    {
        public abstract int Area { get; }
    }
    
    class Rectangle : Shape
    {
        private int FWidth, FHeight;
    
        public Rectangle(int w, int h)
        {
            FWidth = w;
            FHeight = h;
        }
    
        public override int Area
        {
            get { return FWidth * FHeight; }
        }
    }
    
    
    class Program
    {
        static void Main()
        {
            Rectangle Rect = new Rectangle(20, 10);
    
            Console.WriteLine(Rect.Area); // 200
    
            Console.ReadKey();
        }
    }
    

    密封类与密封成员:
    using System;
    
    class Parent
    {
        public virtual void Method1() { Console.WriteLine("Method1"); }
        public virtual void Method2() { Console.WriteLine("Method2"); }
        public virtual void Method3() { Console.WriteLine("Method3"); }
    }
    
    class Child1 : Parent
    {
        /* 下面两个方法可以继续覆盖 */
        public override void  Method1() { Console.WriteLine("New Method1"); }
        public override void  Method2() { Console.WriteLine("New Method2"); }
    
        /* 此方法已用 sealed 禁止了继续覆盖, 也就是取消了虚函数的特性 */
        public sealed override void Method3() { Console.WriteLine("New Method3"); }
    }
    
    /* 此类用 sealed 定为密封类, 不能再有派生类; 不管其内部的性质如何 */
    sealed class Child2 : Child1
    {
        public override void Method1() { Console.WriteLine("New New Method1"); }
        public sealed override void Method2() { Console.WriteLine("New New Method2"); }
    }
    
    
    class Program
    {
        static void Main()
        {
            Child2 obj = new Child2();
            obj.Method1(); // New New Method1
            obj.Method2(); // New New Method2
            obj.Method3(); // New Method3
    
            Console.ReadKey();
        }
    }
    

  • 相关阅读:
    进程 线程
    random模块 时间模块 sys模块 os模块 json模块 pickle模块
    异常处理
    面向对象进阶篇
    面向对象初级篇
    正则表达式
    re模块的相关知识
    CNN归纳偏好
    window和Linux下安装nvidia的apex
    使用GrabCut做分割
  • 原文地址:https://www.cnblogs.com/del/p/1367307.html
Copyright © 2011-2022 走看看