我不明白爲什麼你會想在這種情況下使用嵌套類。你寫它的方式,子類是你所需要的。如果你想要多個方法(或者你稱之爲「函數」),只需添加你的方法。
有沒有一些隱藏的原因,你想在這裏使用嵌套類?作爲一般規則,很少需要嵌套類。
namespace SameAsPrincipal
{
public class Class1
{
private SQLiteConnection handle;
public Class1(string db_file)
{
handle = new SQLiteConnection(db_file);
}
public int AddRecord(Record record)
{
// use handle to add record and get record Id
return record.Id;
}
public void DeleteRecord(int id)
{
// Use handle to delete record
}
}
}
當你實例化對象時,你將傳入你的db_file並且連接對象將被創建。然後每個方法都可以在被調用時使用該連接對象。但是,在調用每個方法時創建連接並在操作完成後儘快處置連接通常是更好的主意。當然,這取決於您的業務以及它們是否跨國。大多數情況下,使用「using」塊來實例化連接是使用連接對象的好方法。越早釋放連接越快機器將重新使用該連接,您可以查找連接池以瞭解更多信息。
下面是使用「使用」使用存儲過程添加一個人的實例方法:
public int AddPerson(Person person)
{
using (var connection = new SQLiteConnection(dbFile))
{
connection.Open();
using (var command = new SQLiteCommand("spAddPerson",connection))
{
command.CommandType = CommandType.StoredProcedure;
var idParameter = new SQLiteParameter("@Id", DbType.Int32);
idParameter.Direction = ParameterDirection.Output;
command.Parameters.Add(idParameter);
command.Parameters.AddWithValue("@FirstName", person.FirstName);
command.Parameters.AddWithValue("@LirstName", person.LastName);
command.ExecuteNonQuery();
}
}
return person.Id;
}
編輯:至於下面
有幾件事情您的評論:
- 使用名稱空間而不是父類來對類進行分組。
- 您應該將所有數據庫方法添加到數據庫類並創建用於建模對象的類,而不是子類。
- 每個類應該在它自己的文件中
- 命名空間部分是.. [] * I.E. Music類具有名稱空間YourApplication.YourProject.Models - 在YourProject項目中,在名爲Music的第一級文件夾中,您將找到一個名爲Music.cs的文件,並在該文件中找到您的音樂課程。這不是要求,編譯器不關心這樣的結構。當你開始獲得更多的代碼時,它只會讓你的生活更輕鬆。
這裏是我講的代碼結構的一個例子(記住每一部分是它自己的文件)
在您的項目稱爲模型的根目錄下創建一個文件夾。在這個模型文件夾中創建一個名爲Music.cs
namespace YourApplication.YourProject.Models
{
public class Music
{
public int Id { get; set; }
public string Title { get; set; }
public double Length { get; set; }
public string Artist { get; set; }
public string Album { get; set; }
}
}
在這同一(模型)文件夾中創建一個名爲Film.cs
namespace YourApplication.YourProject.Models
{
public class Film
{
public int Id { get; set; }
public string Title { get; set; }
public double Length { get; set; }
public string Director { get; set; }
public string[] Actors { get; set; }
}
}
現在回到項目根(在模型不再文件文件夾)創建一個名爲Persistence的新文件夾。
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using YourApplication.YourProject.Models;
namespace YourApplication.YourProject.Persistence
{
public static class DatabaseActions
{
public static string dbFile;
public static Music[] ListMusic()
{
var musicList = new List<Music>();
// database call to get all music
using (var connection = new SQLiteConnection(dbFile))
{
connection.Open();
using (var command = new SQLiteCommand("spGetMusic", connection))
{
var reader = command.ExecuteReader();
// The try finally blocks are not strictly needed as these will are suppose to be called upon disposal
try
{
// loop through records creating music objects
while (reader.Read())
{
var music = new Music();
music.Id = reader.GetInt32(0);
music.Title = reader.GetString(1);
musicList.Add(music);
}
}
finally
{
reader.Close();
connection.Close();
}
}
}
return musicList.ToArray();
}
public static int SaveMusic(Music music)
{
if (music.Id == 0)
{
// database stuff - getting the newly created database id
}
else
{
// database calls to update record
}
return music.Id;
}
public static int SaveFilm(Film film)
{
if (film.Id == 0)
{
// database stuff - getting the newly created database id
}
else
{
// database calls to update record
}
return film.Id;
}
public static Music GetMusic(int id)
{
var music = new Music();
// database call and setting of values on music
return music;
}
public static Film GetFilm(int id)
{
var film = new Film();
// database call and setting of values on music
return film;
}
}
}
現在終於創建的根文件名爲WorkHarness.cs
using System;
using YourApplication.YourProject.Persistence;
namespace YourApplication.YourProject
{
public class WorkHarness
{
public void Initialize()
{
DatabaseActions.dbFile = "your db file";
}
public void ShowMusicList()
{
// list the id and title so user can select by Id
foreach (var music in DatabaseActions.ListMusic())
{
Console.WriteLine("{0,-10}{1}",music.Id,music.Title);
}
}
public void DisplayMusicItem(int id)
{
var music = DatabaseActions.GetMusic(id);
Console.WriteLine("Title: " + music.Title);
Console.WriteLine("Length: " + music.Length);
Console.WriteLine("Artist: " + music.Artist);
Console.WriteLine("Album: " + music.Album);
}
}
}
爲了得到你想要的語法,subclass1其方法需要是靜態的,但是,我不確定,如果你能夠訪問class1的屬性,方法等,而無需將它們作爲參數傳遞給subclass1的方法。 – AntiTcb
感謝所有幫助和建議。最後,我創建了一個名爲db_manage的新命名空間,並且在該命名空間中我使用她的方法創建了類。現在就像我想要的:db_manage.class.method(); –