C#父类与子类之间关系的妙用

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            AppleClass AppleClass = new AppleClass();
            AppleClass.Name = "apple";
            AppleClass.Ap = "ap";
            Fruilt fruilt = (Fruilt)AppleClass;
            if (fruilt.FruiltType == EnumFruiltType.Apple)
            {
                var aa = (AppleClass)fruilt;
            }
        }

        public class Fruilt
        {
            public EnumFruiltType FruiltType { get; set; }
            public string Name { get; set; }

        }
        public class AppleClass : Fruilt
        {
            public string Ap { get; set; }
            public AppleClass()
            {
                FruiltType = EnumFruiltType.Apple;
            }
        }
        public class OrangeClass : Fruilt
        {
            public string or { get; set; }
            public OrangeClass()
            {
                FruiltType = EnumFruiltType.Orange;
            }
        }

        public enum EnumFruiltType
        {
            Apple = 0,
            Orange = 1
        }

    }
}

 

 

 

 

posted @ 2024-06-03 16:03  竹林听雨行  阅读(6)  评论(0编辑  收藏  举报