C#基础问题希望得到您的帮助2
abstract class MobileCunCu
{
public MobileCunCu(string name)
{
this.name = name;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
abstract public void Read();
abstract public void Write();
}
class UPan:MobileCunCu
{
public override void Read()
{
//throw new NotImplementedException();
Console.WriteLine("U盘读");
}
public override void Write()
{
Console.WriteLine("U盘写");
//throw new NotImplementedException();
}
class MobileYingPan:MobileCunCu
{
public override void Read()
{
Console.WriteLine("移动硬盘读");
//throw new NotImplementedException();
}
public override void Write()
{
Console.WriteLine("移动硬盘写");
//throw new NotImplementedException();
}
}
class Computer
{
public Computer()
{
}
public Computer(MobileCunCu storage)
{
this.storage = storage;
}
private MobileCunCu storage;
public MobileCunCu Storage
{
get { return storage; }
set { storage = value; }
}
public void Read()
{
storage.Read();
}
public void Write()
{
storage.Write();
}
}
static void Main(string[] args)
{
//Computer pc = new Computer(new FlashDisk());
//pc.Read();
Computer pc = new Computer(new MP3());
pc.Read();
MP3 mp3 = pc.Storage as MP3;
if (mp3 != null)
{
mp3.Play();
}
Console.Read();
}
问题在computer 类中属性MobileCunCu storage 什么意思,希望您解答详细一点谢谢
------解决方案--------------------
private MobileCunCu storage;
public MobileCunCu Storage
{
get { return storage; }
set { storage = value; }
}
这些的意思是创建一个类的对象,你可以把它想成是一个“类变量”,
public Computer(MobileCunCu storage)
{
this.storage = storage;
}
这个就是把storage这个“类”当做参数传进去,赋值给Computer下的变量storage