时间:2021-05-20
复制代码 代码如下:
public class Fruit
{
string peach = "a round juicy fruit that has a soft yellow or red skin and a large hard seed in the center, or the tree that this fruit grows on";
string orange = "a round fruit that has a thick orange skin and is divided into parts inside";
string banana = "a long curved tropical fruit with a yellow skin";
string apple = "a hard round fruit that has red, light green, or yellow skin and is white inside ";
public string this[string fruitName]
{
get
{
switch (fruitName)
{
case "peach":
return peach;
case "orange":
return orange;
case "banana":
return banana;
case "apple":
return apple;
default:
throw new Exception("wrong fruit name");
}
}
set
{
switch (fruitName)
{
case "peach":
peach = value;
break;
case "orange":
orange = value;
break;
case "banana":
banana = value;
break;
case "apple":
apple = value;
break;
default:
throw new Exception("wrong fruit name");
}
}
}
}
class Program
{
static void Main(string[] args)
{
Fruit f = new Fruit();
//关联数组的方式访问get方法
Console.WriteLine(f["peach"]);
//关联数组的方式访问set方法
f["peach"] = "I like to eat peach.";
Console.WriteLine(f["peach"]);
Console.ReadLine();
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例展示了C#索引器的使用方法,对于C#的初学者来说是很有必要熟练掌握的,具体用法如下:首先,索引器(Indexer)是C#引入的一个新型的类成员,它使得类
本文实例讲述了C#简单的特殊输出。分享给大家供大家参考。具体实例如下:复制代码代码如下:classProgram{staticvoidMain(string[]
本文所述实例为C#动态加载一张图片并显示及动态移除它的实现方法,代码主要涉及一些C#图像操作知识,代码简单易懂,对C#的初学者有一定的帮助。主要功能代码如下:u
本文实例讲述了C#中简单的装箱操作。分享给大家供大家参考。具体如下:C#中装箱是:容许将值类型作为引用类型(比如:对象)进行处理的过程。下面的代码非常简单,可将
本文实例讲述了C#实现的简单随机数产生器功能。分享给大家供大家参考,具体如下:运行效果如下:具体代码如下:usingSystem;usingSystem.Col