我的班级:
class Manager (var name: String, var nationality: String) {
constructor(agent: String): this() {}
}
返回以下错误:
None of the following functions can be called with the arguments supplied.
<init>(String) defined in Manager
<init>(String, String) defined in Manager
知道为什么吗?
给定的
type A() =
member val Prop: int = 0 with get, set
创建实例有多种方法
let a0 = A() // Prop = 0
let a1 = A(Prop = 1)
let a2 = A()
a2.Prop <- 2
现在,我们希望增强类并允许在构造函数中传递螺旋桨值,但不丢失我们已经拥有的无参数构造函数。
type A1() =
member val Prop: int = 0 with get, set
new(prop: int) = A1(Prop = prop) //Error
然而,这是一个错误
当我们重载构造函数时,Oracle引用并没有告诉这个关键字的最佳实践。有人能为它提出最佳实践建议吗?
选项1:委托给另一个构造函数
public class A {
private int x, y, z, p;
public A() {
this(1,1,1,1);
}
public A(int x, int y, int z, int p) {
this.x = x;
this.y = y;
this.z = z;
this.p = p;
}
}
和
选项2:设置
例如,如何做到这一点:
class Cat(name: String, val age: Int) {
def this() = this("Garfield", 20)
}
val someCat = new Cat
someCat.age
res0: Int = 20
与以下不同:
class Cat(name: String = "Garfield", val age: Int = 20)
val someCat = new Cat
someCat.age
res0: Int = 20
注意:我看到了其他问题(例如)的答案,这些问题讨论了Java在辅
在以下方面有何区别:
class Pitch(var width: Int = 3, var height: Int = 5) {
constructor(capacity: Int): this()
}
和
class Pitch(var width: Int = 3, var height: Int = 5, capacity: Int)
构造函数提供了哪些优点?
我有以下类型
type NumberedCollection(list) =
let list = list
new () = NumberedCollection(ResizeArray<string>())
它对甜食有好处,就像果冻一样。
但是当我添加另一个构造函数,它接受seq<string>
type NumberedCollection(list) =
let list = list
new () = NumberedCollection(ResizeArray<string>())
new (col :
我是Scala的新手,仍然要面对一些新手(?)问题。
所以我有一个名为Transaction的类,它的主构造函数有5个参数。问题是,我正在尝试编写一个带有字符串参数的单参数构造函数,这样它就可以从中获取其他参数,并使用它们调用主构造函数(它基本上解析字符串)。
我读到辅助构造函数中的第一个调用必须是对主构造函数的调用,那么我应该如何在没有有用数据的情况下调用它呢?还有什么其他方法可以解决这个问题?
这里我粘贴了类代码和失败的构造函数:
class Transaction(srcIp: String, dstIp: String, var srcPort: Int, var dstPort: I
如何在Kotlin Android中为数据类创建构造函数?
data class EventItem(
@SerializedName("dateEvent")
val dateEvent: String,
@SerializedName("dateEventLocal")
val dateEventLocal: Any,
@SerializedName("idAwayTeam")
val idAwayTeam: String,
@Se
因为我有一个有两个参数的User类: first_name,last_name。所以我使用be的kotlin类:
data class User(val first_name:String, val last_name:String)
现在我想要一个只接受first_name的构造函数,或者你可以说只接受一个参数。我如何用Kotlin定义它?
我知道我们可以传递默认值,这样我们就可以忽略第二个参数,但是我们如何编写多个构造函数呢?
我有演示者
class PhonePresenter @Inject constructor(
private val preference: DataPreference,
private val ioScheduler: Scheduler = Schedulers.io())
ioScheduler是默认参数。我只想注入preference: DataPreference。现在我有个例外
[dagger.android.AndroidInjector.inject(T)] io.reactivex.Scheduler cannot be provided without
这是Kotlin中的继承示例。 open class Person() {
var _name : String = ""
var _age : Int = 0
constructor(name: String, age : Int) : this(){
_name = name
_age = age
}
}
class Student() : Person() {
var _university : String = ""
constructor (name : Stri
我想实现一个有两个解析者的类。和空构造函数以及另一个带有param user:FirebaseUser的构造函数。
但我收到的信息错误:
“在授权变更中有一个循环”
class UserModel(user: FirebaseUser) {
var uid: String?
val email: String?
val phoneNumber: String?
val photoUrl: String
val displayName: String?
//error message: There is a cycle in the d
我想使用kotlin数据类作为例外,这似乎很好:
data class MyException(val extraData: Any) : RuntimeException()
在存在cause的情况下,我还希望能够将它传递给超类。不幸的是,数据类只能在其主构造函数中包含val/var,而且由于默认构造函数调用了no-args RuntimeException()构造函数,我似乎无法做到这一点,而不总是要求传递cause,并将其作为字段存储在类中,这是我不想要的。
我想要的是这样的:
data class MyException(val extraData: Any) : RuntimeExc
我不明白如何在Java上开发类似如下的Scala代码:
public abstract class A {
protected A() { ... }
protected A(int a) { ... }
}
public abstract class B {
protected B() { super(); }
protected B(int a) { super(a); }
}
public class C extends B {
public C() { super(3); }
}
虽然很清楚如何开发C类,但我不知道如何接收B.帮助。
另外,我正在尝试创
我有带有参数x和y的B类,它们是从带有参数x的A类扩展而来的,它是可选的(所有参数都是非空的),我如何才能以可选的方式定义B,并且它将在A的构造函数中使用可选值
val y = 0
val b = if (y == 0) B(y) else B(y, 0)
class B(y: Int, x: Int = 238) : A(x)
open class A(x: Int = 238)
在这里,我在B的构造函数中设置了x的默认值。有没有什么方法可以不用在B中设置默认值
我正在尝试做一些类似下面代码中的事情。我收到了错误,就像VS的问题标题一样。有人能告诉我出什么事了吗?
class A
{
public:
int x;
A(int x = 15)
{
this->x = x;
}
};
class B : public A
{
public:
int a,b,c;
B(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
我有一个Kotlin数据类,它接受下面的参数和parcelable参数。我在这个数据类中定义了一个辅助构造函数,它有两个附加参数lastSeen和netTotal。但是,我一直得到错误--在代理中有一个循环,为辅助构造函数调用chain。我该怎么解决这个问题?
@Parcelize
data class Person(
val email: Email,
val overrideEmail: Email,
val phone: String,
val firstName: String,
val overrideFirstName: String,
这里有一件事:我有一些基本的类,例如:
abstract class Car(private val model: String = "no name")
我想让类扩展Car,例如:
class Mazda(model: String) extends Car(model)
我的问题是:如何在没有给定模型的情况下创建马自达实例:new Mazda并拥有名为no name的对象
这是一个主要的构造函数代码A,对吗?
在代码B中构造函数()是主构造函数吗?
代码B中是否没有主构造函数?
码A
class Person constructor(firstName: String) {
}
码B
class Person {
var name: String = ""
constructor() // Is this a primary constructor
constructor(name:String):this(){
this.name = name
}
}
代码C
class Person {
我是斯卡拉的初学者。在试图实现集合时有疑问..。
class MyLinkedHashMap[K, V] {
var initialCapacity = 16
private var index = 0
var map: Array[Node[K, V]]
var head: Node[K, V]
var tail: Node[K, V]
class Node[K, V] {
var key: K;
var value: V;
var next: Node[K, V]
var before: Node[K, V]
var af
我试图像这样调用父母的第二个构造函数
abstract class A(val i: Int) {
constructor(c: C) : this(c.i)
}
class B() : A(0) {
constructor(c: C) : super(c) // error is here
}
class C(val i: Int)
但是它会产生Primary constructor call expected错误。子类如何调用父类的辅助构造函数?