Scala--嵌套类

yunfenglee 2019-11-08

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)    }  }}

Scala--嵌套类

相关推荐