📜  斯卡拉 |抽象类型成员

📅  最后修改于: 2022-05-13 01:54:49.422000             🧑  作者: Mango

斯卡拉 |抽象类型成员

如果该特定成员在类中没有完整的定义,则称该类或特征的成员是抽象的。这些抽象成员总是在定义它的类的任何子类中实现。这种声明在许多编程语言中都是允许的,并且是面向对象编程语言的关键特性之一。 Scala 还允许声明此类方法,如下例所示:

abstract class Sample{
  def contents: Array[String]
  def width: Int = a
  def height: Int = b
}

因此,在上面的类 Sample 中,我们声明了三个方法:contents、width 和 height。最后两个方法的实现已经定义,而在第一个方法中,contents 没有提到任何类型的实现。因此,此方法是类 Sample 的抽象成员。需要注意的是,具有抽象成员的类本身必须声明为抽象的。类前面的这个抽象关键字表示该类肯定会有一个没有实现的抽象成员。
如何在类中编写抽象成员的另一个示例:

abstract class Example{
   type T
   def transform(x: T): T
   val initial: T
   var current: T
}

在上面的示例中,声明了抽象类,它定义了一个抽象类型 T、一个抽象方法转换、一个抽象值初始值和一个抽象值当前。

下面是抽象类型成员的示例:

例子 :

// Scala program of abstract type member 
  
// Declaring an abstract class
abstract class vehicle (name:String) 
{    
    // This is an abstract member 
    // with undefined implementation 
    val category: String     
      
    // A function that is used to print 
    // the value of the abstract member
    def car_type{ println(category) }  
    override def toString = s" The vehicle type is $category"
}
  
// Now extend the classes bike, car, 
// truck and bus and provide values 
// for the variable type
  
class car (name:String) extends vehicle (name)
{
    // assigning the value of 
    // the abstract member as car
    val category = "car"             
}
class bike (name:String) extends vehicle (name)
{
    // assigning the value of 
    // the abstract member as bike
    val category = "bike"         
}
class bus (name:String) extends vehicle (name)
{
    // assigning the value of 
    // the abstract member as bus
    val category = "bus"             
}
class truck (name:String) extends vehicle (name)
{
    // assigning the value of 
    // the abstract member as truck
    val category = "truck"         
}
  
object AbstractFieldsDemo extends App
{
    // assigning the name as Honda in the abstract
    // class where the category value is car
    val car = new car("Honda") 
      
    // assigning the name as Yamaha in the abstract 
    // class where the category value is bike
    val bike = new bike("Yamaha") 
      
    // assigning the name as Tata in the abstract 
    // class where the category value is bus
    val bus = new bus("Tata")     
      
    // assigning the name as Ashok_Leyland in the
    // abstract class where the category value is truck
    val truck = new truck("Ashok_Leyland") 
      
     // implementing the function 
     // car_type for the object car
    car.car_type
      
    // implementing the function 
    // car_type for the object bus
    bus.car_type
      
    // implementing the function 
    // car_type for the object truck
    truck.car_type     
      
    // implementing the function 
    // car_type for the object bus
    bike.car_type     
    println(car)
    println(bus)
    println(truck)
    println(bike)
}

输出

car
bus
truck
bike
The vehicle type is car
The vehicle type is bus
The vehicle type is truck
The vehicle type is bike

在上面的示例中,特征车辆具有一个抽象的 val类别以及一个名为car_type的简单具体方法和一个 toString 方法的覆盖。然后类carbikeTruckBus扩展类vehicle并为字段类别提供值。
我们在上面的代码中看到了抽象成员的未定义实现是如何被用于分配值并为每个不同类型的对象更改分配值的。就像在这个例子中一样,我们为不同类型的车辆类型存储了多个类别值。

因此,作为结论,抽象数据成员是那些具有未知实现的成员。