小小新手,问题有点白痴,望解答
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication4
{
class fuzhi
{
private int a=0;
private int b=0;
public int A
{
get {
return a;
}
set {
a = value;
}
}
public int B
{
get {
return b;
}
set {
b = value;
}
}
}
class add {
fuzhi b=new fuzhi();
public int d (){
return b.A + b.B;
}
}
class Program
{
static void Main(string[] args)
{
//Program program = new Program();
fuzhi a = new fuzhi();
add eee = new add();
a.A = 1;
a.B = 2;
Console.WriteLine(eee.d());
Console.ReadLine();
}
}
}
这一段代码,不知道为啥执行出来的结果不是3,而是0.而且对应的在
class add {
fuzhi b=new fuzhi();
public int d (){
return b.A + b.B;
}
}中,为什么创建了类fuzhi的对象b之后,不能直接用b.A=1;这样的复制
[解决办法]
class add
{
fuzhi b = null;
public add(fuzhi bb)
{
this.b = bb;
}
public int d()
{
return b.A + b.B;
}
}
class Program
{
static void Main(string[] args)
{
fuzhi a = new fuzhi();
a.A = 1;
a.B = 2;
add eee = new add(a);
Console.WriteLine(eee.d());
Console.ReadLine();
}
}