2017-08-16 70 views
1
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 
+0

定義的方法呢​​? – Dima

+0

感謝您的答覆,但請準確 –

回答

1

喜歡的東西

def a() 
println("foo") 

聲明一個未定義的方法a,它沒有任何參數和返回Nothing,然後打印出一個字符串。

def a() = println("foo") 

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

def a() = 
    println("foo") 

定義的方法a,打印出的字符串並返回一個Unit

+0

感謝您的答覆很多迪馬,但它仍然是同樣的錯誤 –

+0

@ShellyVerma不,事實並非如此。 'class inheritance {def a()= println(「Version 1」)}'不會給出任何錯誤。 – Dima

+0

對不起迪馬麻煩你,但現在沒有拋出錯誤,因爲我重新寫的代碼。再次感謝:)))) –