scala 抽象方法

The type whose identity is not known precisely can be termed as abstract type.

身份未知的类型可以称为抽象类型

Scala抽象成员 (Scala Abstract Members)

A member is said to be abstract if the member does not have a complete definition in the class. These are implemented in the subclasses. For example;

如果成员在类中没有完整的定义,则该成员被称为抽象成员。 这些在子类中实现。 例如;

trait Student {type Xdef totalmarks(m1:X,m2:X):Xval age : X
}

Student trait declares a type X where the type of X is unknown. A method total marks returns type X and members marks1, marks2 are of type X. Here X is called as abstract type.

学生特征声明X类型,其中X类型未知。 方法total标记返回类型X,成员mark1,marks2类型为X。这里X称为抽象类型。

Now lets us implement the type X in a subclass.

现在让我们在子类中实现类型X。

class Adam extends Student {type X = Int def totalmarks(m1:Int , m2:Int) = m1 + m2val age = 7
}

We are creating class Adam that extends the student class and defines type X as Integer. The total marks method add the marks m1 and m2. The age variable is initialized to 7.

我们正在创建类Adam,该类扩展了学生类并将X类型定义为Integer。 总分数法将分数m1和m2相加。 age变量被初始化为7。

抽象值 (Abstract vals)

The abstract val takes the form as;

抽象值采用以下形式:

val name: String

The variable name and type is specified here but not the value of the variable name.

在此指定变量名称和类型,但不指定变量名称的值。

For example;

例如;

val name = "Reena"

The val declaration can be used in a class when you don’t know the correct value but we know that the value remains unchanged in each instance of the class.

当您不知道正确的值但可以知道该值在类的每个实例中保持不变时,可以在类中使用val声明。

An abstract val declaration constrains its legal implementation – def or val.

抽象的val声明限制了其合法执行-def或val。

For example;

例如;

abstract class Student {val value:Stringdef method:String
}

An abstract class Student id created with value variable and method with the keyword def.

使用值变量和关键字def创建方法的抽象类Student id。

abstract class Rob extends Student {val value:Stringval method:String
}

A class Rob is created extending Student and the method is defined with the keyword val which is allowed. The code will compile successfully.

将创建一个类Rob来扩展Student,并使用允许的关键字val定义该方法。 该代码将成功编译。

abstract class Micheal extends Student {def value: Stringdef method:String
}

A class Micheal is created extending the student class and the value and method is defined using the keyword def. This will throw an error as overriding is not allowed. Below image shows the above code execution in scala shell.

创建了Micheal类,扩展了学生类,并使用关键字def定义了值和方法。 这将引发错误,因为不允许覆盖。 下图显示了以上在scala shell中执行的代码。

抽象变量 (Abstract vars)

Abstract var defines a name and type but not value. Consider an example which defines two abstract variables – name and age as below.

抽象var定义名称和类型,但不定义值。 考虑一个示例,该示例定义了两个抽象变量–名称和年龄,如下所示。

trait Student {var name: Stringvar age: Int
}

The vars declared are equipped with getter and setter methods.

声明的var配有getter和setter方法。

trait Student {def name:String    //getter method for namedef name_=(n:String)  //setter method for namedef age:Int  //getter method for agedef age_=(a:Int) //setter method for age
}

Initializing abstract vals

初始化抽象值

Abstract vals allows us to provide the details in subclass when missed in superclass. For example;

当在超类中丢失时,抽象val允许我们在子类中提供详细信息。 例如;

trait student_percent {val obtained: Intval total: Intrequire(total != 0)val perc = (obtained % total) * 100override def toString = "Percentage secured is" + perc
}

Here the variables obtained and total are abstract vals and the values are not defined.

在这里,获得的变量和total是抽象值,并且未定义值。

scala> val scale = 5
scale: Int = 5

Here we declare variable scale and use them in the trait as below.

在这里,我们声明可变比例,并在以下特征中使用它们。

new student_percent {val obtained = 5 * scaleval total = 5 * scale
}
java.lang.IllegalArgumentException: requirement failedat scala.Predef$.require(Predef.scala:207)at student_percent$class.$init$(<console>:11)... 40 elided

The exception in this example was thrown because total still had its default value of 0 when trait student_percent was initialized, which caused the require invocation to fail.

抛出此示例异常是因为初始化特征student_percent时total仍具有其默认值0,这导致需求调用失败。

Pre-initialized fields

预初始化字段

Pre-initialized fields allows to initialize the fields of a subclass before the superclass is called. The field definition is placed in the braces before the superclass constructor call. For example create trait student_percent as;

预先初始化的字段允许在调用超类之前初始化子类的字段。 字段定义放在超类构造函数调用之前的花括号中。 例如,将特征student_percent创建为;

trait student_percent {val obtained: Intval total: Intrequire(total != 0)val perc = (obtained.toFloat / total.toFloat) * 100override def toString = "Percentage secured is" + perc
}

