unity游戏开发-C#语言基础篇(面向对象-继承带参和不带参 )

class Program
    {
        static void Main(string[] args)
        {
            RoundClass round = new RoundClass(2);//实例化子类函数时,先执行父类构造函数,再执行子类的构造函数;
            round.show();



             SphereClass sphere = new SphereClass();
            sphere.d; //通过实例化也调用不了d,因为d是受保护的,只能给父类和子类使用;要调用d必须改权限 
            round.a// 点不出来,a在父类是私有的;






            //练习

            SphereClass sph = new SphereClass();

            RoundClass rou = new RoundClass();

            rou.ShowDate();

            Console.ReadKey();

        }
    }
 class RoundClass : SphereClass//子类:父类
    {
        public RoundClass(int rr)
            : base("1")
        {//调用父类带参构造函数;// 子类构造函数(形参) base(实参)
            Console.WriteLine("这是子类的构造函数!");
        }

        public void ShowDate()
        {
            Console.WriteLine("这是子类的普通方法!");
        }

        public void show()
        {

            Console.WriteLine("半径是:{0}直径是:{1}", r, d);//子类直接调用父类的属性或 字段
            this.ShowSphere();
            this.ShowDate();
            base.ShowDate();

        }

        public RoundClass()
            : base()
        {

            Console.WriteLine("这是子类的不带参函数!");
        }

        public RoundClass(int rr)
            : base(rr)
        {
            this.R = rr;
            Console.WriteLine("这是子类的带参函数!");
        }

        public void Show()
        {

            Console.WriteLine("这是子类的普通方法");
        }

        public void ShowDate()
        {

            this.ShowSphere();
            this.Show();
            base.Show();
        }

    }
 class SphereClass
    {
        public int r;

        //private int a;
        protected int d;


        public int R
        {//属性
            set { r = value; }
            get { return r; }
        }

        public SphereClass()
        {//构造函数初始化;
            r = 5;
            d = 2 * this.r;
            Console.WriteLine("这是不带参父类的构造函数!");
        }

        public SphereClass(int _r)
        {//构造函数初始化;父类带参构造函数
            // r = _r;

            Console.WriteLine("这是带参父类的构造函数!");
        }


        public void ShowDate()
        {
            Console.WriteLine("这是父类的普通方法!");
        }

        public void ShowSphere()
        {

            Console.WriteLine("半径是:{0}直径是:{1}", this.R, d);
        }


        private int r;
        protected int d;

        public int R
        {
            set { r = value; }
            get { return r; }
        }

        public SphereClass()
        {
            r = 5;
            d = 2 * r;
            Console.WriteLine("这是父类不带参构造函数!");

        }

        public SphereClass(int _r)
        {
            r = _r;
            d = 2 * r;
            Console.WriteLine("这是父类带参的函数!");


        }


        public void Show()
        {
            Console.WriteLine("这是父类的普通方法!");
        }

        public void ShowSphere()
        {
            Console.WriteLine("半径{0} 直径{1}", r, d);

        }
    }

你可能感兴趣的:(unity游戏开发-C#语言基础篇(面向对象-继承带参和不带参 ))