首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#基础有关问题希望得到您的帮助2

2012-09-03 
C#基础问题希望得到您的帮助2abstract class MobileCunCu{public MobileCunCu(string name){this.name n

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

热点排行