C#练习——继承与重载、索引器

来源:互联网 发布:危险品物流软件有哪些 编辑:程序博客网 时间:2024/05/16 19:31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ReviewPractice
{
    class Person  //Person类
    {
        #region  创建一个Person类,属性:姓名、性别、年龄;方法:SayHi() 。再创建一个Employee类继承Person类,扩展属性Salary,重写SayHi方法。
        private string name;
        private int age;
        private string sex;


        public string Name
        {
            get
            {
                return name;
            }


            set
            {
                name = value;
            }
        }


        public int Age
        {
            get
            {
                return age;
            }


            set
            {
                age = value;
            }
        }


        public string Sex
        {
            get
            {
                return sex;
            }


            set
            {
                sex = value;
            }
        }
        public virtual void SayHi()
        {
            Console.WriteLine("Hi~~~in Person");
        }
        #endregion
    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ReviewPractice
{
    class Employee:Person  //Employee类继承Person类
    {
        private float salary;


        public float Salary
        {
            get
            {
                return salary;
            }


            set
            {
                salary = value;
            }
        }
        //方法重写
        public override void SayHi()
        {
            Console.WriteLine("Hi~~~in Employee");
        }
    }
}


//索引器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ReviewPractice
{
    class ItcastClass
    {
        #region  编写一个类:ItcastClass,该类中有一个私有字段_names,数据类型为:字符串数组,长度为5,并且有5个默认的姓名。要求:为ItcastClass类编写一个索引器,要求该索引器能够通过下标来访问_names中的内容。

        //索引器的目的在于方便,可在该类型的对象后面直接加一对中括号[]就能访问该类型的成员内容
        private string[] _names= { "迪丽热巴","赵丽颖","杨幂","刘诗诗","唐嫣"};
        public int Length
        {
            get
            {
                return _names.Length;
            }
        }
        //索引器与属性类似
        public string this[int index]
        {
            get
            {
                return _names[index];
            }
            set
            {
                _names[index] = value;
            }
        }
        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ReviewPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            //调用
            ItcastClass itClass = new ItcastClass();
            for (int i = 0; i < itClass.Length; i++)
            {
                Console.WriteLine(itClass[i]);
            }
            Console.ReadKey();
        }
    }
}

原创粉丝点击