Добавил:
ivanov666
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Объектно-ориентированное программирование на языке Visual Basic 2008 в среде разработки Microsoft Visual Studio 2008 и .NET Framework. Часть 2. Учебно
.pdf
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim p3Student As New Student()
MsgBox(p3Student.mName & " " & p3Student.mCourse & " " & p3Student.Specialty)
End Sub
p3Student
mName ,
mCourse 0 Specialty "application
programmer".
p3Student ,
, mName "Smith", mCourse
5, Specialty "application
programmer".
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim p3Student As New Student("Smith", 5)
MsgBox(p3Student.mName & " " & p3Student.mCourse & " " & p3Student.Specialty)
End Sub
, . , Visual Studio IDE IntelliSense, , .
(shared)
(, ), ,
, .
Visual Basic 2008
, . ,
.
Shared. , Shared
.
,
, - . ,
, , , ,
. . Shared
count, counter,
count.
Public Class Student
41

Private Shared count As Integer
Public mName As String
Public mCourse As Integer
Public Specialty As String
Private BirthDate As Date
Public Sub New(ByVal name As String, ByVal course As Integer)
mName = name
mCourse = course
Specialty = "application programmer"
End Sub
Public Sub New()
Specialty = "application programmer"
count += 1
End Sub
Public ReadOnly Property counter() As Integer
Get
Return count
End Get
End Property
End Class
,
Student:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim pStudent As New Student()
pStudent = New Student()
pStudent = New Student()
pStudent = New Student()
MessageBox.Show(pStudent.counter)
End Sub
4, pStudent Student 4 . count
, 1,
, .
(overloading)
, Integer String,
+, - , = , <> . ,
,
.
, ,
, .
.
Operator
,
42

Sub, Function Property.
>, <=, < , <= ,= <> . ,
Student :
Public Class Student
Private Shared count As Integer
Public mName As String
Public mCourse As Integer
Public Specialty As String
Private BirthDate As Date
Public Sub New(ByVal name As String, ByVal course As Integer)
mName = name
mCourse = course
Specialty = "application programmer"
End Sub
Public Shared Operator =(ByVal StudentX As Student, ByVal StudentY As Student) As Boolean
Return StudentX.mName = StudentY.mName
End Operator
Public Shared Operator <>(ByVal StudentX As Student, ByVal StudentY As Student) As
Boolean
Return StudentX.mName <> StudentY.mName
End Operator
End Class
, = <>.
, =, .
, .
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim pStudent As New Student("Smith", 5)
Dim p1Student = New Student("Brown", 2)
MsgBox(CStr(pStudent = p1Student))
End Sub
True,
False. False,
: Smith Brown.
= <>
.
, ... .
String
Student CType.
Public Class Student
Private Shared count As Integer
Public mName As String
Public mCourse As Integer
Public Specialty As String
43

Private BirthDate As Date
Public Shared Narrowing Operator CType(ByVal name As String) As Student
Dim obj As New Student
obj.mName = name
Return obj
End Operator
End Class
CType( ) :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
Dim nn As String = "Halvorson"
pStudent = CType(nn, Student)
MsgBox(pStudent.mName)
End Sub
String ,
Student, ,
(Narrowing).
Student String,
(Widening):
Public Shared Widening Operator CType (ByVal pStudent As Student) As String
Return pSudent.Name
End Operator
, :
>, < . -
=, < > . -
>=, <= .
CType . , -
-, + . .
*, /, \ ,^, Mod , , Mod.
& . x & y -
And, Or . x And y, x Or y. -
x > y x < y. ,
.
x = y x< >y. ,
.
x > = y x < = y.
, .
, . Narrowing,
, , Widening,
.
x - y x + y.
x - = y x + = y.
x * y, x / y, x \ y, x ^ y x Mod y.
, .
, .
44

3
.
-
, .
, :
1. .
2. .
3. .
4. .
5. .
6. .
1. . 1.
2. , .
2. .
3. .
4. .
5. .
6. .
7. .
1
1 :
p1=(x1+i y1) p2=(x2+i y2), -
, x1 x2 , y1 y1
.
*
2 :
p1=(x1+i y1) p2=(x2+i y2), -
, x1 x2 , y1 y1 .
/
3 z r
f ( )
z = r·(cos(f) + i sin(f)).
/
(ݔ
+ݕଵi )* (ݔଶ +ݕଶi ) = (ݔଵ+ ݔଶ ) + (ݕଵ + ݕ
ଵ
௫భା௬భ୧
௫
ା௬మ୧
మ
௫భ௫మା௬భ௬
=
௫మ௬భି௫భ௬
మ
ା୷
+
మ
మ
୶
మ
୶
మ
)i
ଶ
మ
ା୷
i
మ
మ
మ
మ
45

