翻译自官方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); }
};

转载于:https://www.cnblogs.com/deagle/articles/2552579.html

C++/CX:类的继承相关推荐

  1. C++:随笔5---this指针和类的继承

    this指针:(通过一个典型的例子来认识他) class Human {char fishc;Human(char fishc);//构造函数}; Human::Human(char fishc)// ...

  2. 【廖雪峰python进阶笔记】类的继承

    1. 继承 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object):def __init__(self, ...

  3. LotusScript类的继承

    最近在修改产品时,时常遇到需要修改一个自定义类的方法,但是这个类又不能给整个(代码保密),这时如何调试就变得很麻烦了.下面通过类的继承就可以很方便的解决这个问题了. Option PublicClas ...

  4. java类接口实验_实验3_Java类的继承和接口的定义和使用

    本次有三题:学会Java类的继承.接口的定义和使用 // Ex3_1.java /** * 题目要求: * 修改例5.7(P95)实现在页面中拖动鼠标画出矩形,矩形的对角线为点击并拖动鼠标形成的直线线 ...

  5. python 类的继承,类中的函数调用以及类输入数组

    第一部分,类的继承 先给出一个例子: class People(): #定义类,要大写,python 2中要在括号里加如object这个单词def __init__(self,n,a,w): #定义一 ...

  6. python 笔记 之 类的继承与重写

    2019独角兽企业重金招聘Python工程师标准>>> ''' 6.继承 super 重写 调用:先找子类中的方法,如果子类中找不到就到父类中找 多继承:如果父类中都有该方法,那么先 ...

  7. python中的继承有什么特点_python类的继承是什么?类的继承有什么样的规则?

    在这篇文章之中我们来了解一下python类的继承,对于刚刚接触到python这一编程语言的朋友来说,对于python类的继承的了解应该比较少,不过没关系,在接下来的文章之中我们就来了解一下python ...

  8. C++知识点44——类的继承概述

    类的继承,是新类从已有类那里得到已有的特性,或从已有类产生新类的过程.原有类称为基类或父类,新类称为派生类或子类. 子类通过类派生列表确定子类继承了哪些类.类派生列表的形式如下 class 子类名:访 ...

  9. python3 入门 (四) 类与继承

    Python 类 Python中的类提供了面向对象编程的所有基本功能:类的继承机制允许多个基类,派生类可以覆盖基类中的任何方法,方法中可以调用基类中的同名方法. 对象可以包含任意数量和类型的数据. p ...

最新文章

  1. html word-wrap,CSS3 Word-wrap
  2. oracle rac scan ip 用途 原理
  3. 袁崇焕·任志强·张纪中
  4. 20145317 《网络对抗技术》免杀原理与实践
  5. VTK:Texture之TextureCutQuadric
  6. Linux设备驱动之Ioctl控制
  7. 阿里妈妈数据字化营销与MaxCompute的不解之缘
  8. 华为程序员写代码十几年没有被拿去“祭天”,靠的是这5条口诀
  9. JavaScript玩转机器学习:保存并加载 tf.Model
  10. p10可以适配鸿蒙吗,鸿蒙系统支持旧机型吗
  11. vmware不能resume问题,Collect Support Data,vmware.log
  12. 使用DB2遇到的一些错误SQLCODE=-551,SQLCODE: -204,SQLCODE:-433,SQLCODE: -104,rg.springframework.beans.factory.B
  13. 将MongoDB安装在移动硬盘
  14. 无处不在的人生压力让人变得孤独抑郁
  15. YouTuBe油管/头条点赞订阅关注分享提示PR模板Mogrt
  16. QQ电脑版 快捷cmd指令
  17. [行人重识别论文阅读]Fine-Grained Shape-Appearance Mutual Learning for Cloth-Changing Person Re-Identification
  18. ceph-deploy osd activate xxx bluestore ERROR
  19. bootstrap自采样再理解
  20. HFSS中文手册_568页_微波仿真论坛出品[免费下载]

热门文章

  1. Linux之find exec
  2. FontAwesome图标大全
  3. 在线PS(PhotoShop),打开PSD文件,图像处理
  4. 关闭防火墙linux 16.04,如何在Ubuntu 16.04上配置和设置防火墙
  5. consul集群搭建,配合nginx完成服务动态发现和健康检查
  6. echo 单引号和双引号
  7. 关于JDK中的集合总结(二)
  8. 86.最少连接算法以及如何跨worker进程生效
  9. 【免费毕设】基于jsp的新闻发布系统(论文)
  10. 属于哪个单元_到底怎么挑?动铁、动圈、动铁动圈混合买耳机哪个更好?