C# 用多个类C进行封装#
所以我的问题是关于基本的封装。我知道我正在正确设置我的getter和setter(稍后我实际上有一个问题),但我有多个类。我有另一个类,我知道通过公开某些内容,我正在使我的代码视图片段能够在我的外部类中使用。所以我想我的第一个代码文件设置正确了。(在某些背景下,我有一个连接到数据库的类,然后是另一个封装所有数据的类。发布的第一个代码部分是封装部分,然后我发布了三个我搞砸了的方法。) 我对获取和设置感觉很好,我对我的构造函数有点不确定。我觉得我把变量放在参数列表中,这样我就可以从我的外部类中把值放进去?正当或者我应该将私有变量的公共形式放在另一个代码文件中,然后将它们传递到同一个文件中的构造函数中 /这是我的第一个代码文件C# 用多个类C进行封装#,c#,encapsulation,C#,Encapsulation,所以我的问题是关于基本的封装。我知道我正在正确设置我的getter和setter(稍后我实际上有一个问题),但我有多个类。我有另一个类,我知道通过公开某些内容,我正在使我的代码视图片段能够在我的外部类中使用。所以我想我的第一个代码文件设置正确了。(在某些背景下,我有一个连接到数据库的类,然后是另一个封装所有数据的类。发布的第一个代码部分是封装部分,然后我发布了三个我搞砸了的方法。) 我对获取和设置感觉很好,我对我的构造函数有点不确定。我觉得我把变量放在参数列表中,这样我就可以从我的外部类中把值放
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();
}
}
}
/我搞砸的最后一个方法是:
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();
}
}
}
如果您需要我详细说明以帮助您了解我的情况,请告诉我。我是一个新的词汇学习者,我想了解这一点!谢谢你的帮助。:) 您提供的代码是一个公共对象和一堆私有方法,因此很难全面了解您的代码是如何协同工作的,但现在和将来,您可以应用一些原则来改进代码的结构 读一读关于固体的书(http://en.wikipedia.org/wiki/SOLID_(面向对象的设计)。S和D很适合你的例子 你还提到了承包商和私有财产。尝试寻找可替换的类型。这意味着一旦创建了对象,就不能对其进行更改。对于clsEncapsulate类,这意味着将字段设置为只读,并删除公共setter
祝你好运。我想我会做得更好,谢谢!如果我有其他问题,我会在下面发布。