Scala是一个多范式的编程语言(支持多种方式的编程)
定义一个学生类
class Student {
//定义学生的学号
private[this] var studentID : Int = 0;
//定义学生的姓名
private[this] var studentName : String = "";
//定义set get方法
def setStudentID(id : Int) = {this.studentID = id}
def getStudentID() : Int = studentID
def setStudentName(name : String) = {this.studentName = name}
def getStudentName() : String = studentName
}
/*
*
* @Attention object和class的名字可以不一样
* 如果一样,这个object就叫做class的伴生对象
*
* */
object Student {
def main(args: Array[String]): Unit = {
//实例化Student对象
var student = new Student
student.setStudentID(1)
student.setStudentName("Freedom")
println(student.getStudentID()+"\t"+student.getStudentName())
}
}
定义一个工人类(继承人员)
class Person (val name : String){
def say() : String = {
"Hello "+name
}
}
//创建子类
class Employee(override val name : String, val age : Int) extends Person (name){
//重写父类方法
override def say(): String = {
"Hello "+name+" ,his age is "+age
}
}
object Demo extends App {
//实例化父类对象
var person = new Person("Destiny")
println(person.say())
//实例化子类对象
var person1 = new Employee("Freedom",18)
println(person1.say())
//匿名子类
var person2 = new Person("Fate"){
override def say(): String = "hello " + name
}
println(person2.say())
}
为什么可以访问私有成员?
当属性是private的时候,Scala会自动为其生成对应的set get方法。student.studentID,实际上调用了get方法
如果只希望生成get方法而不生成set方法的时候,可以定义属性为常量
如果希望属性不能被外部访问,使用private[this]关键字
在类的内部,定义另外一个类
定义一个人员类
import scala.collection.mutable.ArrayBuffer
class Person {
//定义人的姓名和年龄
private[this] var name : String = ""
private[this] var age : Int = 0
private var courseList = new ArrayBuffer[Course]()
//定义set get方法
def setName(name : String) = {this.name = name}
def getName() : String = name
def setAge(age : Int) = {this.age = age}
def getAge() : Int = age
/*
* 添加课程
* */
def addCourse(courseName : String,grade : Int): Unit ={
var course = new Course(courseName,grade)
courseList += course
}
class Course(var courseName : String,var grade : Int){}
}
object Person{
def main(args: Array[String]): Unit = {
var person = new Person
//给人添加课程信息
person.addCourse("语文",107)
person.addCourse("数学",120)
person.addCourse("英语",110)
//课程的迭代
for (c <- person.courseList)println(c.courseName+"\t"+c.grade)
}
}
1、主构造器:和类的声明在一起,并且一个类智能有一个主构造器
2、辅助构造器:一个类可以有多个辅助构造器,通过this来实现
class Student(var studentID : Int,var studentName : String) {
/*
*
* 辅助构造器就是一个函数,只不过函数的名字为this
*
*
* */
def this(studentName : String){
this("no studentID",studentName)
println("这是一个辅助构造器")
}
}
object Student {
def main(args: Array[String]): Unit = {
//使用主构造器实例化Student对象
var student = new Student(1,"Freedom")
println(student.studentID+"\t"+student.studentName)
//使用辅助构造器实例化Student对象
var student = new Student("Destiny")
println(student.studentID+"\t"+student.studentName)
}
}
abstract class Person{
val name : String
val age : Int
}
class Employee extends Person {
val name : String = "Freedom"
val age : Int = 18
}
class Teacher(val name : String,val age : Int) extends Person{}
object Demo {
}
特质可以实现多重继承
trait Car{
val name : String
val id : Int
}
trait Action{
def drive() : String
}
class Bike(val name : String,val id : Int) extends Car with Action{
override def drive(): String = "Bike's name is "+name+", the bike's number is "+id
}
object Demo3 {
def main(args: Array[String]): Unit = {
var bike = new Bike("宝马",8888)
println(bike.drive())
}
}