scala中抽象類
抽象類 (Abstract Class)
In the Scala programming language, abstraction is achieved using abstract class.
在Scala編程語言, 抽象是使用抽象類來實現的。
Abstraction is the process of showing only functionality and hiding the details from the final user.
抽象是僅顯示功能并向最終用戶隱藏細節的過程。
Abstract classes are defined using the "abstract" keyword. An abstract class contains both abstract and non-abstract methods. Multiple inheritances are not allowed by abstract class i.e. only one abstract class can be inherited by a class.
抽象類使用“抽象”關鍵字定義。 抽象類包含抽象方法和非抽象方法。 抽象類不允許多重繼承,即一個類只能繼承一個抽象類。
Syntax to create Abstract Class in Scala:
在Scala中創建Abstract類的語法:
abstract class class_name {
def abstract_method () {}
def method() {
//code
}
}
An abstract method is that method which does not have any function body.
抽象方法是沒有任何函數體的方法。
Example:
例:
abstract class bikes
{
def displayDetails()
}
class myBike extends bikes
{
def displayDetails()
{
println("My new bike name : Harley Davidson Iron 833 ")
println("Top speed : 192 kmph")
}
}
object MyObject
{
def main(args: Array[String])
{
var newBike = new myBike()
newBike.displayDetails()
}
}
Output
輸出量
My new bike name : Harley Davidson Iron 833
Top speed : 192 kmph
Some Points about Abstract Classes in Scala
關于Scala抽象類的幾點
Instance creation of Abstract class is not allowed. If we try to create objects of abstract class then an error will be thrown.
不允許創建Abstract類的實例。 如果我們嘗試創建抽象類的對象,則將引發錯誤。
Field creation of an abstract class is allowed and can be used by methods of abstract class and classes that inherit it.
允許抽象類的字段創建,并且抽象類的方法和繼承它的類都可以使用該字段。
A constructor can also be created in an abstract class which will be invoked by the instance of the inherited class.
也可以在抽象類中創建構造函數,該抽象類將由繼承的類的實例調用。
翻譯自: https://www.includehelp.com/scala/abstract-classes.aspx
scala中抽象類