http://topic.csdn.net/u/20080730/20/a1064fd8-58fc-4f98-8e0e-7ed8c0eed0d3.html?seed=1256715118'例子1:Imports System
Imports System.Console
Module Module1
Public Class Employee
Private PName As String
Public Property Name() As String
Get
Name = PName
End Get
Set(ByVal value As String)
PName = value
End Set
End Property
Public Sub Work()
Dim iCount As Integer
For iCount = 1 To 10
Console.WriteLine(PName & "Working" & CStr(iCount))
Next
End Sub
End Class
Public MustInherit Class Salesman
Inherits Employee
Public Corporation As String
Public City As String
End Class
Public Class storesalesman
Inherits Salesman
Public Sub Speak()
Console.WriteLine("My Name is " & Name)
End Sub
End Class
Sub Main()
Dim Person As New Employee
Person.Name = "John"
Console.WriteLine(Person.Name)
Person.Work()
Dim aStoreSalesMan As New storesalesman
aStoreSalesMan.City = "Beijing"
aStoreSalesMan.Name = "Peter"
Console.WriteLine(aStoreSalesMan.Name)
Console.WriteLine(aStoreSalesMan.City)
aStoreSalesMan.Work()
aStoreSalesMan.Speak()
Console.Read()
End SubEnd Module'例子2:Imports System
Imports System.Console
Module Module1
Public Class Employee
Private PName As String
Public Property Name() As String
Get
Name = PName
End Get
Set(ByVal value As String)
PName = value
End Set
End Property
Public Sub Work()
Dim iCount As Integer
For iCount = 1 To 10
Console.WriteLine(PName & "Working" & CStr(iCount))
Next
End Sub
End Class Public Class Teacher
Inherits Employee
Public TeachingAges As Integer
End Class Public Class Salesman
Inherits Employee
Public WorkingAges As Integer
End Class
Sub Main()
Dim MrSmith As New Teacher
MrSmith.Name = "James Smith"
MrSmith.TeachingAges = 30
Dim MSRose As New Salesman
MSRose.Name = "Jessica Rose"
MSRose.WorkingAges = 5
MrSmith.Work()
Console.WriteLine("Mr Smith's full name is " & MrSmith.Name)
Console.WriteLine("Mr Smith's working time is " & MrSmith.TeachingAges)
MSRose.Work()
Console.WriteLine("Miss Rose's full name is " & MSRose.Name)
Console.WriteLine("Mr Rose's working time is " & MSRose.WorkingAges)
Console.Read()
End SubEnd Module'例子3:Imports System
Imports System.Console
Module Module1
'Public PName As String
Public Class Employee
Private PName As String
Private Sub Work()
MsgBox("Working", MsgBoxStyle.OkOnly, PName)
End Sub
Public WriteOnly Property Name() As String
Set(ByVal value As String)
PName = value
End Set
End Property
Public Sub speakoutname()
MsgBox("My Name is " & PName, MsgBoxStyle.Information, "Employes")
End Sub
Public Sub GoToWork()
MsgBox("On the way", MsgBoxStyle.OkOnly, PName)
Work()
End Sub
End Class
Public Class Salesman
Inherits Employee
Public Sub SetName(ByVal MyName As String)
Name = MyName
End Sub
End Class
Sub Main()
Dim MrLee As New Salesman
MrLee.SetName("Vincent Lee")
MrLee.speakoutname()
MrLee.GoToWork()
End SubEnd Module'例子4:Imports System
Imports System.Console
Module Module1
Public Class A
Public Overridable Sub F()
Console.WriteLine("A.F")
End Sub
End Class
Public Class B
Inherits A
Public Overrides Sub F()
Console.WriteLine("B.F")
End Sub
End Class
Public Class C
Inherits B
Public Overridable Shadows Sub F()
Console.WriteLine("C.F")
End Sub
End Class Public Class D
Inherits C
Public Overrides Sub F()
Console.WriteLine("D.F")
End Sub
End Class Sub Main()
'Test 1
Dim d As New D()
Dim a As A = d
Dim b As B = d
Dim c As C = d
a.F() 'B.F
b.F() 'B.F
c.F() 'D.F
d.F() 'D.F
Console.WriteLine("Above is Test 1")
'Test 2
Dim b1 As New B
Dim a1 As A = b1
a1.F() 'B.F
b1.F() 'B.F
Console.WriteLine("Above is Test 2")
'Test 3
Dim a2 As New A
a2.F() 'A.F
Console.WriteLine("Above is Test 3")
'Test 4
Dim c4 As New C
c4.F() 'C.F
Console.WriteLine("Above is Test 4") 'Test 5
Dim c5 As New C
Dim b5 As B = c5
Dim a5 As A = c5
a5.F() 'B.F
b5.F() 'B.F
c4.F() 'C.F
Console.WriteLine("Above is Test 5") Console.Read()
End Sub
End Module
Imports System.Console
Module Module1
Public Class Employee
Private PName As String
Public Property Name() As String
Get
Name = PName
End Get
Set(ByVal value As String)
PName = value
End Set
End Property
Public Sub Work()
Dim iCount As Integer
For iCount = 1 To 10
Console.WriteLine(PName & "Working" & CStr(iCount))
Next
End Sub
End Class
Public MustInherit Class Salesman
Inherits Employee
Public Corporation As String
Public City As String
End Class
Public Class storesalesman
Inherits Salesman
Public Sub Speak()
Console.WriteLine("My Name is " & Name)
End Sub
End Class
Sub Main()
Dim Person As New Employee
Person.Name = "John"
Console.WriteLine(Person.Name)
Person.Work()
Dim aStoreSalesMan As New storesalesman
aStoreSalesMan.City = "Beijing"
aStoreSalesMan.Name = "Peter"
Console.WriteLine(aStoreSalesMan.Name)
Console.WriteLine(aStoreSalesMan.City)
aStoreSalesMan.Work()
aStoreSalesMan.Speak()
Console.Read()
End SubEnd Module'例子2:Imports System
Imports System.Console
Module Module1
Public Class Employee
Private PName As String
Public Property Name() As String
Get
Name = PName
End Get
Set(ByVal value As String)
PName = value
End Set
End Property
Public Sub Work()
Dim iCount As Integer
For iCount = 1 To 10
Console.WriteLine(PName & "Working" & CStr(iCount))
Next
End Sub
End Class Public Class Teacher
Inherits Employee
Public TeachingAges As Integer
End Class Public Class Salesman
Inherits Employee
Public WorkingAges As Integer
End Class
Sub Main()
Dim MrSmith As New Teacher
MrSmith.Name = "James Smith"
MrSmith.TeachingAges = 30
Dim MSRose As New Salesman
MSRose.Name = "Jessica Rose"
MSRose.WorkingAges = 5
MrSmith.Work()
Console.WriteLine("Mr Smith's full name is " & MrSmith.Name)
Console.WriteLine("Mr Smith's working time is " & MrSmith.TeachingAges)
MSRose.Work()
Console.WriteLine("Miss Rose's full name is " & MSRose.Name)
Console.WriteLine("Mr Rose's working time is " & MSRose.WorkingAges)
Console.Read()
End SubEnd Module'例子3:Imports System
Imports System.Console
Module Module1
'Public PName As String
Public Class Employee
Private PName As String
Private Sub Work()
MsgBox("Working", MsgBoxStyle.OkOnly, PName)
End Sub
Public WriteOnly Property Name() As String
Set(ByVal value As String)
PName = value
End Set
End Property
Public Sub speakoutname()
MsgBox("My Name is " & PName, MsgBoxStyle.Information, "Employes")
End Sub
Public Sub GoToWork()
MsgBox("On the way", MsgBoxStyle.OkOnly, PName)
Work()
End Sub
End Class
Public Class Salesman
Inherits Employee
Public Sub SetName(ByVal MyName As String)
Name = MyName
End Sub
End Class
Sub Main()
Dim MrLee As New Salesman
MrLee.SetName("Vincent Lee")
MrLee.speakoutname()
MrLee.GoToWork()
End SubEnd Module'例子4:Imports System
Imports System.Console
Module Module1
Public Class A
Public Overridable Sub F()
Console.WriteLine("A.F")
End Sub
End Class
Public Class B
Inherits A
Public Overrides Sub F()
Console.WriteLine("B.F")
End Sub
End Class
Public Class C
Inherits B
Public Overridable Shadows Sub F()
Console.WriteLine("C.F")
End Sub
End Class Public Class D
Inherits C
Public Overrides Sub F()
Console.WriteLine("D.F")
End Sub
End Class Sub Main()
'Test 1
Dim d As New D()
Dim a As A = d
Dim b As B = d
Dim c As C = d
a.F() 'B.F
b.F() 'B.F
c.F() 'D.F
d.F() 'D.F
Console.WriteLine("Above is Test 1")
'Test 2
Dim b1 As New B
Dim a1 As A = b1
a1.F() 'B.F
b1.F() 'B.F
Console.WriteLine("Above is Test 2")
'Test 3
Dim a2 As New A
a2.F() 'A.F
Console.WriteLine("Above is Test 3")
'Test 4
Dim c4 As New C
c4.F() 'C.F
Console.WriteLine("Above is Test 4") 'Test 5
Dim c5 As New C
Dim b5 As B = c5
Dim a5 As A = c5
a5.F() 'B.F
b5.F() 'B.F
c4.F() 'C.F
Console.WriteLine("Above is Test 5") Console.Read()
End Sub
End Module
解决方案 »
- 像素到底是什么意思?看到"鼠标移动**像素",貌似是距离的意思;但"手机**像素"又类似是图片清晰度 质量 精度
- VB安装程序
- 关于MODEM的拨号问题,请教各位在线大侠,问题解决即结贴给分~~~在线等待中..........
- 一个奇怪的问题
- 关于INI的问题
- 我参加高程,请问学习VB用什么书好?请说明作者,出版社,书名,翻译者,版本!不胜感激! 在线等候!
- 10 and 6 含义
- 什么是中断向量
- 还是ADO对象问题
- 关于vb与access数据库备份时出现"数据库以排它的方式打开"错误
- 从数据库取出这样一个字段的字符串:5+9*(8+2.54)。如何将这个字符串自动算出一个数99.86后赋给一个变量cvfol
- ----vb中文件复制的问题------
我是边看书边自己敲的,中文太难敲了。
你运行后自己体会一下,如果运行的结果与你想象的不一样,查MSDN或其他资料去学习。
子类在继承基类时,会得到基类所有的接口属性,字段和方法。但有时子类中定义的
特有属性和方法往往与基类中继承得到的接口属性和方法同名。VB.NET允许在子类中
使用与基类接口属性或方法同名的属性及方法,即允许在一个类中,相同名字的属性
或方法能够通过不同的代码,完成不同的功能,这样就实现了类的多态性。
类的多态性提高了子类的灵活性和程序的实用性。
要在子类中使用与父类属性或方法同名但功能不同的属性或方法,一个简单的方法
是把原来基类中定义的属性或方法覆盖,换成子类的相关代码,用关键字Overrides.
在基类中定义属性及方法时,可用关键字Overridable说明属性及方法允许在子类中
被覆盖。Module Module1
Public Class Employee
Protected MyWords As String
Public Overridable Property Words() As String
Get
Return MyWords
End Get
Set(ByVal value As String)
MyWords = value
End Set
End Property
Public Overridable Sub Speak()
MsgBox(MyWords, MsgBoxStyle.Critical, "Base")
End Sub
End Class Public Class Salesman
Inherits Employee
Public Overrides Property Words() As String
Get
Return MyWords
End Get
Set(ByVal value As String)
Select Case value
Case "Can I help you?", "Thanks", "Good-bye", "See you Later", "Bye Bye", "You are welcome"
MyWords = value
Case Else
MsgBox("Please use nice words and tune")
End Select
End Set
End Property
End Class
Public Class Customer
Inherits Employee
Public Overrides Sub Speak()
If MyWords = "" Then
MsgBox("Good Morning,I want to buy something", MsgBoxStyle.Information, "Customer")
Words = "Thank you"
Else
MsgBox(MyWords, MsgBoxStyle.Information, "Customer")
End If
End Sub
End Class
Sub Main()
Dim Guest As New Customer
Dim MSRose As New Salesman
MSRose.Words = "What are you doing?"
MSRose.Words = "Can I help you?"
MSRose.Speak()
Guest.Speak() 'Words is being set to "Thank you"
Guest.Speak()
MSRose.Words = "You are welcome"
MSRose.Speak()
Guest.Words = "Bye Bye"
Guest.Speak()
MSRose.Words = "See you Later"
MSRose.Speak()
End SubEnd Module
VB6是为了解决实际问题而编程,所以,真要搞这些东西,直接转C++去,C++对于这些概念有更深入透彻的理念,VB.NET里面多处来的这些东西,对于真正了解VB这个语言的本来意义的人来说,都是些画蛇添足的东西.VB6和VB.NET根本就是完全不同的两种语言,不要再混为一谈了.
可以这么看。但也可以看着是.NET平台VB6的升级版。