參數設計
更新:2007 年 11 月
這個主題中的方針可協助您針對成員參數選取正確的型別和名稱。下列主題也會呈現參數適用的設計方針。
一定要使用可提供成員所需功能的最低衍生性之參數型別。
下列程式碼範例將說明這個方針。BookInfo 類別繼承自 Publication 類別。Manager 類別會實作兩個方法:BadGetAuthorBiography 和 GoodGetAuthorBiography. ;BadGetAuthorBiography 會使用 BookInfo 物件的參考,雖然它只會使用於 Publication 中宣告的成員。GoodGetAuthorBiography 方法會示範正確的設計。
' A Class with some basic information.
Public Class Publication
Dim Protected authorValue as String
Dim Protected publicationDateValue as DateTime
Public Sub new(author as String, publishDate as DateTime)
Me.authorValue = author
Me.PublicationDateValue = publishDate
End Sub
Public Readonly Property PublicationDate as DateTime
Get
Return publicationDateValue
End Get
End Property
Public Readonly Property Author as String
Get
Return authorValue
End Get
End Property
End Class
' A Class that derives from Publication
Public Class BookInfo
Inherits Publication
Dim isbnValue as String
Public Sub new(author as string, _
publishDate as DateTime, _
isbn as String)
MyBase.New(author, publishDate)
Me.isbnValue = isbn
End Sub
Public Readonly Property Isbn as String
Get
Return isbnValue
End Get
End Property
End Class
Public Class Manager
' This method does not use the Isbn member
' so it doesn't need a specialized reference to Books
Shared Function BadGetAuthorBiography(book as BookInfo) as String
Dim biography as String = ""
Dim author as String = book.Author
' Do work here.
Return biography
End Function
' This method shows the correct design.
Shared Function GoodGetAuthorBiography(item as Publication) as String
Dim biography as String = ""
Dim author as String = item.Author
' Do work here.
Return biography
End Function
// A class with some basic information.
public class Publication
{
string author;
DateTime publicationDate;
public Publication(string author, DateTime publishDate)
{
this.author = author;
this.publicationDate = publishDate;
}
public DateTime PublicationDate
{
get {return publicationDate;}
}
public string Author
{
get {return author;}
}
}
// A class that derives from Publication
public class BookInfo :Publication
{
string isbn;
public BookInfo(string author, DateTime publishDate, string isbn) :
base(author, publishDate)
{
this.isbn = isbn;
}
public string Isbn
{
get {return isbn;}
}
}
public class Manager
{
// This method does not use the Isbn member
// so it doesn't need a specialized reference to Books
static string BadGetAuthorBiography(BookInfo book)
{
string biography = "";
string author = book.Author;
// Do work here.
return biography;
}
// This method shows the correct design.
static string GoodGetAuthorBiography(Publication item)
{
string biography = "";
string author = item.Author;
// Do work here.
return biography;
}
不要使用保留的參數。
程式庫的將來版本可以加入接受其他參數的新多載。
下列程式碼範例會先示範違反此方針的錯誤方法,然後再示範具有正確設計的方法。
Public Sub BadStoreTimeDifference (localDate as DateTime, _
toWhere as TimeZone, _
reserved as Object)
' Do work here.
End Sub
Public Sub GoodCStoreTimeDifference (localDate as DateTime, _
toWhere as TimeZone)
' Do work here.
End Sub
Public Sub GoodCStoreTimeDifference (localDate as DateTime, _
toWhere as TimeZone, _
useDayLightSavingsTime as Boolean)
' Do work here.
End Sub
public void BadStoreTimeDifference (DateTime localDate,
TimeZone toWhere,
Object reserved)
{
// Do work here.
}
public void GoodCStoreTimeDifference (DateTime localDate,
TimeZone toWhere)
{
// Do work here.
}
public void GoodCStoreTimeDifference (DateTime localDate,
TimeZone toWhere,
bool useDayLightSavingsTime)
{
// Do work here.
}
請勿使用接受指標、指標陣列或多維陣列做為參數的公開方法。
使用大多數的程式庫時,應該不需要這些進階功能的知識。
一定要將所有 out 參數放在所有 pass-by-value 和 ref 參數之後 (不包括參數陣列),即使這樣會在多載之間產生不一致的參數排序時亦然。
這個慣例會讓方法簽章更容易了解。
當覆寫成員或實作介面成員時,一定要在命名參數上維持一致性。
覆寫應該使用相同的參數名稱;多載應該使用與宣告成員相同的參數名稱;介面實作應該使用定義於介面成員簽章中的相同名稱。
Portions Copyright 2005 Microsoft Corporation.All rights reserved.
Portions Copyright Addison-Wesley Corporation.All rights reserved.
如需設計方針的詳細資訊,請參閱由 Krzysztof Cwalina 和 Brad Abrams 所著,並由 Addison-Wesley 於 2005 年發行的「Framework 設計方針:可重複使用之 .NET 程式庫的慣例、慣用語法和模式」一書。