• 今天有个朋友问我抽象方法和接口的区别,为了解释清楚这个事情,我在网上看到一篇文章讲的非常好给大家分享一下,也感谢原作者的付出


    获取原地址:

    版权声明:本文为CSDN博主「私人专属cry」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_42469910/article/details/82712482

    用抽象方法和接口实现:

    1 创建一个人的类包含属性:姓名,年龄,性别,住址  对属性进行封装,自我介绍,吃的方法
    2 创建一个男人的类继承于人的类,创建一个独有的特性:体重,对自我介绍的方法进行重写(输出:我是?,我来自于?,我的性别是?)
    3 创建一个女人的类继承于人的类,创建一个独有的特性:身高,对自我介绍的方法进行重写(输出:我是?,我来自于?)
    4 定义一个测试类,对男人的信息和女人的信息进行输出(里氏替换原则)

     

    这是一个抽象的方法,先定义属性,进行封装(sg 身高 和tz 体重  是因为在方法里面要用到,所以加上)

    然后将自我介绍和吃的方法写上:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
         
        namespace _009
        {
           public abstract class person
            {
                //姓名,年龄,性别,住址
                private string pname;
                private int page;
                private char psex;
                private string paddress;
                private string sg;
                private string tz;
         
                public string Pname
                {
                    get
                    {
                        return pname;
                    }
         
                    set
                    {
                        pname = value;
                    }
                }
         
                public int Page
                {
                    get
                    {
                        return page;
                    }
         
                    set
                    {
                        page = value;
                    }
                }
         
                public char Psex
                {
                    get
                    {
                        return psex;
                    }
         
                    set
                    {
                        psex = value;
                    }
                }
         
                public string Paddress
                {
                    get
                    {
                        return paddress;
                    }
         
                    set
                    {
                        paddress = value;
                    }
                }
                public abstract void showper();//自我介绍
                public abstract void eatper();//吃
            }
        }
    ————————————————

    写一个接口

    写上吃和男人自我介绍,女人自我介绍的方法:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
         
        namespace _009
        {
             interface IPerson
            {
                void eat();//吃
                void show(string name, int age, char sex, string address,string tz);//男人的自我介绍
                void show(string name,string address,string sg);//女人的自我介绍
            }
        }
    ————————————————
     建一个男人和女人的类,继承并同时实现上面抽象方法和接口,将方法重载或实现:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
         
        namespace _009
        {
            class Man : person,IPerson
            {
                 string tz;
         
                public string Tz
                {
                    get
                    {
                        return tz;
                    }
         
                    set
                    {
                        tz = value;
                    }
                }
                
                public void eat()
                {
                    Console.WriteLine("我不和你玩了,我要回家吃饭");
                    Console.ReadKey();
                }
         
                public void show(string name, string address, string sg)
                {
                }
         
                public void show(string name, int age, char sex, string address)
                {
         
                }
         
                public void show(string name, int age, char sex, string address, string tz)
                {
                    Console.WriteLine("大家好,我叫{0}性别{1}年龄{2}来自{3}体重{4}", name, age, sex, address, tz);
                    Console.ReadKey();
                }
         
                public override void eatper()
                {
                    Console.WriteLine("我中午还没吃饭呢");
                    Console.ReadKey();
                }
                
                public override void showper()
                {
                    Console.WriteLine("大家好,我叫{0}性别{1}来自{2}体重{3}",base.Pname,base.Psex,base.Paddress,this.Tz);
                    Console.ReadKey();
                }
            }
        }
    ————————————————
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace _009
    {
        class Wuman : person, IPerson
        {
            private string sg;
     
            public string Sg
            {
                get
                {
                    return sg;
                }
     
                set
                {
                    sg = value;
                }
            }
     
            public void eat()
            {
                Console.WriteLine("我不和你玩了,我要回家吃饭");
                Console.ReadKey();
            }
     
            public void show(string name, string address, string sg)
            {
                Console.WriteLine("我叫{0}来自{1}身高{2}",name,address,sg);
                Console.ReadKey();
            }
     
            public void show(string name, int age, char sex, string address)
            {
                Console.WriteLine("大家好,我叫{0}性别{1}年龄{2}来自{3}", name, age, sex, address);
                Console.ReadKey();
            }
     
            public void show(string name, int age, char sex, string address, string tz)
            {
            }
     
            public override void eatper()
            {
                Console.WriteLine("哼,我中午不吃饭了");
                Console.ReadKey();
            }
     
            public override void showper()
            {
                Console.WriteLine("大家好,我叫{0}来自{1}身高{2}", base.Pname,base.Paddress,this.Sg);
                Console.ReadKey();
            }
        }
    }
    ————————————————
     写一个测试类进行测试:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
         
        namespace _009
        {
            class Program
            {
                static void Main(string[] args)
                {
                    IPerson ip = new Wuman();
                    IPerson iip = new Man();
                    iip.eat();
                    iip.show("小张", 18, '男', "厕所", "120");
                    ip.eat();
                    ip.show("小芳","丽春院","165");
         
                    Console.WriteLine("............................");
                    Man pe = new Man();
                    pe.Pname = "小王";
                    pe.Psex = '男';
                    pe.Paddress = "火星";
                    pe.Tz = "120";
                    pe.eatper();
                    pe.showper();
         
                    Wuman pa = new Wuman();
                    pa.Pname = "球球";
                    pa.Paddress = "长沙";
                    pa.Sg = "165";
                    pa.eatper();
                    pa.showper();
         
                }
            }
        }
    ————————————————
    版权声明:本文为CSDN博主「私人专属cry」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_42469910/article/details/82712482

  • 相关阅读:
    hdu4273Rescue(三维凸包重心)
    hdu4449Building Design(三维凸包+平面旋转)
    hdu3847Trash Removal(凸包)
    CodeForces 166B (凸包)
    机器学习文章导航
    梯度下降法深入
    插值法
    离散系统频域分析
    离散系统时域分析
    连续系统频域分析
  • 原文地址:https://www.cnblogs.com/sdjxcolin/p/12807492.html
Copyright © 2020-2023  润新知