1、代码
package com.yy.enhance
/**
* Type:抽象类型
* 抽象类型使用Type关键字定义,在子类中被确定类型
*/
//抽象类
abstract class Animal1{
//使用Type定义抽象类型
type myType
//方法的返回值声明为抽象类型
def myfunc():myType
}
//子类Tiger
class Tiger extends Animal1{
//抽象类型具体化
type myType=String
def myfunc()="this is a str"
}
//子类Cat
class Cat extends Animal1{
//抽象类型具体化
type myType=Int
def myfunc()=25
}
//入口
object AbstractTypeTest extends App {
println(new Tiger().myfunc)
println(new Cat().myfunc)
}
2、结果
this is a str
25