scala 抽象方法
The type whose identity is not known precisely can be termed as abstract type.
身份未知的类型可以称为抽象类型 。
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 X
def totalmarks(m1:X,m2:X):X
val 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 + m2
val 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。
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:String
def 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:String
val 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: String
def 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 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: String
var age: Int
}
The vars declared are equipped with getter and setter methods.
声明的var配有getter和setter方法。
trait Student {
def name:String //getter method for name
def name_=(n:String) //setter method for name
def age:Int //getter method for age
def 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: Int
val total: Int
require(total != 0)
val perc = (obtained % total) * 100
override 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 * scale
val total = 5 * scale
}
java.lang.IllegalArgumentException: requirement failed
at 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: Int
val total: Int
require(total != 0)
val perc = (obtained.toFloat / total.toFloat) * 100
override 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 = 70
val 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编程中抽象类型的全部内容,我们将在后续文章中研究注释。
scala 抽象方法