我试图通过在数据库中查阅记录(意图将查询数据库,并将标本的回归值定在数据库中),或者通过指明“手头”的数值,来建立能够自信的等级,而不需要数据库电话。
我先看一对夫妇的教科书,以发现这一功能的“最佳实践”,令人痛心的是,我早上来。
我撰写了两部 sample样本,反映了我认为是两种最可能的解决办法,但我并没有提出最妥善执行的想法?
样本1使用大多数书本告诉我的是“首选”方式,但与这些要求一并介绍的大多数例子实际上不符合我的情况。 我在此担心,流动不能像第2号申请那样读。
using System;
namespace TestApp
{
public class Program
{
public static void Main(string[] args)
{
var one = new OverloadedClassTester();
var two = new OverloadedClassTester(42);
var three = new OverloadedClassTester(69, "Mike", 24);
Console.WriteLine("{0}{1}{2}", one, two, three);
Console.ReadKey();
}
}
public class OverloadedClassTester
{
public int ID { get; set; }
public string Name { get; set; }
public int age { get; set; }
public OverloadedClassTester() : this(0) { }
public OverloadedClassTester (int _ID) : this (_ID, "", 0)
{
this.age = 14; // Pretend that this was sourced from a database
this.Name = "Steve"; // Pretend that this was sourced from a database
}
public OverloadedClassTester(int _ID, string _Name, int _age)
{
this.ID = _ID;
this.Name = _Name;
this.age = _age;
}
public override string ToString()
{
return String.Format("ID: {0}
Name: {1}
Age: {2}
", this.ID, this.Name, this.age);
}
}
}
这份Sample(App #2)“耳朵”更容易读懂,因为我认为更容易看到行动的流动。 然而,从所节省的特性来看,这似乎有效率。 而且,在完全初步确定之前,使用某种物体的方法,也无危险吗?
using System;
namespace TestApp
{
public class Program
{
public static void Main(string[] args)
{
var one = new OverloadedClassTester();
var two = new OverloadedClassTester(42);
var three = new OverloadedClassTester(69, "Mike", 24);
Console.WriteLine("{0}{1}{2}", one, two, three);
Console.ReadKey();
}
}
public class OverloadedClassTester
{
public int ID { get; set; }
public string Name { get; set; }
public int age { get; set; }
public OverloadedClassTester()
{
initialise(0, "", 21); // use defaults.
}
public OverloadedClassTester (int _ID)
{
var age = 14; // Pretend that this was sourced from a database (from _ID)
var Name = "Steve"; // Pretend that this was sourced from a database (from _ID)
initialise(_ID, Name, age);
}
public OverloadedClassTester(int _ID, string _Name, int _age)
{
initialise(_ID, _Name, _age);
}
public void initialise(int _ID, string _Name, int _age)
{
this.ID = _ID;
this.Name = _Name;
this.age = _age;
}
public override string ToString()
{
return String.Format("ID: {0}
Name: {1}
Age: {2}
", this.ID, this.Name, this.age);
}
}
}
解决这一问题的“正确”方式是什么?
感谢