博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Abstract (抽象类、接口、重写、覆写、重载)
阅读量:6591 次
发布时间:2019-06-24

本文共 11664 字,大约阅读时间需要 38 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MyAbstract.Abstract{    ///     /// 抽象类是一个类,里面可以包含一切类可以包含的    /// 抽象成员 必须包含在抽象类里面,抽象类还可以包含普通成员    /// 继承抽象类后,必须显示的override其抽象成员    /// 抽象类不能直接实例化,声明的对象只能使用抽象类里的方法,不能用子类新增的方法    /// 父类只有一个    /// is a    ///     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");        }        ///         /// 品牌        ///         /// 
public abstract string Brand(); /// /// 系统 /// ///
public abstract string System(); /// /// 打电话 /// public abstract void Call(); /// /// 拍照 /// public abstract void Photo(); public abstract void Do
(); }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MyAbstract.Abstract{    ///     /// 接口不是类,里面可以包含属性、方法、事件   不能包含字段,委托,不能用访问修饰符    /// 接口只能包含没有实现的方法    /// 实现接口的话,必须实现全部方法    /// 接口不能直接实例化,声明的对象只能使用接口里的方法,不能用子类新增的方法    /// 接口可以实现多个    /// can do    ///     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{    ///     /// 手机    ///     public class iPhone : BasePhone, IExtend, IPay    {        ///         /// 品牌        ///         /// 
public override string Brand() { return "iPhone"; } /// /// 系统 /// ///
public override string System() { return "IOS"; } /// /// 打电话 /// public override void Call() { Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System()); } /// /// 拍照 /// public override void Photo() { Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System()); } public override void Do
() { } 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{    ///     /// 手机    ///     public class Lumia : BasePhone,IExtend    {        ///         /// 品牌        ///         /// 
public override string Brand() { return "Nokia"; } /// /// 系统 /// ///
public override string System() { return "Winphone"; } /// /// 打电话 /// public override void Call() { Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System()); } /// /// 拍照 /// 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
() { } 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{    ///     /// 手机    ///     public class Mi : BasePhone    {        ///         /// 品牌        ///         /// 
public override string Brand() { return "XiaoMi"; } /// /// 系统 /// ///
public override string System() { return "Android"; } /// /// 打电话 /// public override void Call() { Console.WriteLine("User{0} {1} {2} Call", this.GetType().Name, this.Brand(), this.System()); } /// /// 拍照 /// public override void Photo() { Console.WriteLine("User{0} {1} {2} Photo", this.GetType().Name, this.Brand(), this.System()); } public override void Do
() { } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MyAbstract.Polymorphism{    ///     /// 普通方法是由编译时决定的    ///   虚方法是由运行时决定的    ///     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    {        ///         /// CommonMethod        ///         public void CommonMethod()        {            Console.WriteLine("ParentClass CommonMethod");        }        ///         /// virtual  虚方法  必须包含实现 但是可以被重载        ///         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    {        ///         /// new 隐藏        ///         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");        }        ///         /// virtual 可以被覆写        ///         ///         /// 
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{    ///     /// 1 作业讲解    /// 2 封装继承多态    /// 3 抽象类接口    /// 4 重写overwrite(new)  覆写override 重载overload(方法)     ///     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) 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(); } }}

 

转载于:https://www.cnblogs.com/zhengqian/p/8493256.html

你可能感兴趣的文章
华丽的设计,20个免费的图标字体
查看>>
任意不规则形状的图片剪裁 .
查看>>
asp.net负载均衡方案[转]
查看>>
利用备份技术获取apk本地存储数据
查看>>
【POJ】3133 Manhattan Wiring
查看>>
VB中的冒号——bug
查看>>
微软职位内部推荐-Software Development Engineer 2
查看>>
mysql触发器使用实例
查看>>
工作流和审批流
查看>>
使用JPedal取代PDFBox
查看>>
uva--562Dividing coins +dp
查看>>
underscore 1.7.0 api
查看>>
C# CheckedListBox控件的使用方法
查看>>
spring Transaction Management --官方
查看>>
jar的打包与共享
查看>>
iOS开发-清理缓存功能的实现
查看>>
linux----关于定位和查找
查看>>
ci创建zip
查看>>
IS_ERR、PTR_ERR、ERR_PTR
查看>>
html5 canvas 奇怪的形状垂直渐变
查看>>