参数设计

本主题中的准则帮助您为成员参数选择正确的类型和名称。 此外,下列主题还提供了参数设计准则。

使用派生程度最小的参数类型提供成员所需的功能。

下面的代码示例阐释了这一准则。 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;
    }
// A class with some basic information.
public ref class Publication
{
private:
    String^ author;
    DateTime publicationDate;

public:
    Publication(String^ author, DateTime publishDate)
    {
        this->author = author;
        this->publicationDate = publishDate;
    }

    property DateTime PublicationDate
    {
        DateTime get() {return publicationDate;}
    }

    property String^ Author
    {
        String^ get() {return author;}
    }
};

// A class that derives from Publication
public ref class BookInfo : public Publication
{
private:
    String^ isbn;

public:
    BookInfo(String^ author, DateTime publishDate, String^ isbn) :
            Publication(author, publishDate)
    {
        this->isbn = isbn;
    }

    property String^ Isbn
    {
        String^ get() {return isbn;}
    }
};

private enum class ErrorOptions {ThrowOnError};

private enum class CasingOptions {CaseInsensitive};

private ref class BetterType
{
internal:
    static void GetType(String^ name,
    ErrorOptions throwOption,
    CasingOptions caseOption)
    {}
};

public ref class Manager
{
public:
    // 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.
}
void BadStoreTimeDifference(DateTime localDate,
    TimeZone^ toWhere,
    Object^ reserved)
{
    // Do work here.
}

void GoodCStoreTimeDifference(DateTime localDate,
    TimeZone^ toWhere)
{
    // Do work here.
}

void GoodCStoreTimeDifference(DateTime localDate,
    TimeZone^ toWhere,
    bool useDayLightSavingsTime)
{
    // Do work here.
}

不要使用公开显露的采用指针、指针数组或多维数组作为参数的方法。

使用大多数库时都无需了解这些高级功能。

将所有输出参数放在所有按值传递参数和引用传递参数(不包括参数数组)之后,即使这会导致参数在重载间排序不一致也要如此。

这种约定使得方法签名更易于理解。

在重写成员或实现接口成员时,要保持参数命名的一致性。

重写应使用相同的参数名。 重载应使用与声明成员相同的参数名。 接口实现应使用接口成员签名中定义的相同名称。

部分版权所有 2005 Microsoft Corporation。 保留所有权利。

部分版权所有 Addison-Wesley Corporation。 保留所有权利。

设计指引的详细信息,请参阅"框架设计准则: 公约、 成语和可重复使用的模式。网络图书馆"书 Krzysztof Cwalina 和布拉德 · 艾布拉姆斯,2005年艾迪生 - 韦斯利,发表。

请参见

其他资源

成员设计准则

类库开发的设计准则