C++/CX:类的继承

翻译自官方MSDN文档:

An authored base class can inherit a class, and optionally one or more interfaces which must be implemented by the base class.And in a class derived from the base class, you can override a virtual method in the base class

一个授权的基本类能继承于另外一个类,并且能实现一个或者多个接口。从基类派生的类,能覆盖基类中的徐方法。、

Guidance:(引言)

Class inheritance requires that a class be accessible; that is, declared public or protected.As a result, metadata about the class is injected into the corresponding Windows metadata file (.winmd).Class definitions that are declared private, abstract, or pure virtual ("=0") contradict the accessibility principle.

类的继承需要一个类是可以呗访问的,即被申明为public或者是protected。因此,类的元数据同样被写入响应的windows 元素据文件中(.winmd). 类定义被声明为私有的,抽象的或者纯虚的违背可访问原理。

Virtual methods are naturally more affected by class inheritance.With the accessibility principle and virtual methods in mind, the following guidance is offered.

虚方法自然受到类继承关系的影响。根据可反问原理和印象中的虚方法,提供以下指导意见:

1.By default, all classes are extensible. You can make a class non-extensible with the sealed keyword, but an error is emitted if you declare a protected method in a sealed class.

在默认情况下,所有类都是可扩展的。你可以使用sealed关键字使一个类变成不可扩展的,但是如果你在sealed类中声明一个protected方法,将会报错。

2.It is recommended that base classes provide a default constructor.

建议积累必须提供一个默认构造函数。

3.A base class can define protected non-virtual methods.

一个基类能定义protected的非虚函数。

4.Virtual methods must be declared protected or public, and can't specify a private modifier (including protected private). Otherwise, an error is emitted.

虚方法必须被声明为protected或者public,不能被声明为private,否则将会报错。

5.In constructors and destructors, virtual calls invoke the most derived override. Unless you are certain which override will be called, it is recommended that you not make virtual calls in a constructor or destructor.

在构造函数和析构函数中,virtual calls 调用最派生的覆盖方法。除非你确定哪一个“重写”被调用,否则建议不要在构造函数和析构函数中调用虚函数。

Example:(示例)

The following three examples author a base class, then a derived class, then focuses on overridding a method in a final derived class.

接下来的三个例子编写了一个基类,一个派生类,在最终派生类中集中于重写一个方法。

ref class Button 
{
public:
    Button() : s(0,0)
    {
    }
    Button(Size defaultSize) : s(defaultSize)
    {
        // In C++/CX, this is a virtual call. 
        // To call the function non-virtually, use Button::OnSizeChanged().
        OnSizeChanged(); 
    }
public:
    void SetSize(Size size)
    {
        s = size;
        OnSizeChanged();
    }
    void Measure(Size availableSize)
    {
        // Doesn’t call MeasureOverride(). It calls the most derived type.
        Size s = MeasureOverride(sz); 
 
        // The following calls the non-virtual MeasureOverride() directly.
        Size s2 = Button::MeasureOverride(sz);  
    }
    event Click;
public:
    virtual void OnPublicOverride() {}
protected: 
    virtual void RealignContent(Size sz) {}
    Size GetInternalSize() { return s; }
    virtual Size MeasureOverride(Size availableSize) {return availableSize;}
    virtual void OnClick() {}
    virtual void OnSizeChanged() {}
    int GetTemplateChild() 
    {
        return;
    }
private:
    Size s;
};

Authoring a derived class(编写派生类)

The following code authors a class, BestCustomButton class, that is derived from the Button class. The RealignContent() method overrides the same method in the base class. Note that to be overridden, RealignContent() must be declared virtual in the base and derived classes, and that the override keyword must be specified in the derived class.

接下来的代码编写了类BestCustomButton ,派生于Button类。RealignContent() 方法重写了基类中的方法。记住,要被重写,RealignContent() 方法必须在基类和派生类中被声明为virtual,并且必须在派生类中指定override 关键字。

ref class BestCustomButton: public Button 
{ 
      public: 
      BestCustomButton(): Button(Size(120, 160))  
      { 
            // Call to base class protected method.
            Size sz = GetInternalSize(); 
            // Call to virtual method defined in base class calls the override 
            // from the most derived type.
            RealignContent(sz); 
       } 
       
       protected:
       // Define a new virtual method.
       virtual Size CustomMarginSize() {}  
       
       // Override a base class virtual method.
       virtual Size RealignContent(Size sz) override  
       { 
           // Call to a virtual method calls the override from the most derived type.
           Size szMargin = CustomMarginSize();  
           if(sz.Width < szMargin.Width) sz.Width = szMargin.Width; 
           return sz; 
        } 
};

Overridding methods in a derived class(在派生类中重写方法)

The following code snippet shows how to override a method in the MyButton class that was derived from the BestCustomButton class defined in the previous example. As was shown in the BestCustomButton class, the CustomMarginSize() method is declared virtual in the base and derived class and is overridden in the derived class.

接下来的代码块展示了如何去在BestCustomButton类的派生类MyButton中重写方法。正如已经展示过的BestCustomButton类,CustomMarginSize()方法在基类和派生类被声明为virtual并且在派生类中被重写。

ref class MyButton sealed : public BestCustomButton 
{ 
      protected: 
      // overrides a base class virtual method 
      virtual Size CustomMarginSize()override 
            { return Size(160, 12); } 
};

 

posted @ 2012-06-17 14:13  狼哥2  阅读(416)  评论(0编辑  收藏  举报