Private Sub Command1_Click()Dim total As Integertotal = s(1) + s(2)Print totalEnd SubPrivate Function s(m As Integer) As IntegerStatic x As IntegerFor i = 1 To mx = x + 1Next is = xEnd Function单击Command1三次的结果
来源:学生作业帮助网 编辑:六六作业网 时间:2025/02/07 03:12:51
Private Sub Command1_Click()Dim total As Integertotal = s(1) + s(2)Print totalEnd SubPrivate Function s(m As Integer) As IntegerStatic x As IntegerFor i = 1 To mx = x + 1Next is = xEnd Function单击Command1三次的结果
Private Sub Command1_Click()
Dim total As Integer
total = s(1) + s(2)
Print total
End Sub
Private Function s(m As Integer) As Integer
Static x As Integer
For i = 1 To m
x = x + 1
Next i
s = x
End Function
单击Command1三次的结果
Private Sub Command1_Click()Dim total As Integertotal = s(1) + s(2)Print totalEnd SubPrivate Function s(m As Integer) As IntegerStatic x As IntegerFor i = 1 To mx = x + 1Next is = xEnd Function单击Command1三次的结果
第一次单击时,进入s,定义一次x(以后都不会再重新定义,就是说Static使得x在程序一运行时就有了,三次单击都用的是同一个x),s(1)=0+1=1 (x=1),s(2)=(x+1)+1=3 (x=3),total=4;(s、x值为最后一次循环的结果值)
第二次单击时,进入s,用已存在的x(并没有定义一个新的x,x为上一次单击后的x值,即为3),s(1)=x+1=4 (x=4),s(2)=(x+1)+1=6 (x=6),total=10;
第二次单击时,进入s,s(1)=x+1=7 (x=7),s(2)=(x+1)+1=9 (x=9),total=16;
这主要就是理解Static的作用,静态变量全局运行就只有一个