scala中抽象类_Scala中的抽象类
scala中抽象類
抽象類 (Abstract Class)
In the Scala programming language, abstraction is achieved using abstract class.
在Scala編程語(yǔ)言, 抽象是使用抽象類來(lái)實(shí)現(xiàn)的。
Abstraction is the process of showing only functionality and hiding the details from the final user.
抽象是僅顯示功能并向最終用戶隱藏細(xì)節(jié)的過(guò)程。
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.
抽象類使用“抽象”關(guān)鍵字定義。 抽象類包含抽象方法和非抽象方法。 抽象類不允許多重繼承,即一個(gè)類只能繼承一個(gè)抽象類。
Syntax to create Abstract Class in Scala:
在Scala中創(chuàng)建Abstract類的語(yǔ)法:
abstract class class_name {def abstract_method () {}def method() {//code} }An abstract method is that method which does not have any function body.
抽象方法是沒(méi)有任何函數(shù)體的方法。
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 kmphSome Points about Abstract Classes in Scala
關(guān)于Scala抽象類的幾點(diǎn)
Instance creation of Abstract class is not allowed. If we try to create objects of abstract class then an error will be thrown.
不允許創(chuàng)建Abstract類的實(shí)例。 如果我們嘗試創(chuàng)建抽象類的對(duì)象,則將引發(fā)錯(cuò)誤。
Field creation of an abstract class is allowed and can be used by methods of abstract class and classes that inherit it.
允許抽象類的字段創(chuàng)建,并且抽象類的方法和繼承它的類都可以使用該字段。
A constructor can also be created in an abstract class which will be invoked by the instance of the inherited class.
也可以在抽象類中創(chuàng)建構(gòu)造函數(shù),該抽象類將由繼承的類的實(shí)例調(diào)用。
翻譯自: https://www.includehelp.com/scala/abstract-classes.aspx
scala中抽象類
總結(jié)
以上是生活随笔為你收集整理的scala中抽象类_Scala中的抽象类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: “如令圣纳贤”上一句是什么
- 下一篇: c语言语言教程0基础_C语言基础