📜  在 Scala 中调用超类构造函数

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

在 Scala 中调用超类构造函数

先决条件 - Scala 构造函数
在 Scala 中,构造函数用于初始化对象的状态,并在对象创建时执行。有一个主构造函数,所有其他构造函数最终都必须链接到它。当我们在 Scala 中定义一个子类时,当我们定义子类声明的扩展部分时,我们控制由其主构造函数调用的超类构造函数。

  • 使用一个构造函数:调用超类构造函数的示例
    例子:
Scala
// Scala program to illustrate
// calling a super class constructor
 
// Primary constructor
class GFG (var message: String)
{
    println(message)
}
 
 
// Calling the super class constructor
class Subclass (message: String) extends GFG (message)
{
    def display()
    {
        println("Subclass constructor called")
    }
}
 
// Creating object
object Main
{
    // Main method
    def main(args: Array[String])
    {
         
        // Creating object of Subclass
        var obj = new Subclass("Geeksforgeeks");
        obj.display();
    }
}


Scala
// Scala program to illustrate
// calling a specific super class constructor
 
// Primary constructor (1)
class GFG (var message: String, var num: Int)
{
     
    println(message+num)
     
    // Auxiliary constructor (2)
    def this (message: String)
    {
        this(message, 0)
         
    }
     
}
 
// Calling the super class constructor with 2 arguments
class Subclass (message: String) extends GFG (message, 3000)
{
    def display()
    {
        println("Subclass constructor called")
    }
}
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
         
        // Creating object of Subclass
        var obj = new Subclass("Article count ");
        obj.display();
    }
}


Scala
// Scala program to illustrate
// calling a specific super class constructor
 
// Primary constructor (1)
class GFG (var message: String, var num: Int)
{
     
    println(message + num)
     
    // Auxiliary constructor (2)
    def this (message: String)
    {
        this(message, 0)
         
    }
     
}
 
 
// Calling the superclass constructor with 1 arguments
class Subclass (message: String) extends GFG (message)
{
    def display()
    {
        println("Subclass constructor called")
    }
}
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
         
        // Creating object of Subclass
        var obj = new Subclass("Article Count ");
        obj.display();
    }
}


输出:
Geeksforgeeks
Subclass constructor called

  • 在上面的例子中,子类被定义为调用GFG类的主构造函数,它是一个以message为参数的单参数构造函数。在 Scala 中定义子类时,在定义子类声明的扩展段时,控制子类的主构造函数调用的超类构造函数。
  • 具有多个构造函数:在超类具有多个构造函数的情况下,可以使用子类的主构造函数调用这些构造函数中的任何一个。例如,在下面的代码中,超类的双参数构造函数由子类的主构造函数通过定义特定的构造函数使用extends子句调用。
    例子:

斯卡拉

// Scala program to illustrate
// calling a specific super class constructor
 
// Primary constructor (1)
class GFG (var message: String, var num: Int)
{
     
    println(message+num)
     
    // Auxiliary constructor (2)
    def this (message: String)
    {
        this(message, 0)
         
    }
     
}
 
// Calling the super class constructor with 2 arguments
class Subclass (message: String) extends GFG (message, 3000)
{
    def display()
    {
        println("Subclass constructor called")
    }
}
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
         
        // Creating object of Subclass
        var obj = new Subclass("Article count ");
        obj.display();
    }
}
输出:
Article count 3000
Subclass constructor called

  • 我们可以在这里调用单参数构造函数,默认情况下另一个参数值为 0。
    例子:

斯卡拉

// Scala program to illustrate
// calling a specific super class constructor
 
// Primary constructor (1)
class GFG (var message: String, var num: Int)
{
     
    println(message + num)
     
    // Auxiliary constructor (2)
    def this (message: String)
    {
        this(message, 0)
         
    }
     
}
 
 
// Calling the superclass constructor with 1 arguments
class Subclass (message: String) extends GFG (message)
{
    def display()
    {
        println("Subclass constructor called")
    }
}
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
         
        // Creating object of Subclass
        var obj = new Subclass("Article Count ");
        obj.display();
    }
}
输出:
Article Count 0
Subclass constructor called