'例子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 Sub
End 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 Sub
End 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 Sub
End 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
21 个解决方案
#1
占沙发学习~
#2
点地板先.
#3
点地板先.
#4
点地板先.
#5
想和楼主说:你的代码一句注释都没有!!!
#6
我是边看书边自己敲的,中文太难敲了。
你运行后自己体会一下,如果运行的结果与你想象的不一样,查MSDN或其他资料去学习。
#7
补充:阅读的对象是要求熟练掌握VB6的人。
#8
例子5:类的多态:
子类在继承基类时,会得到基类所有的接口属性,字段和方法。但有时子类中定义的
特有属性和方法往往与基类中继承得到的接口属性和方法同名。VB.NET允许在子类中
使用与基类接口属性或方法同名的属性及方法,即允许在一个类中,相同名字的属性
或方法能够通过不同的代码,完成不同的功能,这样就实现了类的多态性。
类的多态性提高了子类的灵活性和程序的实用性。
要在子类中使用与父类属性或方法同名但功能不同的属性或方法,一个简单的方法
是把原来基类中定义的属性或方法覆盖,换成子类的相关代码,用关键字Overrides.
在基类中定义属性及方法时,可用关键字Overridable说明属性及方法允许在子类中
被覆盖。
子类在继承基类时,会得到基类所有的接口属性,字段和方法。但有时子类中定义的
特有属性和方法往往与基类中继承得到的接口属性和方法同名。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 Sub
End Module
#9
哈哈,支持教学贴
#10
mark
#11
我这次考计算机二级的VB还不大会
请教一下 哪几个方面是我必须精通的嗄?
请教一下 哪几个方面是我必须精通的嗄?
#12
个人认为不值得考“计算机二级”,把时间拿来写代码。
#13
教学帖我收藏
谢谢老师
谢谢老师
#14
mark
#15
为了编程而编程引申出来的东西,
VB6是为了解决实际问题而编程,
所以,真要搞这些东西,直接转C++去,C++对于这些概念有更深入透彻的理念,VB.NET里面多处来的这些东西,对于真正了解VB这个语言的本来意义的人来说,都是些画蛇添足的东西.
VB6和VB.NET根本就是完全不同的两种语言,不要再混为一谈了.
VB6是为了解决实际问题而编程,
所以,真要搞这些东西,直接转C++去,C++对于这些概念有更深入透彻的理念,VB.NET里面多处来的这些东西,对于真正了解VB这个语言的本来意义的人来说,都是些画蛇添足的东西.
VB6和VB.NET根本就是完全不同的两种语言,不要再混为一谈了.
#16
不错
#17
今天看到的最牛的评论。。。嗯。无语。
PS一句:不要随便评论某个语言什么东西是多余的,特别是在你还没能完全精通这个语言之前。
#18
支持!!
#19
可以这么看。但也可以看着是.NET平台VB6的升级版。
#20
#21
不错不错,学习了
#1
占沙发学习~
#2
点地板先.
#3
点地板先.
#4
点地板先.
#5
想和楼主说:你的代码一句注释都没有!!!
#6
我是边看书边自己敲的,中文太难敲了。
你运行后自己体会一下,如果运行的结果与你想象的不一样,查MSDN或其他资料去学习。
#7
补充:阅读的对象是要求熟练掌握VB6的人。
#8
例子5:类的多态:
子类在继承基类时,会得到基类所有的接口属性,字段和方法。但有时子类中定义的
特有属性和方法往往与基类中继承得到的接口属性和方法同名。VB.NET允许在子类中
使用与基类接口属性或方法同名的属性及方法,即允许在一个类中,相同名字的属性
或方法能够通过不同的代码,完成不同的功能,这样就实现了类的多态性。
类的多态性提高了子类的灵活性和程序的实用性。
要在子类中使用与父类属性或方法同名但功能不同的属性或方法,一个简单的方法
是把原来基类中定义的属性或方法覆盖,换成子类的相关代码,用关键字Overrides.
在基类中定义属性及方法时,可用关键字Overridable说明属性及方法允许在子类中
被覆盖。
子类在继承基类时,会得到基类所有的接口属性,字段和方法。但有时子类中定义的
特有属性和方法往往与基类中继承得到的接口属性和方法同名。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 Sub
End Module
#9
哈哈,支持教学贴
#10
mark
#11
我这次考计算机二级的VB还不大会
请教一下 哪几个方面是我必须精通的嗄?
请教一下 哪几个方面是我必须精通的嗄?
#12
个人认为不值得考“计算机二级”,把时间拿来写代码。
#13
教学帖我收藏
谢谢老师
谢谢老师
#14
mark
#15
为了编程而编程引申出来的东西,
VB6是为了解决实际问题而编程,
所以,真要搞这些东西,直接转C++去,C++对于这些概念有更深入透彻的理念,VB.NET里面多处来的这些东西,对于真正了解VB这个语言的本来意义的人来说,都是些画蛇添足的东西.
VB6和VB.NET根本就是完全不同的两种语言,不要再混为一谈了.
VB6是为了解决实际问题而编程,
所以,真要搞这些东西,直接转C++去,C++对于这些概念有更深入透彻的理念,VB.NET里面多处来的这些东西,对于真正了解VB这个语言的本来意义的人来说,都是些画蛇添足的东西.
VB6和VB.NET根本就是完全不同的两种语言,不要再混为一谈了.
#16
不错
#17
今天看到的最牛的评论。。。嗯。无语。
PS一句:不要随便评论某个语言什么东西是多余的,特别是在你还没能完全精通这个语言之前。
#18
支持!!
#19
可以这么看。但也可以看着是.NET平台VB6的升级版。
#20
#21
不错不错,学习了