C# 乱七八糟的栗子

163 阅读1分钟

乱七八糟的栗子

using System;
using System.Diagnostics;

namespace ConsoleApp2
{
    public interface ICharacter
    {
        int ID { get; set; }
        int teamID { get; set; }
        bool PrintTeamID(string str);
    }

    //抽象类继承接口 以抽象方式实现接口中的成员
    public abstract class Role : ICharacter
    {
        public Role(int id)
        {
            this.ID = id;
            Console.WriteLine("抽象类Role的有参构造函数被调用!");
        }

        public abstract int ID { get; set; }
        public abstract int teamID { get; set; }

        //在父类中定义的抽象方法不能实现(也就是没有方法体,定义的时候去掉大括号{})。
        public abstract bool PrintTeamID(string str);

        //抽象类中 可以定义非抽象属性
        private int blood;
        public int Blood
        {
            get
            {
                return blood;
            }
            set
            {
                if (value < 0)
                {
                    blood = 0;
                }
                else
                {
                    blood = value;
                }
            }
        }

        //虚方法
        public virtual void ShowBlood()
        {
            Console.WriteLine("血量是{0}", Blood);
        }
    }

    // 隐式实现接口成员
    // 密封类不能被继承,密封方法不能被重写。
    public sealed class Monster : ICharacter
    {
        public int ID { get; set; }

        public int teamID { get; set; }

        public bool PrintTeamID(string str)
        {
            Console.WriteLine("class monster");
            return true;
        }

        public int monsterBlood;
    }

    public class Player : Role
    {
        // 构造函数 同时调用了父类的构造函数
        public Player(int id, int team_id) : base(id)
        {
            this.ID = id;
            this.teamID = team_id;
        }
        // 重写了抽象属性和方法
        public override int ID { get; set; }
        public override int teamID { get; set; }

        public override bool PrintTeamID(string str)
        {
            Console.WriteLine(teamID);
            return true;
        }

        // 重写了父类的虚方法
        public sealed override void ShowBlood()
        {
            // 调用父类的虚方法
            base.ShowBlood();
        }
    }

    public class Test : Player
    {
        //父类存在有参构造函数
        public Test():base(1,1001)
        {

        }

        public override bool PrintTeamID(string str)
        {
            Console.WriteLine("class test "+ str);
            return true;
        }

        //父类密封方法 无法重写
        //public override void ShowBlood()
        //{
        //    Console.WriteLine("class test ");
        //}
    }


    class Program
    {
        static void Main(string[] args)
        {
            Player player1 = new Player(1, 1001);
            player1.ShowBlood();
            Console.WriteLine(player1.ID);

            Player player2 = new Test();
            player2.PrintTeamID("player 类型 的 test实例");
            player2.ShowBlood();

            Monster monster1 = new Monster();
            monster1.teamID = 1002;
            monster1.PrintTeamID("");

            Console.ReadKey();
        }
    }
}