1、特质Worker与特质Teacher中具有相同方法work,其中Teacher.work是具体方法,而Worker.work是抽象方法
// 类体内容,执行类构造器时执行类体内容
class Person{
// 类体内容
println("class: Person")
}
trait Worker extends Person{
// 类体内容
println("class: Worker")
// 抽象方法
def work
}
trait Teacher extends Person{
// 类体内容
println("class: Teacher")
// 具体方法
def teaching(): Unit ={
println("class: Teacher, def: teaching")
}
// 具体方法
def work: Unit ={
println("class: Teacher, def: work")
}
}
class MathTeacher extends Person with Teacher with Worker{
// 类体内容
println("class: MathTeacher")
}
object Person {
def main(args: Array[String]): Unit = {
val person = new MathTeacher
println(person.work)
/**
* 运行结果:
* class: Person
* class: Teacher
* class: Worker
* class: MathTeacher
* class: Teacher, def: work
* ()
*
* 解析:
* 对于MathTeacher extends Person with Teacher with Worker,
* 可知构造器顺序为:Person -> Teacher -> Worker -> 对于MathTeacher
*
* 由于Worker中的work是抽象类型,所以person.work执行的是Teacher中的work方法,
* person.work返回Unit, println语句,则打印()
* 在scala中,void由Unit类型表示,该类型只有一个值,那就是()。 ——来自快学scala第二版 P114
*/
}
}
2、特质Worker与特质Teacher中具有相同方法work,两个work方法都是是具体方法
// 类体内容,执行类构造器时执行类体内容
class Person{
// 类体内容
println("class: Person")
}
trait Worker extends Person{
// 类体内容
println("class: Worker")
// 具体方法
def work: Unit ={
println("class: Worker, def: work")
}
}
trait Teacher extends Person{
// 类体内容
println("class: Teacher")
// 具体方法
def teaching(): Unit ={
println("class: Teacher, def: teaching")
}
// 具体方法
def work: Unit ={
println("class: Teacher, def: work")
}
}
class MathTeacher extends Person with Teacher with Worker{
// 类体内容
println("class: MathTeacher")
}
object Person {
def main(args: Array[String]): Unit = {
val person = new MathTeacher
println(person.work)
/**
* Error:(34, 7) class MathTeacher inherits conflicting members:
* method work in trait Teacher of type => Unit and
* method work in trait Worker of type => Unit
* (note: this can be resolved by declaring an 'override' in class MathTeacher.)
* class MathTeacher extends Person with Teacher with Worker{
**/
}
}
按上面运行结果错误指示修改,在MathTeacher中,重写继承的的work方法即可:
// 类体内容,执行类构造器时执行类体内容
class Person{
// 类体内容
println("class: Person")
}
trait Worker extends Person{
// 类体内容
println("class: Worker")
// 具体方法
def work: Unit ={
println("class: Worker, def: work")
}
}
trait Teacher extends Person{
// 类体内容
println("class: Teacher")
// 具体方法
def teaching(): Unit ={
println("class: Teacher, def: teaching")
}
// 具体方法
def work: Unit ={
println("class: Teacher, def: work")
}
}
class MathTeacher extends Person with Teacher with Worker{
// 类体内容
println("class: MathTeacher")
override def work = println("class: MathTeacher, def: work")
}
object Person {
def main(args: Array[String]): Unit = {
val person = new MathTeacher
person.work
/**
* 运行结果:
* class: Person
* class: Teacher
* class: Worker
* class: MathTeacher
* class: MathTeacher, def: work
*/
}
}
3、特质Worker有抽象方法work,Teacher中没有work方法
// 类体内容,执行类构造器时执行类体内容
class Person{
// 类体内容
println("class: Person")
}
trait Worker extends Person {
// 类体内容
println("class: Worker")
// 抽象方法
def work
}
trait Teacher extends Person{
// 类体内容
println("class: Teacher")
// 具体方法
def teaching(): Unit ={
println("class: Teacher, def: teaching")
}
}
class MathTeacher extends Person with Teacher with Worker{
// 类体内容
println("class: MathTeacher")
// 这里必须复写,因为继承的类中存有抽象方法work,没有实现方法work
override def work = println("class: MathTeacher, def: work")
}
object Person {
def main(args: Array[String]): Unit = {
val person = new MathTeacher
person.work
/**
* 运行结果:
* class: Person
* class: Teacher
* class: Worker
* class: MathTeacher
* class: MathTeacher, def: work
*/
}
}
4、匿名类对象,构造对象的构造器执行顺序与上面一致
// 类体内容,执行类构造器时执行类体内容
class Person{
// 类体内容
println("class: Person")
}
trait Worker extends Person {
// 类体内容
println("class: Worker")
// 抽象方法
def work
}
trait Teacher extends Person{
// 类体内容
println("class: Teacher")
// 具体方法
def teaching(): Unit ={
println("class: Teacher, def: teaching")
}
}
class MathTeacher extends Person with Teacher with Worker{
// 类体内容
println("class: MathTeacher")
// 这里必须复写,因为继承的类中存有抽象方法work,没有实现方法work
override def work = println("class: MathTeacher, def: work")
}
object Person {
def main(args: Array[String]): Unit = {
// 匿名类对象person
val person = new Person with Worker with Teacher{
override def work: Unit = println("Anonymous class, def: work")
}
person.work
/**
* new Person with Worker with Teacher 构造器执行顺序:
* Person -> Worker -> Teacher
* 运行结果:
* class: Person
* class: Worker
* class: Teacher
* Anonymous class, def: work
*/
}
}
5、Scala中的trait与class继承实现AOP 【特别注意】
trait Action{
def doAction
}
trait TBeforeAfter extends Action{
/*注意 doAction虽然被复写 但仍然是abstract方法
因为super.doAction并未实现 所以用abstract关键字标识*/
abstract override def doAction {
println("Initialization")
super.doAction
println("Destroyer")
}
}
class Work extends Action{
override def doAction = println("Working...")
}
object AOP {
def main(args: Array[String]): Unit = {
val w = new Work with TBeforeAfter
//这里work实现了doAction,所以成功执行。
w.doAction
/**
* 运行结果:
* Initialization
* Working...
* Destroyer
*/
}
}