请原谅我对此没有超级准确,但我是一个c#noob而且只是学习编程。这是我现在无法真正测试的问题,但必须决定如何处理它。
我有一个SampleClass:
public class SampleClass {
string Name;
SecondClass SecondClass;
string Attribute;
public SampleClass (string Name, SecondClass SecondClass)
{
this.Name = Name;
this.SecondClass = SecondClass;
Attribute = SecondClass.HakunaMatata;
}
}
和SecondClass
public class SecondClass{
public string HakunaMatata;
public SecondClass (string HakunaMatata){
this.HakunaMatata = HakunaMatata;
}
到目前为止一切顺利。现在我需要知道是否可以向SampleClass添加一个函数来从Second类更改HakunaMatata?
像:
if (Name == "Simba"){
SecondClass.HakunaMatata = "Dont'w worry, be happy"}
通过将此代码插入SampleClass,我是否会更改用于构造SampleClass的SecondClasses对象的值,或者是为了构造而制作副本的程序?它会改变SampleClass上下一个实例的SampleClass属性吗?
答案 0 :(得分:1)
是的,因为string
是参考类型,而您的string Attribute
实际上是指SecondClass.HakunaMatata
。顺便说一句,您在SampleClass
Attribute = SampleClass.HakunaMatata;
应该是
Attribute = SecondClass.HakunaMatata;