标签:http 依赖 object 规则 插入 调用 ESS 而且 text
class Person
class Student extends Person
val p: Person = new Student
p match { //模式匹配
case per: Person => println("it's Person's object")
case _ => println("unknown type")
}
// 运行后将会出现 “it's Person's object” 的字样
class Person {
protected var name: String = "leo"
protected[this] var hobby: String = "game"
}
class Student extends Person {
def sayHello = println("Hello, " + name)
//无法访问到 hobby 变量,因为被 protected[this] 所保护
def makeFriends(s: Student) {
println("my hobby is " + hobby + ", your hobby is " + s.hobby)
}
}
class Person(val name: String, val age: Int)
//在主 constructor 中调用父类的 constructor
class Student(name: String, age: Int, var score: Double) extends Person(name, age) {
def this(name: String) {
this(name, 0, 0)
}
def this(age: Int) {
this("0mifang", age, 0)
}
}
匿名子类,也就是说,可以定义一个类的没有名称的子类,并直接创建其对象,然后将对象的引用赋予一个变量。之后甚至可以将该匿名子类的对象传递给其他函数。
class Person(protected val name: String) {
def sayHello = "Hello, I'm " + name
}
val p = new Person("0mifang") { //匿名内部类
override def sayHello = "Hi, I'm " + name
}
def greeting(p: Person { def sayHello: String }) { //使用匿名内部类作为参数
println(p.sayHello)
}
abstract class Person(val name: String) {
def sayHello: Unit //无具体实现
}
class Student(name: String) extends Person(name) {
def sayHello: Unit = println("Hello, " + name) //覆盖并做出具体实现
}
abstract class Person {
val name: String //无初始值
}
class Student extends Person {
val name: String = "leo" //覆盖并给出初始值
}
欢迎关注,本号将持续分享本人在编程路上的各种见闻。
标签:http 依赖 object 规则 插入 调用 ESS 而且 text
原文地址:https://www.cnblogs.com/Alex458/p/12237493.html