Sortie de l'indexeur que vous avez enseigné dans le passé
Pas en Java
Program.cs
using System;
using System.Collections.Generic;
namespace IndexerLesson
{
    public class Colors
    {
        private string[] data = { "rouge", "Bleu", "Jaune" };
       
         //Accéder à la valeur de retour this[Argument de type]
        public string this[int index]{
            set{
                this.data[index] = value;
            }
            get{
                return data[index];
            }
        }
    }
}
Colors.cs
using System;
using System.Collections.Generic;
namespace IndexerLesson
{
    public class Colors
    {
        private string[] data = { "rouge", "Bleu", "Jaune" };
        public string this[int index]
        {
            set
            {
                this.data[index] = value;
            }
            get
            {
                return data[index];
            }
        }
    }
}
Surcharge possible
JMonth.cs
using System;
using System.Collections.Generic;
namespace IndexerLesson
{
    public class JMonth
    {
        private string[] months = { "janvier", "février", "Yayoi", "Uzuki", "Satsuki", "Minazuki", "juillet", "Hazuki", "Plusieurs mois", "Kannazuki", "Shimotsuki", "Course de maître" };
        public string this[int index]
        {
            get
            {
                return months[index - 1];
            }
        }
        public int this[string name]
        {
            get
            {
                return Array.IndexOf(months, name) + 1;
            }
        }
    }
}
Profram.cs
using System;
using System.Collections.Generic;
namespace IndexerLesson
{
    class Class1
    {
        static void Main(string[] args)
        {
            JMonth jMonth = new JMonth();
            Console.WriteLine(jMonth[6]);
            Console.WriteLine(jMonth["Kannazuki"]);
        }
    }
}
C'est la reconnaissance.
Recommended Posts