中的操作符應計劃
任何一門語言都提供了豐富的操作符,vb也不例外。操作符一般分為:算術運算符,賦值運算符,比較運算符,邏輯運算符,其它。本文只以幾個小例子來介紹其中一些比較重要的應用。
在本文中將會用到一個自定義類 dog ,代碼如下:
Public Class dogClass dog
Private s_name As String
Private s_color As Color
Private s_age As Integer
Public Sub New()Sub New()
s_name = \"塞北的雪\"
s_age = 22
s_color = d
End Sub
Public Property Name()Property Name() As String
Get
Return s_name
End Get
Set(ByVal value As String)
s_name = value
End Set
End Property
Public Property Color()Property Color() As Color
Get
Return s_color
End Get
Set(ByVal value As Color)
s_color = value
End Set
End Property
Public Property Age()Property Age() As Integer
Get
Return s_age
End Get
Set(ByVal value As Integer)
s_age = value
End Set
End Property
Public Shared Operator +(ByVal a As dog, ByVal b As dog)
Dim c As New dog
= +
e = e + e
lor = omArgb((Argb + Argb) / 2)
Return c
End Operator
End Class
1、Is 運算符
Is運算符比較兩個引用對象是否引用了同一實例。 例子如下:
Dim a As New dog
Dim b As New dog()
If a Is b Then
MsgBox(\"True\")
Else
MsgBox(\"False\")
End If
b = a
= \"NorthSnow\"
If a Is b Then
MsgBox(\"True\")
Else
MsgBox(\"false\")
End If
\'false
\'true
2、操作符重載
我們可以根據需要對操作符進行重載,本文中的 dog 類 就 對 + 操作符 進行了重載 操作。此時對兩個dog 類進行相加時,會返回一個dog 類實例 ,他的 Name 是 那兩個 dog 實例的 名字相加,年齡也是 dog 實例的年齡之和,顏色是兩個顏色的平均值。例子如下:
Dim sb As New ringBuilder
Dim m As New dog
Dim n As New dog
Dim c As dog
= \"NorthSnow\"
c = m + n
pendLine()
pend()
pend(\"--\")
pend(e)
pend(\"--\")
pend(Argb)
pendLine()
pend()
pend(\"--\")
pend(e)
pend(\"--\")
pend(Argb)
pendLine()
pend()
pend(\"--\")
pend(e)
pend(\"--\")
pend(Argb)
MsgBox(String)
\'NorthSnow--22---65536
\'塞北的雪--22---65536
\'NorthSnow塞北的雪--44---655363、like操作符
like 操作符用于判斷一個字符串與另外一個字符串是否匹配,大小寫敏感。語法是:
Result=String like Pattern
例子如下:
Dim a As String = \"Northsnow,塞北的雪\"
Dim b As String = \"Northsnow,塞北的雪\"
If a Like b Then
MsgBox(\"true\")
Else
MsgBox(\"false\")
End If
b = \"northsnow,塞北的雪\"
If a Like b Then
MsgBox(\"true\")
Else
MsgBox(\"false\")
End If
\'true
\'false
另外,Pattern支持通配符。這一點肯定讓我們想起了正則表達式(Regular Expression)。不過我們千萬不要搞混了,雖然有些地方相近,但是有些通配符與正則表達式不太一樣,功能也有區別。like操作符支持的通配符共有5種:
? 表示一個任意字符(1個)
* 任意個任意字符(0-n個)
# 表示一個數字
[charlist] 表示 charlist 中的任意一個字符
[!charlist] 表示 不在charlist 中的任意一個字符
用過正則的朋友一定會很容易的理解的,有幾個核心的規則一定要記牢,那就是:
(1) * 可以匹配任何字符串
(2) ?可以匹配任何一個字符
(3) 而# 相當于[] 只能匹配一個數字 如果要匹配兩個數字需要寫成 ##
(4) 字符列表中的字符可以用-提供一個區間,以簡化書寫。
(5) 大小寫敏感
下面就舉幾個例子說明一下:
Dim a As String = \"Northsnow.123\"
Dim b As String = \"*.###\"
MsgBox(a Like b)
\'True
a = \"Northsnow.123\"
b = \"[A-Z][a-z]*#\"
MsgBox(a Like b)
\'True
a = \"Northsnow.123\"
b = \"[A-Z][a-z]*[a-z]\"
MsgBox(a Like b)
\'False
a = \"Northsnow.123\"
b = \"[A-Z][a-z]*[]\"
MsgBox(a Like b)
\'True
4、其它的一些特殊操作符
AndAlse ,OrElse 與 and 和 or 差不多,只是他們比 后者更智能,當前面的判斷已經確定了返回值的時候,后面的表達式的值就不再計算了。
TypeOf 返回當前對象的類型,每一個類都有一個getType方法與它很相似,據一個小例子如下:
Dim a As New dog
Dim b As New dog
Dim c As Type
c = tType
MsgBox(String)
音頻一元一條 If TypeOf (a) Is g Then
MsgBox(True)
End If
\'rm7+dog
\'True
就說這么多吧。已有在應用中如果在總結出什么好的經驗,再慢慢補充。
杭州醫院哪婦科好石家莊牛皮癬醫院
貴陽前列腺炎