ݎ
݂
݂
݂
݂
݂
݂
݂
݂
(cos(
) + i sin(
ଵ
ଵ
ଵ
)) : ݎ
(cos(
ଶ
భ
(cos(
మ
) + i sin(
ଶ
ଵ
െ
)) =
ଶ
) + i sin(
ଶ
െ
))
ଵ
ଶ
4 z r
f ( )
z = r·(cos(f) + i sin(f)).
^ n-
( r·(cos(f) + i sin(f))^n = ݎ
5
a=(ܽ
, ܽଶ,…, ܽ) b=(ܾଵ, ܾଶ,…, ܾ
ଵ
. a * b =
6
a=(ܽ
, ܽଶ,…, ܽ
ଵ
) s. * -
s*a = (s*ܽ
(cos(nf) + i sin(nf))
). *
σ
ܽ
ܾ
.
ୀଵ
, ݏכܽଶ,…, ݏכܽ
ଵ
)
7 s. * -
s* , s*
, s.
8 A B. + C = A
+ B, ܿ
= ܽ + ܾ
.
9 A B. - C = A
- B, ܿ
= ܽ - ܾ
10 A B. * -
C = A* B, ܿ
11
(ܽ
.
σ
=
ୀଵ
ܽȉܾ
) (-ܽ
.
).
CType .
:
Public Class Matrix
Public vect() As Single
Public Shared Narrowing Operator CType(ByVal interVect() As Single) As Student
Dim obj As New Matrix
ReDim obj.vect(15)
For I As Integer = 1 To 15
obj.vect(i) = -interVect(i)
Next
Return obj
End Operator
End Class
Button1
Public Class Form1
Public nn() As Single
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
Dim pStudent As New Student
ReDim nn(5)
For i As Integer = 1 To 5
nn(i) = InputBox(“ “ & i & "- ")
Next
pStudent = CType(nn, Student)
For i As Integer = 1 To 5
MsgBox(pStudent.vect(i))
46

Next
௬
௬
End Sub
End Class
12 , CType, -
A –A, -A=(-1)·A.
13
a=(ܽ
, .. i = 1, 2,…, n ˊː˃˚ˈːˋˈܽ
, ܽଶ,…, ܽ) b=(ܾଵ, ܾଶ,…, ܾ
ଵ
) , -
= ܾ
.
= a=b.
14
a=(ܽ
ˊː˃˚ˈːˋˈܽ
, ܽଶ,…, ܽ) b=(ܾଵ, ܾଶ,…, ܾ
ଵ
) ,
<> ܾ
, i =
1, 2,…, n Ǥ <>
a<>b.
15
a=(ܽ
i=1, 2, …, n ˊː˃˚ˈːˋˈܽ
, ܽଶ,…, ܽ) b=(ܾଵ, ܾଶ,…, ܾ
ଵ
ܾ
. >
) ,
.
16
a=(ܽ
i=1, 2, …, n ˊː˃˚ˈːˋˈܽ
, .
, ܽଶ,…, ܽ) b=(ܾଵ, ܾଶ,…, ܾ
ଵ
<= ܾ
. <= -
) ,
17 , Integer.
, .
And , .
18 A B ,
, . \
A \ B.
19 , (
yearnow), ( monthnow) (
daynow) :
Dim rightnow As Date = Now
Dim yearnow As Integer = rightnow.Year
Dim monthnow As Integer = rightnow.Month
Dim daynow As Integer = rightnow.Day
&
...
20 z = x + iy.
CType ݖ
21 z = x + iy.
CType
22 z = x + iy.
CType ݁
ଶ
(ݔଶ - ݕ
ଵ
(
௭
௭
݁௫cos y + i ݁
௫
మ
మ
௫
ା
ଶ
) + i2·x·y
) + i(-
మ
௫
௫
sin y.
௬
)
మ
ା
1. .
2. .
3. .
4. .
47

5. , -
.
6.
7. .
:
p1=(x1+i y1) p2=(x2+i y2), , x1 x2 , y1 y1 .
+
p1+p2=(x1+x2) + i (y1+y2).
Complex,
, imaginary,
,
. imaginary
ToString()
.
:
1. Visual Studio 2008.
2. (New Project).
3. MenuStrip, -
Panel Label,
.3.1
4. Complex imaginary.
5. .
6.
, Start Debugging.
7. .
:
48

.3.1
:
Public Class Complex
Public Shadows Class imaginary
Private x As Single
Private y As Single
Public Sub New(ByRef x As Single, ByRef y As Single)
Me.x = x
Me.y = y
End Sub
Public Overloads Function ToString() As String
If Me.y >= 0 Then
Return "(" & Me.x & "+ " & " i " & Me.y & ")"
Else
Return "(" & Me.x & "- " & " i " & Math.Abs(Me.y) & " )"
End If
End Function
Public Shared Operator +(ByVal z1 As imaginary, ByVal z2 As imaginary) As imaginary
Return New imaginary(z1.x + z2.x, z1.y + z2.y)
End Operator
End Class
Public a As Single = Val(InputBox("
"))
Public b As Single = Val(InputBox(" "))
Dim p1 As New imaginary(a, b)
49

Public c As Single = Val(InputBox("
"))
Public d As Single = Val(InputBox(" "))
Dim p2 As New imaginary(c, d)
Dim p3 As imaginary = p1 + p2 '
Dim s1 As String = " : " & p1.ToString() & " + " & p2.ToString() & " = " &
p3.ToString()
Public ReadOnly Property s() As String
Get
Return s1
End Get
End Property
End Class
:
Public Class Form1
Private Sub ToolStripMenuItem_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles
ToolStripMenuItem.Click
Dim addition As Complex = New Complex
MsgBox(addition.s)
End Sub
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles ToolStripMenuItem.Click
End
End Sub
End Class
:
50
Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]
