国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

VB.Net的ByVal和ByRef --ByVal時(shí)的淺拷貝和深拷貝

2024-07-10 13:01:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
初學(xué)vb.net ,總結(jié)一下byval和byref

1 通過(guò)byval傳遞的變量,vb.net會(huì)復(fù)制與源值相等的一個(gè)新的變量。而byref則相當(dāng)于引用。
例如我們學(xué)習(xí)c的時(shí)候得swap()函數(shù)


imports system
'test that can't swap a and b
class myapp
public shared sub main()
dim a as integer = 0
dim b as integer = 1
console.writeline("source: a" & a & "b"& b)
fakeswap(a,b) ' after this fakeswap(a,b), a still is 0 and b still is 1
console.writeline("after fakeswap: a" & a & "b"& b)
swap(a,b) ' after this swap(a,b), a is 1 and b is 0
console.writeline("after swap: a" & a & "b"& b)
end sub

' fake swap function:fakeswap()
shared sub fakeswap(byval ina as integer, byval inb as integer)
dim tmp as integer
tmp = ina
ina = inb
inb = tmp
end sub
' real swap function :swap()
shared sub swap(byref ina as integer, byref inb as integer)
dim tmp as integer
tmp = ina
ina = inb
inb = tmp
end sub
end class




2 注意的是: 如果byval傳遞的是自定義的類的一個(gè)實(shí)例,被復(fù)制的只是該實(shí)例的引用,引用所指向的資源并沒(méi)有被復(fù)制。--相當(dāng)于c++中的淺拷貝。


imports system

' 類a的實(shí)例mya作為函數(shù) testa(byval ina as a)的參數(shù),結(jié)果應(yīng)該是
' --按值傳遞為淺拷貝,只是復(fù)制了一份引用--a的實(shí)例mya和 ina共享一個(gè)資源
class myapp
public shared sub main()
dim mya as a
console.writeline("the original resource of mya is: " & mya.resource)
' call testa()
testa(mya)
console.writeline("after call the byval fun , the resource of mya is: " & mya.resource)
end sub
' 函數(shù)testa() 將mya按值傳遞進(jìn)去為ina 修改ina的resource ,實(shí)際上修改的也是mya的resource
shared sub testa(byval ina as a)
ina.resource = 1
end sub
end class

' 類a 有資源 resource (integer)
class a
public resource as integer = 0
end class




3 如果想實(shí)現(xiàn)類的實(shí)例(不是引用)的“按值“傳遞(深拷貝),則必須overridde clone()方法 ?還是專門(mén)有拷貝構(gòu)造函數(shù)?
方法一:
<serializable>_
class abc
xxx
end class

然后用memorystream和binaryformatter(streamcontext要用file類型的),這樣絕對(duì)是深拷貝。但是如何實(shí)現(xiàn)c++中的“拷貝構(gòu)造”呢?
待續(xù)...


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 文昌市| 北辰区| 瓦房店市| 商河县| 离岛区| 沐川县| 合山市| 汪清县| 平武县| 东乌珠穆沁旗| 扎鲁特旗| 都江堰市| 吕梁市| 奉贤区| 郓城县| 宣恩县| 河源市| 陆丰市| 镶黄旗| 乌苏市| 红河县| 治县。| 东明县| 吉木乃县| 凤台县| 柏乡县| 永州市| 喀喇沁旗| 宜都市| 蓝田县| 锡林郭勒盟| 霞浦县| 盐津县| 临夏县| 蒙阴县| 陇川县| 甘谷县| 苍溪县| 安溪县| 静海县| 阆中市|