范范(6)+ 作业

property VS index

<span style="font-size:18px;"><span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    interface Student
    {
        int this[int y,int m,int d]{get;set;}
    }
    class MyCalender
    {
        private int y, m, d;
        public MyCalender()
        {

        }
        public MyCalender(int _y,int _m,int _d)
        {
            y = _y;
            m = _m;
            d = _d;
        }
        public string DayOfWeek
        {
            get
            {
                System.DateTime dt = new DateTime(y, m, d);
                return dt.DayOfWeek.ToString();
            }
        }
        public bool IsLeapYear
        {
            get
            {
                if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
                    return true;
                else
                    return false;
            }
        }

        public string this[int year,int month,int day]
        {
            get
            {

                System.DateTime dt = new DateTime(year, month, day);
                return dt.DayOfWeek.ToString();
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyCalender mc = new MyCalender();
           //Console.WriteLine(mc.DayOfWeek);
            Console.WriteLine(mc[2016, 3, 25]);
            Console.ReadLine();
        }
    }
}
</span>

http://www.cnblogs.com/tank110/archive/2010/10/18/1854013.html


<span style="font-size:18px;">http://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html</span>

范范的作业 : 重载indexer的this。

任务:写一个Population类,一个列表存储城市和人口,可以增删,可以查询,计算总人口,并用重载this的方法来获取人口数排行第n的城市。

<span style="font-size:18px;">using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Population
    {
        private Hashtable popluation = new Hashtable();

        public int this[string index]
        {
            get
            {
                return Int32.Parse(popluation[index].ToString());
            }
            set
            {
                popluation.Add(index, value);
            }
        }

        public string this[int n]
        {
            get
            {
                ArrayList al = new ArrayList(popluation.Values);
                al.Sort();
                al.Reverse();
                int value = Int32.Parse(al[n-1].ToString());
                foreach(DictionaryEntry d in popluation)
                {
                    if (Int32.Parse(d.Value.ToString()) == value)
                        return d.Key.ToString();
                }
                return null;
            }
            
        }
  
        private int totalPopulation;
        private int amount;
        private string topCity;

        public int TotalPopulation
        {
            get
            {
                int totalpop = 0;
                IDictionaryEnumerator en = popluation.GetEnumerator();
                while (en.MoveNext())
                {
                    totalpop += Int32.Parse(en.Value.ToString());
                }
                return totalpop;
            }

          
        }

        public int Amount
        {
            get
            {
                int count = 0;
                IDictionaryEnumerator en = popluation.GetEnumerator();
                while (en.MoveNext())
                {
                    count++;
                }
                return count;

            }

        }

        public string TopCity
        {
            get
            {
                string currentTopCity = "";
                int currentTopCount = 0;
                IDictionaryEnumerator en = popluation.GetEnumerator();
                while (en.MoveNext())
                {
                    if (Int32.Parse(en.Value.ToString()) > currentTopCount)
                    {
                        currentTopCount = Int32.Parse(en.Value.ToString());
                        currentTopCity = en.Key.ToString();
                    }
                       

                }
                return currentTopCity;
            }

           
        }

        public void AddCity(string city, int pop)
        {
            popluation.Add(city, pop);
        }
        public void RemoveCity(string city)
        {
            popluation.Remove(city);
        }
        public void ShowAllData()
        {
            IDictionaryEnumerator en = popluation.GetEnumerator();
            while (en.MoveNext())
            {
                Console.WriteLine(en.Key.ToString());
                Console.WriteLine(en.Value);
            }

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Population population = new Population();
            population["shanghai"] = 5000;
            population["shenzhen"] = 6000;
            //Console.Write(population["shanghai"]);
           // population.ShowAllData();
           // population.RemoveCity("shanghai");
           // population.ShowAllData();
            population.AddCity("guangzhou", 70000);
            population.ShowAllData();
           // Console.WriteLine(population.TotalPopulation);
           // Console.WriteLine(population.Amount);
           // Console.WriteLine(population.TopCity);
          Console.WriteLine(population[2]);
            Console.ReadLine();
        }
    }
}
</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值