博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala中抽象类和特质_Scala中特质与抽象类的区别
阅读量:2529 次
发布时间:2019-05-11

本文共 2185 字,大约阅读时间需要 7 分钟。

scala中抽象类和特质

Scala中的抽象类 (Abstract Class in Scala)

Abstract Class in Scala is created using the abstract keyword. Both abstract and non-abstract methods are included in an abstract class.

Scala中的Abstract类是使用abstract关键字创建的。 抽象类中包含抽象方法和非抽象方法。

Multiple inheritances are not supported by them.

它们不支持多重继承。

Syntax:

句法:

abstract class class_name{
def abstract_mathod def general_method(){
}}

Example:

例:

abstract class bike {
def speed def display() {
println("This is my new Bike") ; }} class ninja400 extends bike {
def speed() {
println("Top Speed is 178 Kmph"); } } object myObject {
def main(args: Array[String]) {
var obj = new ninja400(); obj.display() ; obj.speed() ; } }

Output

输出量

This is my new BikeTop Speed is 178 Kmph

Scala的特质 (Traits in Scala)

Traits in Scala are created using trait keyword. Traits contain both abstract and non-abstract methods and fields. These are similar to interfaces in java. They allow multiple inheritances also and they are more powerful as compared to their successors in java.

Scala中的特征是使用trait关键字创建的。 特性包含抽象和非抽象的方法和字段。 这些类似于Java中的接口。 它们还允许多重继承,并且与Java中的继承者相比,它们更强大。

Syntax:

句法:

trait trait_name{
}

Example:

例:

trait bike {
def speed def display() {
println("This is my new Bike") ; } } class ninja400 extends bike {
def speed() {
println("Top Speed is 178 Kmph"); } } object myObject {
def main(args: Array[String]) {
var obj = new ninja400(); obj.display() ; obj.speed() ; } }

Output

输出量

This is my new BikeTop Speed is 178 Kmph

抽象类和特征之间的区别 (Difference between abstract class and traits)

Traits Abstract Class
Allow multiple inheritances. Do not Allow multiple inheritances.
Constructor parameters are not allowed in Trait. Constructor parameter are allowed in Abstract Class.
The Code of traits is interoperable until it is implemented. The code of abstract class is fully interoperable.
Traits can be added to an object instance in Scala. Abstract classes cannot be added to object instance in Scala.
特质 抽象类
允许多个继承。 不允许多重继承。
特性中不允许使用构造函数参数。 抽象类中允许使用构造函数参数。
特质代码在实施之前是可以互操作的。 抽象类的代码是完全可互操作的。
可以将特性添加到Scala中的对象实例。 无法将抽象类添加到Scala中的对象实例。

翻译自:

scala中抽象类和特质

转载地址:http://kwvzd.baihongyu.com/

你可能感兴趣的文章
第一次接触安卓--记于2015.8.21
查看>>
(转)在分层架构下寻找java web漏洞
查看>>
C++ ifstream ofstream
查看>>
跟初学者学习IbatisNet第四篇
查看>>
seL4环境配置
查看>>
Git报错:insufficient permission for adding an object to repository database .git/objects
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>