Now call the trait student_percent by initializing values for “obtained” and “total” as

现在,通过将“获得”和“总计”的值初始化为

new {
val obtained = 80
val total = 100
} with student_percent

Output:res22: student_percent = Percentage secured is80.0

输出:res22:student_percent =安全百分比为80.0

The fields can be initialized in object definition as;

这些字段可以在对象定义中初始化为:

object stud extends {val obtained = 70val total = 100
} with student_percent

Below image shows the above code execution in scala shell.

下图显示了以上在scala shell中执行的代码。

That’s all for abstract types in Scala programming, we will look into annotations in coming posts.

这就是Scala编程中抽象类型的全部内容,我们将在后续文章中研究注释。

翻译自: https://www.journaldev.com/8415/scala-abstract-types

scala 抽象方法

scala 抽象方法_Scala抽象类型相关推荐

  1. 冒号课堂§10.2:抽象类型

    冒号课堂 第十课 多态机制(2) 抽象类型--实中之虚 郑晖 摘要 介绍抽象类型的种类.意义及其用法 目录 !预览 ?提问 :讲解 ,插语 .总结 ""参考 有无相生,难易相成 - ...

  2. Scala类型系统——高级类类型(higher-kinded types)

    高级类类型就是使用其他类型构造成为一个新的类型,因此也称为 类型构造器(type constructors).它的语法和高阶函数(higher-order functions)相似,高阶函数就是将其它 ...

  3. Scala教程之:静态类型

    文章目录 泛类型 型变 协变 逆变 不变 类型上界 类型下界 内部类 抽象类型 复合类型 自类型 隐式参数 隐式转换 多态方法 类型推断 Scala是静态类型的,它拥有一个强大的类型系统,静态地强制以 ...

  4. scala 单元测试_Scala中的法律测试简介

    scala 单元测试 Property-based law testing is one of the most powerful tools in the scala ecosystem. In t ...

  5. scala上界_Scala方差,上界和下界

    scala上界 Variance refers as how subtyping between complex types relates to subtypes of their componen ...

  6. scala运算符_Scala运算符–算术,关系,逻辑,按位,赋值

    scala运算符 Today we will look into various types of scala operator. An operator tells the compiler to ...

  7. scala java抽象理解_scala – 抽象覆盖如何在Java代码方面起作用?

    它来了: 它简短,抽象覆盖def m()允许从方法调用super.m()并且当一个特征在所有其他类型之后混合时,这个超级调用动态地代替类型创建,例如 trait Printer { def print ...

  8. java scala 互操作_Scala类型边界和Java通用互操作

    嗯,你正在踩踏Scala中使用的Java泛型的方差问题.我们一步一步走吧. 我们来看看你的实现: // does not compile (with your original error) def ...

  9. scala 构造_Scala咖喱和自动类型依赖的封闭构造

    scala 构造 Scala Currying is the process of transforming a function that takes multiple arguments into ...

最新文章

  1. Windows系统下制作一个记事本以语音方式读出你输入的文字 以及放到开机启动项,开机自启动读出语音!
  2. PHP实现常见排序算法
  3. 【QA】Xcodeproj doesn't know about the following xxx 问题及解决方案
  4. ubuntu10.04 android编译问题
  5. 在vsphere6.5启用Tesla K80
  6. OpenCV修养(一)——引入
  7. Ruby file操作cheatsheet
  8. Mac10.12开启NTFS读写
  9. 【微信小程序】(一)开发工具下载与界面介绍
  10. android+日文输入法,玩转手机日语输入法(安卓/iphone)
  11. Python爬虫爬取中国大学慕课MOOC课程的课程评价信息(讨论信息),采用selenium动态爬取方法
  12. 通过X11转发在服务器上用IGV
  13. 记一次 unicode-escape 和 utf-8 编码的互解
  14. 论文阅读:MobileNetV2: Inverted Residuals and Linear Bottlenecks(MobileNetV2)
  15. Instagram后端架构
  16. 个人免签码支付源码|服务监控模块强大后台功能全面
  17. 劳动合同与聘用合同的区别
  18. Web中html个人介绍代码,web开发工程师自我介绍示例
  19. 2017全国计算机高校排名,全国计算机专业大学排名_2017计算机专业大学排名
  20. java中math的方法_Java中Math类常用方法代码详解

热门文章

  1. 懂,你的App生,不懂,死!
  2. 查询数据库里所有表名,字段名的语句
  3. 关于公司通过CMMI3级认证
  4. [转载] 递归函数python基例_python递归函数详解 python 递归函数使用装饰器
  5. [转载] Python之使用K-Means算法聚类消费行为特征数据分析(异常点检测)
  6. (@WhiteTaken)设计模式学习——代理模式
  7. EntityFramework6.X 之 Operation
  8. 《Simbody Theory Manual》
  9. Qt Qwdget 汽车仪表知识点拆解2 图像放大
  10. javascript中的内存泄漏