我发现我的编码习惯最近会滑落 - 并注意到自己陷入了一些坏习惯 - 通常是由于我缺乏动力(可能是由于我为我设定的任务的无聊性) - 所以给自己一个启动我决定写自己应该是一个非常简单的基础课。这是:
public class Customer
{
public string CUSTOMERNAME;
public List<Site> sites = new List<Site>();
public Customer()
{
}
public void AddSite(Site location)
{
sites.Add(location);
}
}
public class Site
{
public string SITENAME;
public Address SITEADDRESSDETAILS;
public string SITEPHONENUMBER;
public Site(string sitename, Address siteaddress, string tel)
{
SITENAME = sitename;
SITEADDRESSDETAILS = siteaddress;
SITEPHONENUMBER = tel;
}
}
public class Address
{
public List<string> address = new List<string>();
public Address() {
}
public void AddAddressDetail(string line)
{
address.Add(line);
}
}
现在一切似乎都有效,但我不禁感到事情可以做得更好。我用以下代码测试了它:
static void Main(string[] args)
{
Customer customer = new Customer();
customer.CUSTOMERNAME = "Max Hammer Ltd";
Address addy = new Address();
addy.AddAddressDetail("1 Edgerail Close");
addy.AddAddressDetail("Greenbushes");
addy.AddAddressDetail("Bluehill");
addy.AddAddressDetail("Surrey");
addy.AddAddressDetail("RH0 6LD");
Site surreyOffice = new Site("Surrey Office", addy, "01737 000000");
addy = new Address();
addy.AddAddressDetail("6 Electric Avenue");
addy.AddAddressDetail("Brixton");
addy.AddAddressDetail("London");
addy.AddAddressDetail("SW4 1BX");
Site brixtonOffice = new Site("Brixton Office", addy, "020 7101 3333");
customer.AddSite(surreyOffice);
customer.AddSite(brixtonOffice);
Console.WriteLine(customer.CUSTOMERNAME);
int numberOfSutes = customer.sites.Count;
for (int i = 0; i < numberOfSutes; i++)
{
Console.WriteLine(customer.sites[i].SITENAME);
foreach (string line in customer.sites[i].SITEADDRESSDETAILS.address)
{
Console.WriteLine(line);
}
Console.WriteLine(customer.sites[i].SITEPHONENUMBER);
}
Console.ReadKey();
}
我对我的Main
课程感到不满意,但我不确定为什么 - 即使它符合我的要求。任何提示,指针?
答案 0 :(得分:1)
以下是我的看法:
public class Customer
{
public string Name {get; set;}
public List<Site> Sites { get; set; }
public Customer()
{
Sites = new List<Site>();
}
}
public class Site
{
public string Name { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public Site(string sitename, string siteaddress, string tel)
{
Name = sitename;
Adress = siteaddress;
PhoneNumber = tel;
}
}
public class Program
{
static void Main(string[] args)
{
Customer customer = new Customer();
customer.Name = "Max Hammer Ltd";
string address = string.Join(Environment.NewLine, new []{"1 Edgerail Close", "Greenbushes", "Bluehill"
, "Surrey", "RH0 6LD"});
Site surreyOffice = new Site("Surrey Office", address, "01737 000000");
address = string.Join(Environment.NewLine, new[]{"1 Edgerail Close", "Greenbushes", "Bluehill"
, "Surrey", "RH0 6LD"});
Site brixtonOffice = new Site("Brixton Office", address, "020 7101 3333");
customer.Sites.Add(surreyOffice);
customer.Sites.Add(brixtonOffice);
Console.WriteLine(customer.Name);
foreach (Site site in customer.Sites)
{
Console.WriteLine(site.Name);
Console.WriteLine(site.Adress);
Console.WriteLine(site.PhoneNumber);
Console.WriteLine();
}
Console.ReadKey();
}
}