Scala Trait(特征)

Scala Trait(特征)

Traits就像Java中的接口。 Traits 可以有方法(包括抽象的和非抽象的)和字段作为其成员,但是它们比Java中的接口更强大,因为在traits中你可以实现成员。

关于Scala Traits的一些重要观点

  • Traits是使用trait关键字创建的。

语法

trait Trait_Name{
// Fields..
// Methods..
}

例子

// Scala program to illustrate how to
// create traits
 
// Trait
trait MyTrait
{
    def pet
    def pet_color
}
 
// MyClass inherits trait
class MyClass extends MyTrait
{
     
    // Implementation of methods of MyTrait
    def pet()
    {
        println("Pet: Dog")
    }
     
    def pet_color()
    {
        println("Pet_color: White")
    }
     
    // Class method
    def pet_name()
    {
        println("Pet_name: Dollar")
    }
}
 
object Main
{
     
    // Main method
    def main(args: Array[String])
    {
        val obj = new MyClass();
        obj.pet();
        obj.pet_color();
        obj.pet_name();
    }
}

输出

Pet: Dog
Pet_color: White
Pet_name: Dollar
  • 在Scala中,我们被允许在trait中实现方法(仅指抽象方法)。如果一个trait包含方法的实现,那么扩展这个trait的类不需要实现trait中已经实现的方法。正如下面的例子所示。

例子

// Scala program to illustrate the concept of
// abstract and non-abstract method in Traits
 
// Trait with abstract and non-abstract methods
trait MyTrait
{
    // Abstract method
    def greeting
 
    // Non-abstract method
    def tutorial
    {
        println("This is a tutorial" +
                "of Traits in Scala")
    }
}
 
 
// MyClass inherits trait
class MyClass extends MyTrait
{
     
    // Implementation of abstract method
    // No need to implement a non-abstract
    // method because it already implemented
    def greeting()
    {
        println("Welcome to GeeksforGeeks")
    }
}
 
object Main
{
     
    // Main method
    def main(args: Array[String])
    {
        val obj = new MyClass();
        obj.greeting
        obj.tutorial
    }
}

输出

Welcome to GeeksforGeeks
This is a tutorial of Traits in Scala
  • 特质不包含构造函数参数。
  • 当一个类继承了一个trait时,就使用extends关键字。

语法

class Class_Name extends Trait_Name{
// Code..
}
  • 当一个类继承了多个特征时,在第一个特征前使用extends关键字,之后在其他特征前使用with关键字。如下面的例子所示。

语法

class Class_Name extends Trait_Name1 with Trait_Name2 with Trait_Name3{
// Code..
}

例子

// Scala program to illustrate how
// a class inherits multiple traits
 
// Trait 1
trait MyTrait1
{
     
    // Abstract method
    def greeting
 
}
 
//Trait 2
trait MyTrait2
{
     
    // Non-abstract method
    def tutorial
    {
        println("This is a tutorial" +
               "of Traits in Scala")
    }
}
 
// MyClass inherits multiple traits
class MyClass extends MyTrait1 with MyTrait2
{
     
    // Implementation of abstract method
    def greeting()
    {
        println("Welcome to GeeksforGeeks")
    }
}
 
object Main
{
     
    // Main method
    def main(args: Array[String])
    {
        val obj = new MyClass();
        obj.greeting
        obj.tutorial
    }
}

输出

Welcome to GeeksforGeeks
This is a tutorial of Traits in Scala
  • 抽象类也可以通过使用extends关键字来继承特性。

语法

abstract class Class_name extends Trait_Name{
// code..
}
  • 在Scala中,一个trait可以通过使用extends关键字来继承另一个trait。

语法

trait Trait_Name1 extends Trait_Name2{
// Code..
}
  • 特质支持多重继承。
  • 在Scala中,通过在类的名称前使用 extends 关键字和在trait的名称前使用with 关键字,一个类可以同时继承普通类或抽象类和traits。

语法

class Class_Name1 extends Class_Name2 with Trait_Name{
// Code..
} 
  • 在trait中,抽象字段是那些包含初始值的字段,具体字段是那些包含初始值的字段。我们可以在扩展trait的类中覆盖它们。如果一个字段是用var关键字声明的,那么当我们覆盖它们时就不需要写override关键字。如果一个字段是用val 关键字声明的,那么当我们覆盖它们的时候,你必须写override关键字。

例子

// Scala program to illustrate
// concrete and abstract fields in traits
 
trait MyTrait
{
     
    // Abstract field
    var value: Int
     
    // Concrete field
    var Height = 10
    val Width = 30
}
 
class MyClass extends MyTrait
{
     
    // Overriding MyTrait's fields
    var value = 12
    Height = 40
    override val Width = 10
     
    // Method to display the fields
    def Display()
    {
        printf("Value:%d", value);
        printf("\nHeight:%d" ,Height);
        printf("\nWidth:%d", Width);
    }
}
 
object Main
{
     
    // Main method
    def main(args: Array[String])
    {
        val obj = new MyClass();
        obj.Display();
    }
}

输出

Value:12
Height:40
Width:10
  • 我们也可以将特质添加到一个对象实例中。或者换句话说,我们可以直接在一个类的对象中添加特质,而不需要将该特质继承到该类中。我们可以通过使用with关键字在对象实例中添加一个特性。

语法

val object_name = new Class_name with Trait_Name;

例子

// Scala program to illustrate how
// to add a trait to an object instance
 
class MyClass{}
trait MyTrait
{
    println("Welcome to MyTrait");
}
object Main
{
     
    // Main method
    def main(args: Array[String])
    {
         
        // Here MyTrait is added to the
        // object instance of MyClass
        val obj = new MyClass with MyTrait;
    }
}

输出

Welcome to MyTrait

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程