當我在研究嵌套類並遇到問題時。那時有人告訴我我需要使用界面。在實現接口的過程中,我瞭解到接口不允許構造函數或傳遞參數參數。所以,我在想抽象,但我不確定如何實現它們並保持簡單。使用抽象類的嵌套類? (以及保護/隱藏課程)?
在下面的源代碼。環境設置在Console項目中填充並傳遞給DLL項目中的Database類。
問題1 - 是否可以抽象簡化腳本的環境設置?
問題#2 - 使用嵌套類或沒有,我如何保護DealerRepository類(使其不可見到控制檯項目(我猜抽象)
問題#3 - ?是否DealerRepository類需要被抽象還是什麼?
我想這裏是保護/隱藏控制檯項目DealerRepository類,並沒有對EnvironmentSetting屢通過巢班班通/模型。
謝謝...
//EnvironmentSetting.cs
namespace zTestcase1.Model {
public class EnvironmentSetting : IDisposable
{
public EnvironmentSetting() { } //Constructor...
public void Dispose() { } /Dispose...
//Member variables...
private string _emulation = "";
private string _application = "";
private string _database = "";
//Get/Set properties...
public string Emulation { get {return _emulation;} set {_emulation = value;} }
public string Application { get {return _application;} set {_application = value;} }
public string Database { get {return _database;} set {_database = value;} }
}
}
//DealerRepository.cs
namespace zTestcase1.Data.Dealer
{
public class DealerRepository : IDisposable
{
public DealerRepository(EnvironmentSetting parmEnvironmentSetting) {
_environmentSettingA = parmEnvironmentSetting;
} //Constructor...
public void Dispose() { } //Dispose...
//Member variables...
private EnvironmentSetting _environmentSettingA = "";
//Get/Set properties...
//N/A...
//Functions...
public string EnvironmentResponse()
{
return "Emulation - " + _environmentSettingA.Emulation + ", Application - " + _environmentSettingA.Application + ", Database - " + _environmentSettingA.Database + "";
}
public string DealerProifle_Lookup(string parmName)
{
return "(DealerProfile-Lookup) - " + parmName;
}
public string DealerProfile_Save(string parmName)
{
return "(DealerProfile-Save) - " + parmName;
}
}
}
namespace zTestcase1.Library
{
/*public class Database : IDisposable
{
//Class...
public class DataDealer : IDisposable
{
//Constructor...
public DataDealer(EnvironmentSetting parmEnvironmentSetting)
{
_environmentSettingA = parmEnvironmentSetting;
}
//Dispose...
public void Dispose() { }
//Member variables...
private EnvironmentSetting _environmentSettingA = null;
private DealerRepository _dataDealer = null;
//Get/Set properties...
public DealerRepository Dealer { get { if (_dataDealer == null) { _dataDealer = new DealerRepository(_environmentSettingA); } return _dataDealer; } }
//Functions...
//N/A...
}*/
//Constructor...
public Database(EnvironmentSetting parmEnvironmentSetting)
{
_environmentSettingB = parmEnvironmentSetting;
}
public void Dispose() { } //Dispose...
//Member variables...
private EnvironmentSetting _environmentSettingB = null;
//private DataDealer _dataDealerB = null;
private DealerRepository _dataDealerC = null;
//Get/Set properties...
//public DataDealer Dealer { get { if (_dataDealerB == null) { _dataDealerB = new DataDealer(_environmentSettingB); } return _dataDealerB; } }
public DealerRepository Dealer { get { if (_dataDealerC == null) { _dataDealerC = new DealerRepository(_environmentSettingB); } return _dataDealerC; } }
//Functions...
//N/A...
}
}
namespace zTestcase1.Sample1.Console
{
class Program
{
static void Main(string[] args)
{
EnvironmentSetting environmentSettting = new EnvironmentSetting();
environmentSettting.Application = "DOS Console";
environmentSettting.Database = "Not yet hooked up";
environmentSettting.Emulation = "Development";
Database _libraryDatabase = new Database(environmentSettting);
//var test1 = _libraryDatabase.Dealer.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy");
var test2 = _libraryDatabase.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy");
System.Console.WriteLine(_libraryDatabase.Dealer.EnvironmentResponse() + " [=] " + test2);
System.Console.ReadLine(); //To pause the console...
}
}
}
感謝您的迴應。感謝您對抽象和內部類的澄清。是的,我試圖使用_libraryDatabase.Dealer來顯示DealerRepository類中的所有函數。讓我看看它在內部使用時是否有效。 – fletchsod
#1 - 當我試圖把這個內部的「公共類DealerRepository」改爲「內部類DealerRepository」時,它並沒有在控制檯項目的intellisense中顯示,這是我想要的,但是我得到編譯器或紅色下劃線語法錯誤,無法訪問。 #2 - 取而代之的是,如果我將構造函數內部化,則可以在控制檯項目的intellisense中看到DealerRepository類。有沒有讓DealerRepository類在控制檯項目中對intellisen不可見的方法?謝謝... – fletchsod
@fletchsod:我還不清楚你期望的最終目標是什麼。您希望:(1)DealerRepository類不會顯示在intellisense中,但(2)該類繼續使用_libraryDatabase.Dealer在相同範圍內工作。你不能同時看到和看不到同一個班級! – CSJ