scala中抽象类_Scala中的抽象类

本文介绍了Scala编程中抽象类的概念和用法。抽象类通过`abstract`关键字定义,可包含抽象和非抽象方法,不允许多重继承。创建抽象类实例是不允许的,但其字段可以被子类使用。此外,抽象类可以有构造函数供子类实例调用。

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中抽象类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值