• Abstract (抽象类、接口、重写、覆写、重载)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAbstract.Abstract
    {
        /// <summary>
        /// 抽象类是一个类,里面可以包含一切类可以包含的
        /// 抽象成员 必须包含在抽象类里面,抽象类还可以包含普通成员
        /// 继承抽象类后,必须显示的override其抽象成员
        /// 抽象类不能直接实例化,声明的对象只能使用抽象类里的方法,不能用子类新增的方法
        /// 父类只有一个
        /// is a
        /// </summary>
        public abstract class BasePhone
        {
            public int Id { get; set; }
            public string Name = "123";
            public delegate void DoNothing();
            public event DoNothing DoNothingEvent;
    
            public void Show()
            {
                Console.WriteLine("这里是Show1");
            }
            public virtual void ShowVirtual()
            {
                Console.WriteLine("这里是Show");
            }
    
            /// <summary>
            /// 品牌
            /// </summary>
            /// <returns></returns>
            public abstract string Brand();
    
            /// <summary>
            /// 系统
            /// </summary>
            /// <returns></returns>
            public abstract string System();
    
            /// <summary>
            /// 打电话
            /// </summary>
            public abstract void Call();
    
    
            /// <summary>
            /// 拍照
            /// </summary>
            public abstract void Photo();
    
            public abstract void Do<T>();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAbstract.Abstract
    {
        /// <summary>
        /// 接口不是类,里面可以包含属性、方法、事件   不能包含字段,委托,不能用访问修饰符
        /// 接口只能包含没有实现的方法
        /// 实现接口的话,必须实现全部方法
        /// 接口不能直接实例化,声明的对象只能使用接口里的方法,不能用子类新增的方法
        /// 接口可以实现多个
        /// can do
        /// </summary>
        public interface IExtend
        {
            //event Action DoNothingEvent;
            //int Id { get; set; }
            void Game();
            //List<>
        }
    
        public interface IPay
        {
            void Pay();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAbstract.Abstract
    {
        public class iPad : IExtend, IPay
        //: BasePhone
        {
            public void Game()
            {
                throw new NotImplementedException();
            }
    
            public void Pay()
            {
                throw new NotImplementedException();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAbstract.Abstract
    {
        /// <summary>
        /// 手机
        /// </summary>
        public class iPhone : BasePhone, IExtend, IPay
        {
            /// <summary>
            /// 品牌
            /// </summary>
            /// <returns></returns>
            public override string Brand()
            {
                return "iPhone";
            }
    
            /// <summary>
            /// 系统
            /// </summary>
            /// <returns></returns>
            public override string System()
            {
                return "IOS";
            }
    
            /// <summary>
            /// 打电话
            /// </summary>
            public override void Call()
            {
                Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
            }
    
            /// <summary>
            /// 拍照
            /// </summary>
            public override void Photo()
            {
                Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
            }
    
    
            public override void Do<T>()
            {
    
            }
    
            public void Game()
            {
                throw new NotImplementedException();
            }
    
            public void Pay()
            {
                throw new NotImplementedException();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAbstract.Abstract
    {
        /// <summary>
        /// 手机
        /// </summary>
        public class Lumia : BasePhone,IExtend
        {
            /// <summary>
            /// 品牌
            /// </summary>
            /// <returns></returns>
            public override string Brand()
            {
                return "Nokia";
            }
    
            /// <summary>
            /// 系统
            /// </summary>
            /// <returns></returns>
            public override string System()
            {
                return "Winphone";
            }
    
            /// <summary>
            /// 打电话
            /// </summary>
            public override void Call()
            {
                Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
            }
    
            /// <summary>
            /// 拍照
            /// </summary>
            public override void Photo()
            {
                Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
            }
    
            public void Metro()
            {
                Console.WriteLine("This is Metro");
            }
            public override void Do<T>()
            {
    
            }
    
            public void Game()
            {
                throw new NotImplementedException();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAbstract.Abstract
    {
        /// <summary>
        /// 手机
        /// </summary>
        public class Mi : BasePhone
        {
            /// <summary>
            /// 品牌
            /// </summary>
            /// <returns></returns>
            public override string Brand()
            {
                return "XiaoMi";
            }
    
            /// <summary>
            /// 系统
            /// </summary>
            /// <returns></returns>
            public override string System()
            {
                return "Android";
            }
    
            /// <summary>
            /// 打电话
            /// </summary>
            public override void Call()
            {
                Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System());
            }
    
            /// <summary>
            /// 拍照
            /// </summary>
            public override void Photo()
            {
                Console.WriteLine("User{0} {1} {2} Photo", this.GetType().Name, this.Brand(), this.System());
            }
            public override void Do<T>()
            {
    
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAbstract.Polymorphism
    {
        /// <summary>
        /// 普通方法是由编译时决定的
        ///   虚方法是由运行时决定的
        /// </summary>
        public class Poly
        {
            public static void Test()
            {
                Console.WriteLine("*******************************************");
                Console.WriteLine("*******************************************");
                Console.WriteLine("*******************************************");
                ParentClass instance = new ChildClass();
                Console.WriteLine("下面是instance.CommonMethod()");
                instance.CommonMethod();
                Console.WriteLine("下面是instance.VirtualMethod()");
                instance.VirtualMethod();
                Console.WriteLine("下面是instance.AbstractMethod()");
                instance.AbstractMethod();
    
    
                Console.WriteLine("*******************************************");
                Console.WriteLine("*******************************************");
                Console.WriteLine("*******************************************");
            }
        }
    
        #region abstract
        public abstract class ParentClass
        {
            /// <summary>
            /// CommonMethod
            /// </summary>
            public void CommonMethod()
            {
                Console.WriteLine("ParentClass CommonMethod");
            }
    
            /// <summary>
            /// virtual  虚方法  必须包含实现 但是可以被重载
            /// </summary>
            public virtual void VirtualMethod()
            {
                Console.WriteLine("ParentClass VirtualMethod");
            }
    
            public virtual void VirtualMethod(string name)
            {
                Console.WriteLine("ParentClass VirtualMethod");
            }
    
            //public override bool Equals(object obj)
            //{
            //    return base.Equals(obj);
            //}
    
            public abstract void AbstractMethod();
        }
    
        public class ChildClass : ParentClass
        {
            /// <summary>
            /// new 隐藏
            /// </summary>
            public new void CommonMethod()
            {
                Console.WriteLine("ChildClass CommonMethod");
            }
    
            public void CommonMethod(string name)
            {
                Console.WriteLine("ChildClass CommonMethod");
            }
            //public void CommonMethod(int id)
            //{
            //    Console.WriteLine("ChildClass CommonMethod");
            //}
            //public void CommonMethod(int id, string name)
            //{
            //    Console.WriteLine("ChildClass CommonMethod");
            //}
            public void CommonMethod(int id, string name = "", string des = "", int size = 0)
            {
                Console.WriteLine("ChildClass CommonMethod");
            }
            public void CommonMethod(string name, int id)
            {
                Console.WriteLine("ChildClass CommonMethod");
            }
    
            /// <summary>
            /// virtual 可以被覆写
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public override void VirtualMethod()
            {
                Console.WriteLine("ChildClass VirtualMethod");
                base.VirtualMethod();
            }
    
            public sealed override void AbstractMethod()
            {
                Console.WriteLine("ChildClass AbstractMethod");
            }
        }
    
        public class GrandClass : ChildClass
        {
    
            //public override void AbstractMethod()
            //{
            //    base.AbstractMethod();
            //}
    
            public override void VirtualMethod()
            {
                base.VirtualMethod();
            }
        }
        #endregion abstract
    
    }
    using MyAbstract.Abstract;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAbstract
    {
        /// <summary>
        /// 1 作业讲解
        /// 2 封装继承多态
        /// 3 抽象类接口
        /// 4 重写overwrite(new)  覆写override 重载overload(方法) 
        /// </summary>
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("欢迎来到.net高级班vip课程,今天是Eleven老师为大家带来的接口抽象类课程");
                    Student student = new Student()
                    {
                        Id = 423,
                        Name = "假洒脱"
                    };
                    {
                        BasePhone phone = new Lumia();
                        //student.PlayLumia(phone);
                        student.PlayPhone(phone);
                    }
                    {
                        //BasePhone phone = new BasePhone();
                        //BasePhone phone = new Lumia();
                        //phone.Metro();
    
                        IExtend phone = new iPhone();
                        //IExtend phone = new IExtend();
                        //phone.Call();
                    }
                    {
                        BasePhone phone = new iPhone();
                        phone.Call();
                        //student.PlayIPhone(phone);
                        student.PlayPhone(phone);
                    }
                    Polymorphism.Poly.Test();
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.Read();
            }
        }
    }
    using MyAbstract.Abstract;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAbstract
    {
    
        public class People
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    
        public class Student : People
        {
            private int Tall { get; set; }
    
            //public void Play<T>(T t) where T:
            //{
            //    Console.WriteLine("This is {0} play {1}", this.Name, lumia.GetType());
            //    t.Brand();
            //    t.Call();
            //    t.Photo();
            //}
    
            public void PlayPhone(BasePhone phone)
            {
                Console.WriteLine("This is {0} play {1}", this.Name, phone.GetType());
                phone.Brand();
                phone.Call();
                phone.Photo();
            }
    
            public void PlayLumia(Lumia lumia)
            {
                Console.WriteLine("This is {0} play {1}", this.Name, lumia.GetType());
                lumia.Brand();
                lumia.Call();
                lumia.Photo();
            }
            public void PlayIPhone(iPhone lumia)
            {
                Console.WriteLine("This is {0} play {1}", this.Name, lumia.GetType());
                lumia.Brand();
                lumia.Call();
                lumia.Photo();
            }
    
            
        }
        public class Teacher : People
        {
            private int Tall { get; set; }
    
            public void PlayLumia(Lumia lumia)
            {
                Console.WriteLine("This is {0} play {1}", this.Name, lumia.GetType());
                lumia.Brand();
                lumia.Call();
                lumia.Photo();
            }
        }
    }
  • 相关阅读:
    Ubuntu16.04 + OpenCV源码 + Qt5.10 安装、配置
    DML和DQL
    初识MySql
    表单校验
    使用jQuery操作DOM
    jQuery中的事件与动画
    jQuery选择器
    初识jQuery
    JavaScript对象及初识OOP
    JavaScript操作DOM对象
  • 原文地址:https://www.cnblogs.com/zhengqian/p/8493256.html
Copyright © 2020-2023  润新知