VB.Net/Class/Interface — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:42, 26 мая 2010
Содержание
- 1 Class implements two interfaces
- 2 Define and use Interface
- 3 Define and use Interface Age
- 4 Generic Class and Interface
- 5 Implement an Interface
- 6 Implements an Interface
- 7 Implements Two Interfaces
- 8 Interface Inherits another Interface
- 9 Interface inherits interface
- 10 One Class implements two Interfaces which have the same name method
Class implements two interfaces
Imports System
Public Class MainClass
Shared Sub Main()
Dim doc As New Document("Test Document")
doc.Status = -1
doc.Read( )
doc.rupress( )
Console.WriteLine("Document Status: {0}", doc.Status)
End Sub
End Class
Interface IStorable
Sub Read( )
Sub Write(ByVal obj As Object)
Property Status( ) As Integer
End Interface
Interface ICompressible
Sub Compress( )
Sub Decompress( )
End Interface
Public Class Document
Implements ICompressible, IStorable
Public Sub New(ByVal s As String)
Console.WriteLine("Creating document with: {0}", s)
End Sub
Public Sub Read( ) Implements IStorable.Read
Console.WriteLine("Implementing the Read Method for IStorable")
End Sub
Public Sub Write(ByVal o As Object) Implements IStorable.Write
Console.WriteLine( _
"Implementing the Write Method for IStorable")
End Sub
Public Property Status( ) As Integer Implements IStorable.Status
Get
Return myStatus
End Get
Set(ByVal Value As Integer)
myStatus = Value
End Set
End Property
Public Sub Compress( ) Implements ICompressible.rupress
Console.WriteLine("Implementing Compress")
End Sub
Public Sub Decompress( ) Implements ICompressible.Decompress
Console.WriteLine("Implementing Decompress")
End Sub
Private myStatus As Integer = 0
End Class
Define and use Interface
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim doc As New Document("Test Document")
doc.Status = -1
doc.Read( )
Console.WriteLine("Document Status: {0}", doc.Status)
End Sub
End Class
Interface IStorable
Sub Read( )
Sub Write(ByVal obj As Object)
Property Status( ) As Integer
End Interface "IStorable
Public Class Document
Implements IStorable
Public Sub New(ByVal s As String)
Console.WriteLine("Creating document with: {0}", s)
End Sub "New
" implement the Read method
Public Sub Read( ) Implements IStorable.Read
Console.WriteLine("Implementing the Read Method for IStorable")
End Sub "Read
" implement the Write method
Public Sub Write(ByVal o As Object) Implements IStorable.Write
Console.WriteLine( _
"Implementing the Write Method for IStorable")
End Sub "Write
" implement the property
Public Property Status( ) As Integer Implements IStorable.Status
Get
Return myStatus
End Get
Set(ByVal Value As Integer)
myStatus = Value
End Set
End Property
" store the value for the property
Private myStatus As Integer = 0
End Class "Document
Define and use Interface Age
Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim person As New CPerson("A", "B", 1983)
Dim iAgeArray As IAge() = New IAge(1) {}
iAgeArray(0) = person
iAgeArray(1) = New CPerson("C", "D", 1999)
Console.WriteLine( person.ToString() & ": " & _
person.Name & vbCrLf & "Age is " & person.Age )
Dim ageReference As IAge
For Each ageReference In iAgeArray
Console.WriteLine( ageReference.Name & ": " & _
"Age is " & ageReference.Age )
Next
End Sub
End Class
Public Interface IAge
ReadOnly Property Age() As Integer
ReadOnly Property Name() As String
End Interface
Public Class CPerson
Implements IAge
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
If (yearBornValue > 0 AndAlso _
yearBornValue <= Date.Now.Year) Then
mYearBorn = yearBornValue
Else
mYearBorn = Date.Now.Year
End If
End Sub
ReadOnly Property Age() As Integer _
Implements IAge.Age
Get
Return Date.Now.Year - mYearBorn
End Get
End Property
ReadOnly Property Name() As String _
Implements IAge.Name
Get
Return mFirstName & " " & mLastName
End Get
End Property
End Class
Generic Class and Interface
Imports System.Collections
Imports System.Collections.Generic
Public Class MainClass
Public Shared Sub Main()
Dim aCat As New Talker(Of Cat)(New Cat())
Dim aDog As New Talker(Of Dog)(New Dog())
aCat.TalkIt()
aDog.TalkIt()
End Sub
End Class
Public Interface ITalker
Sub Talk()
End Interface
Public Class Talker(Of T As ITalker)
Dim talkerItem As T
Public Sub New(ByVal shooterItem As T)
Me.talkerItem = shooterItem
End Sub
Public Sub TalkIt()
Me.talkerItem.Talk()
End Sub
End Class
Public Class Dog
Implements ITalker
Public Sub Talk() Implements ITalker.Talk
Console.Out.WriteLine("Dog->Talk")
End Sub
End Class
Public Class Cat
Implements ITalker
Public Sub Talk() Implements ITalker.Talk
Console.Out.WriteLine("Cat->Talk")
End Sub
End Class
Implement an Interface
Imports System
Imports System.Collections
Public Class MainClass
Shared Sub Main()
Dim e1 As New Programmer("E1", 50000)
Dim e2 As New Employee("E2", 40000)
Dim theTeam As Employee() = {e1, e2}
e1.MyTeam = theTeam
Console.WriteLine(e1.Rate(e2))
End Sub
End Class
Public Interface ILead
Sub SpendMoraleFund(ByVal amount As Decimal)
Function Rate(ByVal aPerson As Employee) As String
Property MyTeam() As Employee()
Property MoraleFund() As Decimal
End Interface
Public Class Programmer
Inherits Employee
Implements ILead
Private m_MoraleFund As Decimal
Private m_MyTeam As Employee()
Public Function Rate(ByVal aPerson As Employee) As String _
Implements ILead.Rate
Return aPerson.TheName & " rating to be done"
End Function
Public Property MyTeam() As Employee() _
Implements ILead.MyTeam
Get
Return m_MyTeam
End Get
Set(ByVal Value As Employee())
m_MyTeam = Value
End Set
End Property
Public Sub SpendMoraleFund(ByVal amount As Decimal) _
Implements ILead.SpendMoraleFund
"spend some money
Console.WriteLine("Spent " & amount.ToString())
End Sub
Public Property OurMoraleFund() As Decimal Implements ILead.MoraleFund
Get
Return m_MoraleFund
End Get
Set(ByVal Value As Decimal)
m_MoraleFund = Value
End Set
End Property
Public Sub New(ByVal theName As String, ByVal curSalary As Decimal)
MyBase.New(theName, curSalary)
End Sub
End Class
Public Class Employee
Private m_Name As String
Private m_Salary As Decimal
Public Sub New(ByVal theName As String, ByVal curSalary As Decimal)
m_Name = theName
m_Salary = curSalary
End Sub
Public ReadOnly Property TheName() As String
Get
Return m_Name
End Get
End Property
Public ReadOnly Property Salary() As Decimal
Get
Return MyClass.m_Salary
End Get
End Property
End Class
Implements an Interface
Imports System
Public Class MainClass
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
Interface IStorable
Sub Read()
Sub Write(ByVal obj As Object)
Property Status() As Integer
End Interface "IStorable
Public Class Document
Implements IStorable
Public Sub New(ByVal s As String)
Console.WriteLine("Creating document with: {0}", s)
End Sub
Public Sub Read() Implements IStorable.Read
Console.WriteLine("Implementing the Read Method for IStorable")
End Sub
Public Sub Write(ByVal o As Object) Implements IStorable.Write
Console.WriteLine( _
"Implementing the Write Method for IStorable")
End Sub
Public Property Status() As Integer Implements IStorable.Status
Get
Return myStatus
End Get
Set(ByVal Value As Integer)
myStatus = Value
End Set
End Property
Private myStatus As Integer = 0
End Class "Document
Implements Two Interfaces
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim doc As New Document("Test Document")
doc.Status = -1
doc.Read( )
doc.rupress( )
Console.WriteLine("Document Status: {0}", doc.Status)
End Sub
End Class
Interface IStorable
Sub Read( )
Sub Write(ByVal obj As Object)
Property Status( ) As Integer
End Interface "IStorable
" here"s the new interface
Interface ICompressible
Sub Compress( )
Sub Decompress( )
End Interface "ICompressible
" Document implements both interfaces
Public Class Document
Implements ICompressible, IStorable
" the document constructor
Public Sub New(ByVal s As String)
Console.WriteLine("Creating document with: {0}", s)
End Sub "New
" implement IStorable
Public Sub Read( ) Implements IStorable.Read
Console.WriteLine("Implementing the Read Method for IStorable")
End Sub "Read
Public Sub Write(ByVal o As Object) Implements IStorable.Write
Console.WriteLine( _
"Implementing the Write Method for IStorable")
End Sub "Write
Public Property Status( ) As Integer Implements IStorable.Status
Get
Return myStatus
End Get
Set(ByVal Value As Integer)
myStatus = Value
End Set
End Property
" implement ICompressible
Public Sub Compress( ) Implements ICompressible.rupress
Console.WriteLine("Implementing Compress")
End Sub "Compress
Public Sub Decompress( ) Implements ICompressible.Decompress
Console.WriteLine("Implementing Decompress")
End Sub "Decompress
Private myStatus As Integer = 0
End Class
Interface Inherits another Interface
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim doc As New Document("Test Document")
If TypeOf doc Is IStorable Then
Dim isDoc As IStorable = doc
isDoc.Read( )
Else
Console.WriteLine("Could not cast to IStorable")
End If
If TypeOf doc Is ICompressible2 Then
Dim ilDoc As ICompressible2 = doc
Console.Write("Calling both ICompressible and ")
Console.WriteLine("ICompressible2 methods...")
ilDoc.rupress( )
ilDoc.LogSavedBytes( )
Else
Console.WriteLine("Could not cast to ICompressible2")
End If
If TypeOf doc Is ICompressible Then
Dim icDoc As ICompressible = doc "
Console.WriteLine( _
"Treating the object as Compressible... ")
icDoc.rupress( )
Else
Console.WriteLine("Could not cast to ICompressible")
End If
End Sub
End Class
Interface IStorable
Sub Read( )
Sub Write(ByVal obj As Object)
Property Status( ) As Integer
End Interface "IStorable
Interface ICompressible
Sub Compress( )
Sub Decompress( )
End Interface "ICompressible
Interface ICompressible2
Inherits ICompressible
Sub LogSavedBytes( )
End Interface "ICompressible2
Public Class Document
Implements ICompressible2, IStorable
Public Sub New(s As String)
Console.WriteLine("Creating document with: {0}", s)
End Sub "New
Public Sub Read( ) Implements IStorable.Read
Console.WriteLine("Implementing the Read Method for IStorable")
End Sub "Read
Public Sub Write(ByVal o As Object) Implements IStorable.Write
Console.WriteLine("Implementing the Write Method for IStorable")
End Sub "Write
Public Property Status( ) As Integer Implements IStorable.Status
Get
Return myStatus
End Get
Set(ByVal Value As Integer)
myStatus = Value
End Set
End Property
" implement ICompressible
Public Sub Compress( ) Implements ICompressible.rupress
Console.WriteLine("Implementing Compress")
End Sub "Compress
Public Sub Decompress( ) Implements ICompressible.Decompress
Console.WriteLine("Implementing Decompress")
End Sub "Decompress
" implement ICompressible2
Public Sub LogSavedBytes( ) Implements ICompressible2.LogSavedBytes
Console.WriteLine("Implementing LogSavedBytes")
End Sub "LogSavedBytes
" hold the data for IStorable"s Status property
Private myStatus As Integer = 0
End Class "Document
Interface inherits interface
Imports System
Public Class MainClass
Shared Sub Main()
Dim doc As New Document("Test Document")
If TypeOf doc Is IStorable Then
Dim isDoc As IStorable = doc
isDoc.Read()
Else
Console.WriteLine("Could not cast to IStorable")
End If
If TypeOf doc Is ICompressible2 Then
Dim ilDoc As ICompressible2 = doc
Console.Write("Calling both ICompressible and ")
Console.WriteLine("ICompressible2 methods...")
ilDoc.rupress()
ilDoc.LogSavedBytes()
Else
Console.WriteLine("Could not cast to ICompressible2")
End If
If TypeOf doc Is ICompressible Then
Dim icDoc As ICompressible = doc
Console.WriteLine( _
"Treating the object as Compressible... ")
icDoc.rupress()
Else
Console.WriteLine("Could not cast to ICompressible")
End If
End Sub
End Class
Interface IStorable
Sub Read()
Sub Write(ByVal obj As Object)
Property Status() As Integer
End Interface
Interface ICompressible
Sub Compress()
Sub Decompress()
End Interface
Interface ICompressible2
Inherits ICompressible
Sub LogSavedBytes()
End Interface
Public Class Document
Implements ICompressible2, IStorable
Public Sub New(s As String)
Console.WriteLine("Creating document with: {0}", s)
End Sub
Public Sub Read() Implements IStorable.Read
Console.WriteLine("Implementing the Read Method for IStorable")
End Sub
Public Sub Write(ByVal o As Object) Implements IStorable.Write
Console.WriteLine( _
"Implementing the Write Method for IStorable")
End Sub
Public Property Status() As Integer Implements IStorable.Status
Get
Return myStatus
End Get
Set(ByVal Value As Integer)
myStatus = Value
End Set
End Property
Public Sub Compress() Implements ICompressible.rupress
Console.WriteLine("Implementing Compress")
End Sub
Public Sub Decompress() Implements ICompressible.Decompress
Console.WriteLine("Implementing Decompress")
End Sub
Public Sub LogSavedBytes() Implements ICompressible2.LogSavedBytes
Console.WriteLine("Implementing LogSavedBytes")
End Sub
Private myStatus As Integer = 0
End Class
One Class implements two Interfaces which have the same name method
Imports System
Imports System.Data
Imports System.Collections
public class MainClass
Shared Sub Main()
Dim c As New Class1()
Dim i2 As MySecondInterface
c.rumonFunction()
i2 = c
i2.rumonFunction()
End Sub
End Class
Interface MyFirstInterface
Sub UniqueFunction()
Sub CommonFunction()
End Interface
Interface MySecondInterface
Sub SecondUniqueFunction()
Sub CommonFunction()
End Interface
Public Class Class1
Implements MyFirstInterface
Implements MySecondInterface
Sub UniqueFunction() Implements MyFirstInterface.UniqueFunction
End Sub
Sub SecondUniqueFunction() Implements MySecondInterface.SecondUniqueFunction
End Sub
Sub CommonFunction() Implements MyFirstInterface.rumonFunction
Console.WriteLine("Common Function on first interface")
End Sub
Sub CommonFunctionSecondInterface() Implements MySecondInterface.rumonFunction
Console.WriteLine("Common function on second interface")
End Sub
End Class