C#问题:如何使用构造函数:
AcctHolder ah1 = new AcctHolder("Dumitru", "St", "Bucharest");
并能够获得ah1.Fname
?(而不是null)
using System;
namespace ConsoleApplication1
{
class ATM
{
public static void Main(string[] args)
{
AcctHolder ah1 = new AcctHolder("Dumitru", "St", "Bucharest");
Console.WriteLine(ah1.FName); //returns null - why???
AcctHolder ah2 = new AcctHolder();
ah2.FName = "Dumi";
Console.WriteLine(ah2.FName); // returns "Dumi"
Console.ReadKey();
}
public class AcctHolder
{
private string fname, lname, city;
public string FName { get; set; }
public string LName { get; set; }
public string City {
get { return city; }
set { city = value; }
}
public AcctHolder(string a, string b, string c)
{
fname = a;
lname = b;
city = c;
}
public AcctHolder()
{
}
}
}
}
您fname
在构造函数中设置了错误的(私有)。使用公共的。
改变
public AcctHolder(string a, string b, string c)
{
fname = a;
lname = b;
city = c;
}
到
public AcctHolder(string a, string b, string c)
{
Fname = a;
LName = b;
City = c;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句