以下是一个示例代码:

class Parent(name: String, age: Int) {
  println(s"Parent: name=$name, age=$age")
}

class Child(name: String, age: Int) extends Parent(name, age) {
  println(s"Child: name=$name, age=$age")
}

val child = new Child("Alice", 10)

在上面的代码中,Parent类没有无参构造函数,只有一个带有两个参数的构造函数。Child类继承自Parent类,并且也只有一个带有两个参数的构造函数。在创建Child对象时,会调用Parent类的构造函数,并打印出相应的信息。最后,会打印出Child类的构造函数信息。

输出结果为:

Parent: name=Alice, age=10
Child: name=Alice, age=10
scala子类只有一个2参数的构造函数父类没有无参构造函数请写出具体代码

原文地址: https://www.cveoy.top/t/topic/jb6m 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录