C# 抽象类

虚方法:子类可直接调用,可重写可补充写。

抽象属性:子类必须重写。

抽象方法:子类必须重写。

普通方法:子类可直接调用,如需定义同名方法,需用New关键字。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public abstract class AbstractClass
    {
        private string privateField;
        public string publicField;
        private string PrivateProperty { get; set; }
        public string PublicProperty { get; set; }
        public abstract string AbstractProperty { get; set; }

        public abstract void AbstractFunction();
        public abstract void AbstractFunction(string parameter);

        public virtual void VirtualFunction()
        {
            Console.WriteLine("VirtualFunction()");
        }
        public virtual void VirtualFunction(string parameter)
        {
            Console.WriteLine("VirtualFunction(string parameter)");
        }

        private void PrivateNormalFunction()
        {
            Console.WriteLine("PrivateNormalFunction()");
        }

        public void PublicNormalFunction()
        {
            Console.WriteLine("PublicNormalFunction()");
        }
        public void PublicNormalFunction(string parameter)
        {
            Console.WriteLine("PublicNormalFunction(string parameter)");
        }

        public AbstractClass()
        {
            Console.WriteLine("Constructor()");
        }

        public AbstractClass(string parameter)
        {
            Console.WriteLine("Constructor(string parameter)");
        }

        ~AbstractClass()
        {
            Console.WriteLine("Destructor()");
        }

    }

    public class Class : AbstractClass
    {

        public override string AbstractProperty { get; set; }//抽象属性必须重写

        public override void AbstractFunction()//抽象方法必须重写
        {
            Console.WriteLine("OverrideAbstractFunction()");
        }

        public override void AbstractFunction(string parameter)
        {
            Console.WriteLine("OverrideAbstractFunction(string parameter)");
        }

        public override void VirtualFunction()//虚方法可重写可不重写
        {
            Console.WriteLine("OverrideVirtualFunction()");
        }

        public override void VirtualFunction(string parameter)
        {
            Console.WriteLine("OverrideVirtualFunction()");
        }

        public new void PublicNormalFunction()//定义方法名称与抽象类定义的普通方法名称相同时需要用New
        {
            Console.WriteLine("PublicNewNormalFunction()");
        }
        public new void PublicNormalFunction(string parameter)
        {
            Console.WriteLine("PublicNewNormalFunction(string parameter)");
        }


        public void Test()
        {
            //base.PrivateProperty;//基类定义的私有属性具有一定的保护级别,无法访问
            PublicNormalFunction();
            PublicNormalFunction("");

            base.PublicNormalFunction();
            base.PublicNormalFunction("");


            VirtualFunction();
            VirtualFunction("");

            base.VirtualFunction();
            base.VirtualFunction("");
            
        }

        public Class()
        {
            Console.WriteLine("ClassConstructor()");
        }

        public Class(string parameter) : base(parameter)//不这么写会再调一次父类的无参数构造函数
        {
            Console.WriteLine("ClassConstructor(string parameter)");
        }
    }


    internal class Program
    {
        static void Main(string[] args)
        {
            Class class1 = new Class();
            Class class2 = new Class("");
            class1.Test();
            Console.ReadLine();

        }
    }
}

你可能感兴趣的:(c#,开发语言)