类细分+构造函数

时间:2014-08-11 09:31:00

标签: c#

您好我正在寻找一些关于如何打破我的课程的专家建议。 请耐心等待,这是c#im第五周仍然新鲜。 我得到的任务是为一家虚构公司创建一个基本的数据库和界面。 界面应支持公司成员的基本管理。

唯一的规则是至少应该分为两个类。

现在我的问题:   - 我在构造函数头中使用了8个成员?有没有更好的方法来做到这一点?   - 瑞典的社交nb是我应该创建日期类的日期吗?   - 如果我为每个成员创建一个get和set方法,这将是一个很长的文档是否有最佳实践?。

using System;

class Person
    {
    private string firstname, lastname, socialnb;
    private string email, phone;
    private string streetaddress, postcode, city;

    public Person(string firstname, string lastname, string socialnb, string, email,      string phone, string streetaddress, string postcode, string city)
    {
    this.firstname = firstname;
    this.lastname = lastname;
    this.socialnb = socialnb;
    this.email = email;
    this.phone = phone;
    this.streetaddress = streetaddress;
    this.postcode = postcode;
    this.city = city;
    }

    public String ToString() // Incomplete
    {
    return "\n\t"; // Will include all fields in the class
    }

}

4 个答案:

答案 0 :(得分:2)

您不需要创建日期类,在C#中,DateTime类存在:

DateTime Socialnb;

您可以使用以下方式将日期设置为当前日期:

Socialnb = DateTime.Now;

声明属性时,您可以使用:

public string firstname {get; set;} //it's like writing getter and setter in C#

用于覆盖ToString()方法:

public String ToString()
{
return firstname + " " + lastname; //...
}

答案 1 :(得分:0)

您应该指定是否为UI使用Windows窗体或WPF。

您应该使用#region ... #endregion来拆分代码并使其更清晰(一个用于属性,一个用于构造函数,一个用于事件......您的选择)

dotnet中的日期类是DateTime

关于成员,如果你需要在类之外(这似乎是这种情况,因为你的类似乎是一个Model类),你应该创建(自动)属性。示例:public type name {get; set;}(查看更多here

答案 2 :(得分:0)

拆分类

您可以通过以下方式划分课程:

using System;

class Person
    {

    public String FirstName {get; set;}
    public String LastName {get; set;}
    public String SocialNb {get; set;}

    public ContactInfo Contact {get; set;}
    public AddressInfo Address {get; set;}

    public Person(string firstname, string lastname, string socialnb, string, email, string phone, string streetaddress, string postcode, string city)
    {
        Contact = new ContactInfo(email,phone);
        Address = new AddressInfo(streetaddress,postcode,city);
        FirstName = firstname;
        LastName = lastname;
        SocialNb = socialnb;
    }

    public String ToString() // Incomplete
    {
    return "\n\t"; // Will include all fields in the class
    }

class ContactInfo
{
    public String Email {get; set;}
    public String Phone {get; set;}

    public ContactInfo(String email = "", String phone = "") 
    { 
        Email = email;
        Phone = phone;
    }
}

class AddressInfo
{
    public String Street {get; set;}
    public String Postcode {get; set;}
    public String City {get; set;}

    public AddressInfo(String street = "", String postcode = "", String city = "") 
    { 
        Street = street;
        Postcode = postcode;
        City = city;
    }
}

我在构造函数头中使用了8个成员 - 是否有更好的方法来执行此操作?

有几种方法可以推动班级成员。您可以使用:

-Default参数:

public Person(string firstname="", string lastname="", string socialnb="", string, mail="", string phone="", string streetaddress="", string postcode="", string city=""){}

-Object Initializer:

Person p = new Person 
{
   FirstName = "John",
   LastName = "Doe",
   Contact = new Contact { Email = "johndoe@dot.com", Phone="123432123"}
   // ... Initialize remaining field...
}

我应该创建日期类吗?

正如您所说,socialnb是瑞典的约会对象。如果您希望将解决方案保持国际化,String将是最佳选择(因为,基本上,socialnb始终是数字和/或符号链)

如果我为每个成员创建一个get和set方法,它将是一个长文档

您不必为每个成员实施getter和setter。只需使用autoproperties:

public String Street {get; set;}

答案 3 :(得分:0)

你可以用两种方式更好地做到这一点 - 1.创建两个单独的类(Person和Address)并保留Address in Person的引用 要么 2.使用嵌套类(了解更多:http://msdn.microsoft.com/en-us/library/ms173120.aspx

Ex 1:

  1. 地址
  2. 一个。名字 湾姓 C。 socialnb d。电子邮件 即电话

    public class Person 
    { 
    
        // constructor takes only person specific data
        public Person(string firstname, string lastname, string socialnb, 
                      string, email, string phone) 
        {
    
        }
    
        // Address Property
        Public Address MyAddress {get; set;}
    
    }
    

    地址

    一个。街道地址 湾邮编 C。城市

    public class Address
    {
        public Address(string streetaddress, string postcode, string city)
        {
    
        } 
    }
    

    前2:

    public class Person
    {
    
       //keep a reference of Address class
       public Address MyAddress {get; set;}
    
        public class Address
        {
    
        }
    }