Code
using System;
namespace TestSpace
{
public class CD
{
public string name;
public double price;
public string type;
public CD(string n,double p,string t)
{name=n;price=p;type=t}
}
public class VideoShop
{
private CD[] cdCollection=new CD[10];
private int cdCount=0;
public int Count{get{return cdCount;}}//返回当前CD数目
public CD this[string name]//按CD名称返回对象
{
get {foreach(CD t in cdCollection)
if(t.name==cdName)return t;
return null;
}
}
publice CD this[string cdName,string type]
{ //按CD名称和类型返回CD对象
get {foreach(CD t in cdCollection)
if(t.name==cdName && t.type==type)return t;
return null;
}
}
public CD this[string name]
{ //如果店里没有此CD则cdCount增加
set {if(cdCollection[n]==null)cdCount++;
if(value==null && cdCollection[n]!=null)
cdCount--;
cdCollection[n]=value;
}
get{return cdCollection[n];}
}
}
class Test
{
[STAThread]
static void Main(string[] args)
{
VideoShop myshop=new VideoShop();
CD cd1=new CD("误入歧途",10.5,"DVD");
CD cd2=new CD("罗马假日",5.5,"VCD");
myshop[myshop.Count]=cd1;
myshop[myshop.Count]=cd2;
CD a=myshop["罗马假日"];
CD b=myshop["误入歧途","DVD"];
Console.WriteLine(a.type+a.name.a.price);
Console.WriteLine(b.type+b.name.b.price);
}
}
}
using System;
namespace TestSpace
{
public class CD
{
public string name;
public double price;
public string type;
public CD(string n,double p,string t)
{name=n;price=p;type=t}
}
public class VideoShop
{
private CD[] cdCollection=new CD[10];
private int cdCount=0;
public int Count{get{return cdCount;}}//返回当前CD数目
public CD this[string name]//按CD名称返回对象
{
get {foreach(CD t in cdCollection)
if(t.name==cdName)return t;
return null;
}
}
publice CD this[string cdName,string type]
{ //按CD名称和类型返回CD对象
get {foreach(CD t in cdCollection)
if(t.name==cdName && t.type==type)return t;
return null;
}
}
public CD this[string name]
{ //如果店里没有此CD则cdCount增加
set {if(cdCollection[n]==null)cdCount++;
if(value==null && cdCollection[n]!=null)
cdCount--;
cdCollection[n]=value;
}
get{return cdCollection[n];}
}
}
class Test
{
[STAThread]
static void Main(string[] args)
{
VideoShop myshop=new VideoShop();
CD cd1=new CD("误入歧途",10.5,"DVD");
CD cd2=new CD("罗马假日",5.5,"VCD");
myshop[myshop.Count]=cd1;
myshop[myshop.Count]=cd2;
CD a=myshop["罗马假日"];
CD b=myshop["误入歧途","DVD"];
Console.WriteLine(a.type+a.name.a.price);
Console.WriteLine(b.type+b.name.b.price);
}
}
}