scala中抽象类_Scala中的抽象类
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 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 in
herited class.
也可以在抽象类中创建构造函数,该抽象类将由继承的类的实例调⽤。
scala中抽象类
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论