zoukankan      html  css  js  c++  java
  • 索引器的重载的一个例子


    //--------------------------------------
    // 索引器的重载
    // 索引器永远属于实例成员,所以不能为static
    //--------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Collections;

    namespace indexoverLoad
    {
        
    class MainClass
        {
            
    public static void Main(string[] args)
            {
                IndexClass indexclass 
    = new IndexClass();
                indexclass[
    0= "张三";
                indexclass[
    1= "李四";
                indexclass[
    2= "王五";
                
                Console.WriteLine(indexclass[
    0]);
                Console.WriteLine(indexclass[
    1]);
                Console.WriteLine(indexclass[
    2]);
                
                IndexStringClass indexStringClass 
    = new IndexStringClass();
                indexStringClass[
    1= "001号";
                indexStringClass[
    2= "002号";
                indexStringClass[
    3= "003号";
                
                Console.WriteLine(indexStringClass[
    "001号"]);
                Console.WriteLine(indexStringClass[
    "002号"]);
                Console.WriteLine(indexStringClass[
    "003号"]);
                
                Console.ReadKey();
            }
        }
        
    class IndexClass
        {
            
    private string[] name = new string[10];
            
    public string this[int index]
            {
                
    get{return name[index];}
                
    set
                {
                    
    if(index >= 0 && index<10)
                        name[index] 
    = value;
                }
                
            }
        }
        
    class IndexStringClass
        {
            
    private Hashtable name = new Hashtable();
            
            
    public string this[int index]
            {
                
    get{return name[index].ToString();}
                
    set
                {
                    name.Add(index,value);
                }    
            }

            
    public int this[string _name]
            {
                
                
    get
                {
                    
    foreach (DictionaryEntry d in name)
                    {
                        
    if(d.Value.ToString() == _name)
                        {
                            
    return Convert.ToInt32(d.Key);
                        }
                    }
                    
    return -1;
                }
                
    set
                {
                    name.Add(value,_name);
                }
            }
            
        }
    }
  • 相关阅读:
    关于iOS开发property with 'retain(or strong)' attribute must be of object type
    机器学习之神经网络
    一些知名的开源社区
    机器学习之正则化
    机器学习之逻辑回归(logistic回归)
    机器学习之正规方程法
    机器学习之线性回归、多项式回归
    机器学习之梯度下降法
    64位windows7下安装python,配置numpy和matplotlib库
    mysql分区查询
  • 原文地址:https://www.cnblogs.com/kakaliush/p/1899737.html
Copyright © 2011-2022 走看看