日期:2014-05-18  浏览次数:20771 次

菜鸟提问,几个C#的继承问题
如下面这个例子:
public class Factor
  {
  public string Name { get; set; }
  public string Desc { get; set; }
}

public class Factores
  {
  public Factor F01;
  public Factor F02;
  public Factor F03;

  public string GetF01Desc(){
  Console.WriteLine(this.F01.Desc);
  }
  }

  public class WaterFactors : Factores
  {
  new Factor F01 = new Factor() { Name = "F01", Desc = "流量" };//为什么这里不能直接写 F01 = new Factor() { Name = "F01", Desc = "流量" }
  new Factor F02 = new Factor() { Name = "F02", Desc = "COD" };
  new Factor F03 = new Factor() { Name = "F03", Desc = "PH" };
  }

  public class GasFactors : Factores
  {
  public Factor F01 = new Factor() { Name = "F01", Desc = "SO2"};
  public Factor F02 = new Factor() { Name = "F02", Desc = "NOx" };
  public Factor F03 = new Factor() { Name = "F03", Desc = "烟尘" };
  }

  public class Test(){
  public void GetFactoreDesc(Factores f){
  Console.Write(f.GetF01Desc());
  }

  public vode t(){
  GasFactors gas = new WaterFactors();
  GetFactoreDesc(gas); //请问一下这一句为什么弹出 F01 is null呢?而不是“SO2”
  }
  }

------解决方案--------------------
你一天都在研究继承
------解决方案--------------------
探讨
public class GasFactors : Factores
{
public Factor F01 = new Factor() { Name = "F01", Desc = "SO2"};
public Factor F02 = new Factor() { Name = "F02", Desc = "NOx" };
public Factor F03 = new Factor() { Name = "F03", Desc = "烟尘" };
}