2012-12-05 71 views
-2

所以我的问题是基本封装。我知道我正在设置我的getter和setters(我后来对此有一个问题),但我有多个类。我有另一堂课,我知道我正在制作我的代码片段 - 通过将某些事情公之于众,可以让我的课堂上的学生了解。所以我认为我建立了我的第一个代码文件。 (一些背景知识,我有一个类连接到数据库,然后是另一个封装所有数据的类。发布的第一个代码段是封装部分,然后我发布了我正在搞的三个方法。)封装与多个类C#

我对获得和设置感觉很好,我对我的构造函数有点不确定。我觉得我把我的变量放在参数列表中,这样我就可以从外部课程中为它们添加值了。对?或者我应该把我的私有变量的公共形式放在我的其他代码文件中,然后将这些变量传入我的构造函数中?

/这是我的第一个代码文件

using System; 

public class clsEncapsulate 
    { 
    private int mID; 
    private string mName; 
    private string mClassification; 
    private DateTime mConvocationDate; 
    private string mLocation; 

public int ID 
{ 
    get 
    { 
     return mID; 
    } 
    set 
    { 
     mID = value; 
    } 
} 

public string Name 
{ 
    get 
    { 
     return mName; 
    } 
    set 
    { 
     mName = value; 
    } 
} 

public string Classification 
{ 
    get 
    { 
     return mName; 
    } 
    set 
    { 
     mName = value;   
    } 
} 

private DateTime ConvocationDate 
{ 
    get 
    { 
     return mConvocationDate; 
    } 
    set 
    { 
     mConvocationDate = value; 
    } 
} 

private string Location 
{ 
    get 
    { 
     return mLocation; 
    } 
    set 
    { 
     mLocation = value; 
    } 
} 

public clsEncapsulate(int id, string name, string classification, DateTime convocationDate, string location) 
{ 
    bool running = false; 

    while(running == false) 
    { 

    ID = mID; 
    Name = mName; 
    Classification = mClassification; 
    ConvocationDate = mConvocationDate; 
    Location = mLocation; 

    running = true; 

    } 
} 

}

在我的第二个代码文件,我正要把那我有麻烦的方法。

private void refreshbox() 
{ 
    string formattedConvocation; 
    string formattedDateTime; 
    string formattedConvocationName; 

    lstConvocations.Items.Clear(); 
    foreach (clsEncapsulate currentConvocation in mConvocationAL) 
    { 
     formattedConvocationName = currentConvocation.Name; 
     formattedConvocationName = truncateString(formattedConvocationName, 30); 

     formattedConvocation = formattedConvocationName.PadRight(33); 
     formattedConvocation += currentConvocation.Classification.PadRight(17); 

     formattedDateTime = currentConvocation.ConvocationDate.ToShortDateString().PadRight(10) 
      + currentConvocation.ConvocationDate.ToShortTimeString().PadLeft(8); 
     formattedConvocation += formattedDateTime; 

     lstConvocations.Items.Add(formattedConvocation); 
    } 
} 

好了,为了我的第二个代码文件来操作的第一个代码文件中的变量,我需要将它们暴露于这种方法。我不知道是否应该将我的公共变量放在构造函数中,或者是否应该在我的第一个代码文件中将它们声明为某处。我很不确定如何将这些变量暴露给这个方法。我已经摆弄它,但是我的书没有完全解决这种情况,而且我很难解决这个问题。

如果有人回答这个问题,请打破为什么你要把你要放的东西!我想明白为什么,比如说,我把我的公共变量放在一个地方,而不是另一个地方。或者为什么我在一个地方而不是另一个地方声明了我的封装类的一个对象。我试图在我的方法中声明一个封装对象,所以它会给这个方法访问变量,但它不工作!请告诉我我做错了什么,或者如果你想让我发布更多的代码。

下面是我搞乱的另外两种方法。

private void displayProperties(int index) 
{ 
    if (index == -1) 
    { 
     return; 
    } 

    clsEncapsulate selectedValue = (clsEncapsulate)mConvocationAL[index]; 


    txtConvocationName.Text = selectedValue.Name; 
    txtConvocationClassification.Text = selectedValue.Classification; 
    txtConvocationDate.Text = selectedValue.ConvocationDate.ToShortDateString(); 
    txtConvocationTime.Text = selectedValue.ConvocationDate.ToShortTimeString(); 
    txtConvocationLocation.Text = selectedValue.Location; 
    txtID.Text = selectedValue.ID.ToString(); 
} 

/最后的方法,我就搞乱了:

从我的第二个代码文件/第二种方法我是在搞乱

private void readConvocations(string filterConstraint, string sortField, string  sortOrder) 
{ 
     OleDbConnection connection = null; 
    OleDbDataReader reader = null; 

    try 
    { 
     connection = new OleDbConnection(); 
     connection.ConnectionString = mConnectionString; 

     connection.Open(); 

     string statement = "SELECT ID, Name, Classification, Location, Date FROM Convocations "; 
     if(filterConstraint != "") 
      { 
       statement += "WHERE Name LIKE " + toSQL(filterConstraint, true) + " "; 
      } 
      string statement2 = statement; 
     statement = string.Concat(new string[] 
     { 
      statement2, "ORDER BY ", sortField, " ", sortOrder 

     }); 

     OleDbCommand oleDbCommand = new OleDbCommand(statement, connection); 
     reader = oleDbCommand.ExecuteReader(); 
     mConvocationAL.Clear(); 

     while(reader.Read()) 
     { 

      clsEncapsulteconvocation = new clsEncapsulate(); 
      convocation.ID = (int)reader["ID"]; 
      convocation.Name = (string)reader["Name"]; 
      convocation.Classification = (string)reader["Classification"]; 
      convocation.Location = (string)reader["Location"]; 
      convocation.ConvocationDate = (DateTime)reader["Date"]; 

      mConvocationAL.Add(convocation); 

     } 
    } 
    finally 
    { 
     if (reader != null) 
     { 
      reader.Close(); 
     } 

     if (connection != null) 
     { 
      connection.Close(); 
     } 
    } 
} 

告诉我,如果你需要我更详细地说明帮助你了解我的情况。我是学习词汇的新手,想要了解这一点!感谢您的帮助。 :)

回答

0

您提供的代码是一个公共对象和一堆私有方法,因此很难获得关于代码如何协同工作的整体画面,但有几条原则可以使您的代码更好地结构化,现在和未来。

阅读关于SOLID(http://en.wikipedia.org/wiki/SOLID_(object-oriented_design))。 S和D将适用于您的示例。

你也提到了建设者和私人财产。尝试查看Imutable类型。这意味着一旦对象被创建,你就不能改变它。对于你的clsEncapsulate类来说,这意味着你的字段只能读取并删除公共设置者。

祝你好运。

+0

我想我越来越好了,谢谢!如果我有其他问题,我会在下面发帖。 – user1877863