此方法应该打印以下:打印命名空间而不是字符串值
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