1. 程式人生 > >c# 介面,抽象類,抽象方法的例項

c# 介面,抽象類,抽象方法的例項

用抽象方法和介面實現:

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();

        }
    }
}