在对话框中加一个标准Edit控件
2.
用类向导为Edit控件添加变量,
变量类型是CEdit
3.
手工到.h中把变量类型改为你自己的CEdit派生类!
Visual Basic 编程指南如何:创建派生类
从另一个类继承
添加一条 Inherits 语句作为派生类中的第一条语句,该语句具有与要用作基类的类相同的名称。Inherits 语句必须是类语句后的第一个非注释语句。
示例
下面的示例定义两个类。第一个类是具有两个方法的基类。第二个类从基类中继承这两个方法,重写第二个方法,并定义一个名为 Field 的字段。
Visual Basic
Class Class1
Sub Method1()
MsgBox("This is a method in the base class.")
End Sub
Overridable Sub Method2()
MsgBox("This is another method in the base class.")
End Sub
End Class
Class Class2
Inherits Class1
Public Field2 As Integer
Overrides Sub Method2()
MsgBox("This is a method in a derived class.")
End Sub
End Class
Protected Sub TestInheritance()
Dim C1 As New Class1
Dim C2 As New Class2
C1.Method1() ' Calls a method in the base class.
C1.Method2() ' Calls another method from the base class.
C2.Method1() ' Calls an inherited method from the base class.
C2.Method2() ' Calls a method from the derived class.
End Sub
当运行过程 TestInheritance 后,看到下面的消息:
This is a method in the base class.
This is another method in the base class.
This is a method in the base class.
This is a method in a derived class.
子类,在面向对象开发中代表一个重要的思想——继承。
继承,一个对象直接使用另一对象的属性和方法。即子类可以使用父类存在的处理。
从宏观的角度来看,继承是多态的基础,是面向对象开发的重要组成。
从局部的角度来看,继承可以节省代码开支,提高内聚,优化代码的可维护性。
创建子类的方法如下:
// 单继承class <派生类名>:<继承方式><基类名>
{
<派生类新定义成员>
}
// 多重继承
class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,…
{
<派生类新定义成员>
}
<继承方式>存在三种:公有继承(public)、私有继承(private)、保护继承(protected)
1. 公有继承(public)
公有继承的特点是基类的公有成员和保护成员作为派生类的成员时,它们都保持原有的状态,而基类的私有成员仍然是私有的。
2. 私有继承(private)
私有继承的特点是基类的公有成员和保护成员都作为派生类的私有成员,并且不能被这个派生类的子类所访问。
3. 保护继承(protected)
上面部分内容引用自百度百科<继承性>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)