VBS封装实例

问题:为完成某个班级同学信息的统计工作,需要将学生抽象为某一类,然后用一个一位数组保存这些学生的信息,并根据要求输出学生信息(语文成绩,计算机成绩,英语成绩,姓名,性别,学号):

         将学生的平均成绩由高到低进行输出

         将输出所有男生姓名

         输出所有女生学号

         获取平均成绩

这里假设全班10名同学

代码如下:

Option explicit
'定义课程类
Class course
	   Private cName  '姓名
	   Private cScore    '成绩
	'Property Get,Property Let,Property Set分别为读值,赋值,和对象类型赋值。
	  Public property get courseName
		 courseName =cName  '读值且有返回值
	  End Property
	  
		Public property let courseName( vName)
		 cName =vName   '变量赋值无返回值
	  End Property
	
	'课程成绩只读性,注意其赋值的过程
		Public property get courseScore
		 courseScore =cScore '读值
	  End Property
	  
		Public property let courseScore( vs)
		 cScore =vs     '变量赋值
	  End Property
End Class

'定义学生类
Class student
	Private sName '学生姓名
	Private sgender '学生性别
	Private sNum  '学号
	Private lstCourses  '课程
   'Property Get,Property Let,Property Set分别为读值,赋值,和对象类型赋值。

   Public property get averScore
			Dim avertmp,ln,aver,i '定义变量总分,课程数,平均基数
			avertmp=0  
			ln = UBound(lstCourses)  'UBOUND 函数返回一个数组的最大下标,数据类型为Long
			For i =0 to ln-1
				avertmp= avertmp+ lstCourses(i).courseScore
			Next
			If ln>1 Then
			  aver= ln
			 else
			   aver=1
			End If
			averScore= avertmp/aver
   End Property

   Public property get studName
	  studName= sName    '读值
   End Property
   
   Public property let studName(sv)
	  sName=sv       '赋值
   End Property

	  Public property get studNum
	  studNum= sNum   '读值
   End Property
   
   Public property let studNum(sv)
	  sNum=sv     '赋值
   End Property

	Public property get studGender
	  studGender= sgender    '读值
   End Property
   
   Public property let studGender(sv)
	  sgender=sv     '赋值
   End Property
   
   Public sub setSelcCourses(sv)   ''声明 Sub 过程的名称、参数以及构成其主体的代码。
	  Dim ln,i
	   ln = UBound(sv)
	  set lstCourses=nothing
	  redim lstCourses(ln)  'ReDim 语句在过程级别中使用,用于为动态数组变量重新分配存储空间。
	  For i=0 to ln-1
	   set lstCourses(i)= sv(i)  '对象类型赋值
	  Next
   End sub
		   
End Class


'测试代码 测试样例仅仅针对某一个同学的信息处理,如果针对全班所有同学,请完善相应的代码
Dim a,j,e,f,g,bb,i,c,m,nn,ss,ni,si
Dim n,x,s
Dim ac(3,10) 
Dim b(10),b2(10)
Dim d(3)
Dim n1(),s1()
ReDim n1(10),s1(10)
c = ""
nn = ""
ss = ""
e=Array(91,89,88,78,85,64,92,85,90,75)  '十位同学的语文成绩
f=Array(91,89,88,78,85,64,92,85,90,75)    '十位同学的数学成绩
g=Array(91,89,88,78,85,64,92,85,90,75)   ' 十位同学的计算机成绩
n = Array("n1","n2","n3","n4","n5","n6","n7","n8","n9","n10")' 十位同学的姓名
x = Array("male","male","fmale","male","male","fmale","fmale","male","male","male")
s = Array("1110","1111","1112","1113","1114","1115","1116","1117","1118","1119")' 十位同学的学号
For  j =0 to 9
      set b(j)= new course  
Next
For j =0 to 9
    b(j).courseName ="Chinese"
	b(j).courseScore = e(j)
Next
For j =0 to 9
     set ac(0,j) = b(j)
    set b(j) = nothing '释放b(对象)占用的内存 
Next

For  j =0 to 9
      set b(j)= new course
Next
For j =0 to 9
    b(j).courseName ="math"
	b(j).courseScore = f(j)
Next
For j =0 to 9
     set ac(1,j) = b(j)
	set b(j) = nothing '释放b(对象)占用的内存 
Next

For  j =0 to 9
      set b(j)= new course
Next
For j =0 to 9
    b(j).courseName ="computer"
	b(j).courseScore = g(j)
Next
For j =0 to 9
     set ac(2,j) = b(j)
	 set b(j) = nothing '释放b(对象)占用的内存 
Next

 For j =0 to 9
      Set b2(j) = new student
 Next
ni =0
si = 0
For j =0 to 9
     b2(j).studName = n(j)
     b2(j).studNum = s(j)
     b2(j).studGender = x(j)
	  If  b2(j).studGender = "male"Then
	     n1(ni) = b2(j).studName
	     ni = ni+1
	 else
	     s1(si) =s(j)
	    si=si+1
	End If  
	Set n(j) = nothing
	Set s(j) = nothing
	Set b2(j)=nothing
Next

 Set bb=new student
For j = 0 to 9 
	For  i = 0 to 2
		 Set d(i)= new course
		set d(i) = ac(i,j)
	Next
	bb.setSelcCourses(d) 
   b(j) =  bb.averScore 
   For i = 0 to  2
		 set d(i) = nothing '释放d(对象)占用的内存 
   Next
 Next	
	'b.studName& b.studNum&  b.studGender&b.averScore& vbcrlf 

'排序
For i = 1 to 9
	For j =1 to (10-i)
		If b(j)<b(j+1) Then
			m =b(j) 
			b(j) = b(j+1) 
			b(j+1)=m
		End If
     Next
Next
 For i=1 to 10
    c = c&"  " &b(i)
 Next 
 Dim length '用length表示数组的长度
length = UBound(n1) - LBound(n1) + 1
  For i=0 to length-1
    nn = nn&"   " &n1(i)
 Next 
 length = UBound(s1) - LBound(s1) +1
  For i=0 to length-1
    ss = ss&"  " &s1(i)
 Next 
msgbox  "男生姓名:" &nn& vbNewLine & "女生学号:"&ss& vbNewLine & "平均成绩:"&c
For j = 0 to 9 
      Set b(j)=nothing
Next

      以上代码基本实现问题要求的功能,但相较之下,由于循环及赋值过多运行时间较长,且输出结果未考虑生成文件形式,简单以提示框形式展现。

      如若出现不懂的地方,还请参照w3c手册进行先行理解(在下也是如此经历),如有更好的修改方案,还请评论区留下宝贵一笔。感谢观览

发布了17 篇原创文章 · 获赞 8 · 访问量 6769

猜你喜欢

转载自blog.csdn.net/qq_39800695/article/details/104097632
vbs