VB.Net Tutorial/Class Module/Interface — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:55, 26 мая 2010
Содержание
- 1 Implement Interface
- 2 Inheritance And Interfaces
- 3 Interface Inherits Interface
- 4 Interface with Property
- 5 Interface with two methods
- 6 One Class implements two interfaces
- 7 Public Sub aMethod() Implements Interface1.Method, Interface2.Method
- 8 Reimplement interface
- 9 Two classes implement one interface
Implement Interface
<source lang="vbnet">Option Strict On
Imports System Interface Printable Sub Read( ) Sub Write(ByVal obj As Object) Property Status( ) As Integer End Interface Public Class Document Implements Printable Public Sub New(ByVal s As String) Console.WriteLine("Creating document with: {0}", s) End Sub Public Sub Read( ) Implements Printable.Read Console.WriteLine("Implementing the Read Method for Printable") End Sub Public Sub Write(ByVal o As Object) Implements Printable.Write Console.WriteLine("Implementing the Write Method for Printable") End Sub Public Property Status( ) As Integer Implements Printable.Status Get Return myStatus End Get Set(ByVal Value As Integer) myStatus = Value End Set End Property Private myStatus As Integer = 0 End Class Class Tester Public Shared Sub Main( ) Dim doc As New Document("Test Document") doc.Status = -1 doc.Read( ) Console.WriteLine("Document Status: {0}", doc.Status) End Sub End Class</source>
Creating document with: Test Document Implementing the Read Method for Printable Document Status: -1
Inheritance And Interfaces
<source lang="vbnet">Imports System Imports System.Collections Public Interface Printable
ReadOnly Property Print() As Integer
End Interface Public Class Money
Implements Printable Protected mTransactions As New ArrayList() ReadOnly Property Print() As Integer Implements Printable.Print Get Return mTransactions.Count End Get End Property Public Overridable ReadOnly Property Balance() As Double Get Dim result As Double = 0.0 Dim i As Integer For i = 0 To mTransactions.Count - 1 result += CDbl(mTransactions(i)) Next Return result End Get End Property Public Sub Add(ByVal amount As Double) mTransactions.Add(amount) End Sub Public Sub Subtract(ByVal amount As Double) mTransactions.Add(-amount) End Sub
End Class Public Class MyMoney
Inherits Money Public Overrides ReadOnly Property Balance() As Double Get Return MyBase.Balance - Print * 2 End Get End Property
End Class Module Test
Sub Main() Dim acc1 As New Money() acc1.Add(200) acc1.Subtract(40) acc1.Add(30) Console.Write("count: {0}, ", acc1.Print) Console.WriteLine("balance: {0}", acc1.Balance) Dim acc2 As New MyMoney() acc2.Add(200) acc2.Subtract(40) acc2.Add(30) Console.Write("count: {0}, ", acc2.Print) Console.WriteLine("balance: {0}", acc2.Balance) End Sub
End Module</source>
count: 3, balance: 190 count: 3, balance: 184
Interface Inherits Interface
<source lang="vbnet">Imports System
Interface Printable Sub Read( ) Sub Write(ByVal obj As Object) Property Status( ) As Integer End Interface "Printable Interface Zippable Sub Zip( ) Sub Unzip( ) End Interface "Zippable Interface Zippable2 Inherits Zippable Sub Email( ) End Interface Public Class Document Implements Zippable2, Printable Public Sub New(s As String) Console.WriteLine("Creating document with: {0}", s) End Sub Public Sub Read( ) Implements Printable.Read Console.WriteLine("Implementing the Read Method for Printable") End Sub "Read Public Sub Write(ByVal o As Object) Implements Printable.Write Console.WriteLine( _ "Implementing the Write Method for Printable") End Sub "Write Public Property Status( ) As Integer Implements Printable.Status Get Return myStatus End Get Set(ByVal Value As Integer) myStatus = Value End Set End Property Public Sub Zip( ) Implements Zippable.Zip Console.WriteLine("Implementing Zip") End Sub Public Sub Unzip( ) Implements Zippable.Unzip Console.WriteLine("Implementing Unzip") End Sub Public Sub Email( ) Implements Zippable2.Email Console.WriteLine("Implementing Email") End Sub Private myStatus As Integer = 0 End Class "Document Class Tester Shared Sub Main( ) Dim doc As New Document("Test Document") If TypeOf doc Is Printable Then Dim isDoc As Printable = doc isDoc.Read( ) Else Console.WriteLine("Could not cast to Printable") End If If TypeOf doc Is Zippable2 Then Dim ilDoc As Zippable2 = doc Console.Write("Calling both Zippable and ") Console.WriteLine("Zippable2 methods...") ilDoc.Zip( ) ilDoc.Email( ) Else Console.WriteLine("Could not cast to Zippable2") End If If TypeOf doc Is Zippable Then Dim icDoc As Zippable = doc " Console.WriteLine( _ "Treating the object as Zipible... ") icDoc.Zip( ) Else Console.WriteLine("Could not cast to Zippable") End If End Sub End Class</source>
Creating document with: Test Document Implementing the Read Method for Printable Calling both Zippable and Zippable2 methods... Implementing Zip Implementing Email Treating the object as Zipible... Implementing Zip
Interface with Property
<source lang="vbnet">Class Tester
Shared Sub Main() Dim tree As New Plant(1976) Dim person As New Animal("B", "J", 1983) Dim iAgeArray As Markable() = New Markable(1) {} iAgeArray(0) = tree iAgeArray(1) = person Console.WriteLine(tree.ToString() & ": " & _ tree.Name & vbCrLf & "Age is " & tree.Age) Console.WriteLine(person.ToString() & ": " & _ person.Name & vbCrLf & "Age is " & person.Age) Dim ageReference As Markable For Each ageReference In iAgeArray Console.WriteLine(ageReference.Name & ": " & _ "Age is " & ageReference.Age) Next End Sub " Main
End Class
Public Interface Markable
ReadOnly Property Age() As Integer ReadOnly Property Name() As String
End Interface Public Class Animal
Implements Markable Private mYearBorn As Integer Private mFirstName As String Private mLastName As String Public Sub New(ByVal firstNameValue As String, _ ByVal lastNameValue As String, _ ByVal yearBornValue As Integer) mFirstName = firstNameValue mLastName = lastNameValue mYearBorn = yearBornValue End Sub " New ReadOnly Property Age() As Integer _ Implements Markable.Age Get Return Date.Now.Year - mYearBorn End Get End Property " Age ReadOnly Property Name() As String _ Implements Markable.Name Get Return mFirstName & " " & mLastName End Get End Property " Name
End Class Public Class Plant
Implements Markable Private mRings As Integer Public Sub New(ByVal yearPlanted As Integer) mRings = Date.Now.Year - yearPlanted End Sub " New Public Sub AddRing() mRings += 1 End Sub " AddRing ReadOnly Property Age() As Integer _ Implements Markable.Age Get Return mRings End Get End Property " Age ReadOnly Property Name() As String _ Implements Markable.Name Get Return "Tree" End Get End Property " Name
End Class</source>
Plant: Tree Age is 31 Animal: B J Age is 24 Tree: Age is 31 B J: Age is 24
Interface with two methods
<source lang="vbnet">Interface Speak
Sub GoodMorning() Sub GoodEvening(ByVal CurrentDate As DateTime)
End Interface Class English
Implements Speak Public Sub GoodMorning() Implements Speak.GoodMorning Console.WriteLine("Good morning!") End Sub Public Sub GoodEvening(ByVal CurrentDate As DateTime) Implements Speak.GoodEvening Console.WriteLine("Good evening -- it is now " & CurrentDate) End Sub
End Class Class Spanish
Implements Speak Public Sub GoodMorning() Implements Speak.GoodMorning Console.WriteLine("Buenos Dias!") Console.WriteLine(Now()) End Sub Public Sub GoodEvening(ByVal CurrentDate As DateTime) Implements Speak.GoodEvening Console.WriteLine("Buenas noches -- La fetcha y hora son " & CurrentDate) End Sub
End Class
Module Module1
Sub Main() Dim Hello As New English() Dim Hola As New Spanish() Hello.GoodMorning() Hello.GoodEvening(Now()) Hola.GoodMorning() Hola.GoodEvening(Now()) End Sub
End Module</source>
Good morning! Good evening -- it is now 11/05/2007 9:29:46 PM Buenos Dias! 11/05/2007 9:29:46 PM Buenas noches -- La fetcha y hora son 11/05/2007 9:29:46 PM
One Class implements two interfaces
<source lang="vbnet">Option Strict On
Imports System Interface Printable Sub Read( ) Sub Write(ByVal obj As Object) Property Status( ) As Integer End Interface "Printable Interface Zippable Sub Zip( ) Sub Unzip( ) End Interface Public Class Document Implements Zippable, Printable Public Sub New(ByVal s As String) Console.WriteLine("Creating document with: {0}", s) End Sub "New Public Sub Read( ) Implements Printable.Read Console.WriteLine("Implementing the Read Method for Printable") End Sub "Read Public Sub Write(ByVal o As Object) Implements Printable.Write Console.WriteLine( _ "Implementing the Write Method for Printable") End Sub "Write Public Property Status( ) As Integer Implements Printable.Status Get Return myStatus End Get Set(ByVal Value As Integer) myStatus = Value End Set End Property Public Sub Zip( ) Implements Zippable.Zip Console.WriteLine("Implementing Zip") End Sub Public Sub Unzip( ) Implements Zippable.Unzip Console.WriteLine("Implementing Unzip") End Sub Private myStatus As Integer = 0 End Class Class Tester Shared Sub Main( ) Dim doc As New Document("Test Document") doc.Status = -1 doc.Read( ) doc.Zip( ) Console.WriteLine("Document Status: {0}", doc.Status) End Sub "Main End Class</source>
Creating document with: Test Document Implementing the Read Method for Printable Implementing Zip Document Status: -1
Public Sub aMethod() Implements Interface1.Method, Interface2.Method
<source lang="vbnet">Public Interface I1
Sub Draw()
End Interface Public Interface I2
Sub Draw()
End Interface Public Class Class1
Implements I1, I2 Public Sub foo() Implements I1.Draw, I2.Draw End Sub
End Class</source>
Reimplement interface
<source lang="vbnet">Class BaseClass
Implements IFormattable Implements IComparable Public Value As String Public Overridable Overloads Function ToString(ByVal _ Format As String, ByVal Provider As IFormatProvider) _ As String Implements IFormattable.ToString ToString = Value End Function Public Overridable Overloads Function CompareTo(ByVal A _ As Object) As Integer Implements IComparable.rupareTo If (Value = A.Value) Then CompareTo = 0 ElseIf (Value < A.Value) Then CompareTo = -1 Else CompareTo = 1 End If End Function Public Sub New(ByVal Value As String) Me.Value = Value End Sub
End Class Class DerivedClass
Inherits BaseClass Public Overrides Function ToString(ByVal _ Format As String, ByVal Provider As IFormatProvider) _ As String ToString = UCase(Value) End Function Public Sub New(ByVal Value As String) MyBase.New(Value) End Sub
End Class Module Module1
Sub Main() Dim A As New BaseClass("Hello") Dim B As New DerivedClass("Hi") Console.WriteLine(A) Console.WriteLine(B) Console.WriteLine(A.rupareTo(B)) End Sub
End Module</source>
Hello HI -1
Two classes implement one interface
<source lang="vbnet">public class Test
public Shared Sub Main Dim rect As New Rectangle() Dim trap As New Trapezoid() rect.Draw() trap.Draw() End Sub
End class Public Interface IShape
Function Draw() As String
End Interface Public Class Rectangle
Implements IShape Public Function Draw() As String Implements IShape.Draw Return "Drawing Rectangle" End Function
End Class Public Class Trapezoid
Implements IShape Public Function Draw() As String Implements IShape.Draw Return "Drawing Trapezoid" End Function
End Class</source>