2017-01-24 99 views
2

此方法應該打印以下:打印命名空間而不是字符串值

Creating r1 
-> Assigning r2 to r1 
-> Changing values of r2 
String = This is new info!, Top = 10, Bottom = 50, Left = 10, Right = 50 
String = This is new info!, Top = 10, Bottom = 4444, Left = 10, Right = 50 

但是代替印刷字符串值它打印在該串位於

Creating r1 
-> Assigning r2 to r1 
-> Changing values of r2 
String = Csharp_Projects.Program+ShapeInfo, Top = 10, Bottom = 50, Left = 10, Right = 50 
String = Csharp_Projects.Program+ShapeInfo, Top = 10, Bottom = 4444, Left = 10, Right = 50 

這是類代碼:

using System; 

namespace Csharp_Projects 
{ 
    static class Program 
{ 
    static void Main(string[] args) 
    { 
     ShapeInfo.Rectangle.ValueTypeContainingRefType(); 
    } 

    // this class which takes an string as parameter 

    public class ShapeInfo 
    { 
     public string infoString; 

     public ShapeInfo(string info) 
     { 
      infoString = info; 
     } 

     // this struct has two fields, one is int type and the other is ShapeInfo (above) and a constructor which set the values 

     public struct Rectangle 
     { 
      public ShapeInfo rectInfo; 

      public int recTop, rectleft, rectBottom, rectRight; 

      public Rectangle(string info, int top, int left, int Buttom, int Right) 
      { 
       rectInfo = new ShapeInfo(info); 
       recTop = top; 
       rectBottom = Buttom; 
       rectRight = Right; 
       rectleft = left; 
      } 
      // this method print the results 
      public void Display() 
      { 
       Console.WriteLine("string={0},top={1},Bottom={2},"+"left={3},Right={4}",rectInfo,recTop,rectBottom,rectRight,rectleft); 
      } 

      // this method make an object and assign it to second variable and then change the values of second variable . 

      public static void ValueTypeContainingRefType() 
      { 
       Console.WriteLine("Creating r1"); 
       Rectangle r1 = new Rectangle("First Rec", 10, 10, 50, 50); 
       Console.WriteLine("Assigning r2 to r1"); 
       Rectangle r2 = r1; 
       Console.WriteLine("Change Values of r2"); 
       r2.rectInfo.infoString = "This is new info!"; 
       r2.rectBottom = 4444; 
       r1.Display(); 
       r2.Display(); 
      } 


     } 
    } 
} 
    } 

我實在是搞不懂爲什麼會發生,也許我對C#SYST知識時間不夠,這是什麼原因?

回答

2

您不要在ShapeInfo類中覆蓋.ToString(),因此係統無法知道如何將該類的實例作爲字符串打印。默認情況下,所有對象都會打印它們的類名(因爲這幾乎是每個類都保證有的唯一東西)。

只是override the method

public override string ToString() 
{ 
    return infoString; 
} 
+0

哪種方法應採取超?因爲它給了我一個錯誤。順便說一句,我插入了非常有名的書的代碼,爲什麼它沒有在那裏提到它?我的意思是他沒有使用覆蓋而得到第一張照片 – Mohsen

+2

@Mohsen:我無法幫助您解決一個您沒有描述的錯誤。至於你的「非常有名的書」,我也不能評論我沒見過的代碼。 – David

+0

無論如何感謝您的時間。我似乎應該研究覆蓋概念。 – Mohsen

1

你需要重寫ShapeInfo ToString方法返回infoString。

public override string ToString() 
{ 
    return infoString; 
} 

或者改變您的DisplayMethod使用 rectInfo.infoString代替rectInfo

 public void Display() 
     { 
      Console.WriteLine("string={0},top={1},Bottom={2},"+"left={3},Right={4}",rectInfo.infoString,recTop,rectBottom,rectRight,rectleft); 
     } 
+0

謝謝,我現在注意到,在提到你的第二個解決方案的書中,我只是忘記了infoString。哦......那並不難 – Mohsen

相關問題