Scala继承:为什么我必须将我的类定义为抽象?

时间:2017-08-16 12:59:07

标签: scala class inheritance

class inheritance
{ 
  def a()
  println("Version 1")
}

class inheritance1 
{
  def b()
  println("version 2")
}

class inheritance2 extends inheritance 
{
  def c()
  println("version 3")
}

class inheritance3 extends inheritance2 
{
  def d()
  println("version 4")
}

object inherited 
{
  def main(args: Array[String])
  {
    var obj = new inheritance3
    obj.d
    obj.c 
  }
}

它引发了一些错误:

class inheritance needs to be abstract since method a is not defined 

class inheritance1 needs to be abstract since method b is not defined 

class inheritance2 needs to be abstract since it has two unimplemented members 

class inheritance3 needs to be abstract since it has two unimplemented members 

1 个答案:

答案 0 :(得分:1)

这样的东西
def a()
println("foo")

声明一个未定义的方法a,它不接受任何参数并返回Nothing然后打印出一个字符串。

def a() = println("foo")

def a() {
  println("foo")
}

def a() = 
  println("foo")

定义方法a,打印出一个字符串并返回Unit