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);
                }
            }
            
        }
    }
  • 相关阅读:
    ARM标准汇编与GNU汇编
    使用友元,编译出错fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1786) 的解决
    C++中值传递,引用传递,指针传递
    C++命名空间的用法
    关于初始化C++类成员
    vivi的配置与编译
    C++ 容器
    vivi分区问题,及移植时需要修改的地方(转)
    基于S3C2410的VIVI移植
    拷贝构造函数什么时候调用?
  • 原文地址:https://www.cnblogs.com/kakaliush/p/1899737.html
Copyright © 2011-2022 走看看