Scala--嵌套类
1、scala的嵌套类也成为内部类
package com.itstar.scala.testimport scala.collection.mutable.ArrayBufferclass Student2 { private var stuName:String = "Tom" private var stuAge:Int = 20 //定义一个容纳课程的数组 private var courseList = new ArrayBuffer[course]() //声明一个增加课程的方法 def addCourse(cname:String,cgrade:Int) = { var c = new course(cname,cgrade) courseList += c } //定义课程类,使用主构造器,就是把属性写在类名后面 //var courseName:String,var grade:Int等同于private var xxx class course(var courseName:String,var grade:Int){}}object Student2{ def main(args: Array[String]): Unit = { var s1 = new Student2 s1.addCourse("语文",90) s1.addCourse("数学",100) s1.addCourse("英语",55) println("姓名:" + s1.stuName + "\t" + "年龄:" + s1.stuAge) println("=============课程成绩=============") for(s <- s1.courseList){ println(s.courseName+"\t"+s.grade) } }}
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25