2017-01-24 21 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

相关问题