此方法應該打印以下:打印命名空間而不是字符串值
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知識時間不夠,這是什麼原因?
哪種方法應採取超?因爲它給了我一個錯誤。順便說一句,我插入了非常有名的書的代碼,爲什麼它沒有在那裏提到它?我的意思是他沒有使用覆蓋而得到第一張照片 – Mohsen
@Mohsen:我無法幫助您解決一個您沒有描述的錯誤。至於你的「非常有名的書」,我也不能評論我沒見過的代碼。 – David
無論如何感謝您的時間。我似乎應該研究覆蓋概念。 – Mohsen