2017-05-31 5 views
16

私は公式の文書からKotlinを学んでいます。私はparametersという2つのを作成したところで、classを作成しました。 constructorのボディはブロックinitです。コトルのコンストラクタ

class Person(name: String, surname: String) { 
    init { 
     Log.d("App", "Hello"); 
    } 
} 

まあ、私はconstructorで取る1以上を作成したいです。どのように行うのですかKotlin

答えて

22

まあinitは、コンストラクタの本体ではありません。プライマリコンストラクタのコンテキストを持つプライマリコンストラクタの後に呼び出されます。公式ドキュメントに与えられたよう

:あなたの質問を1として

The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, which are prefixed with the init keyword:

class Customer(name: String) { 
    init { 
     logger.info("Customer initialized with value ${name}") 
    } 
} 

Note that parameters of the primary constructor can be used in the initializer blocks. They can also be used in property initializers declared in the class body:

class Customer(name: String) { 
    val customerKey = name.toUpperCase() 
} 

In fact, for declaring properties and initializing them from the primary constructor, Kotlin has a concise syntax:

class Person(val firstName: String, val lastName: String, var age: Int) { 
    // ... 
} 

使用すると、1つのパラメータを受け入れるようにコンストラクタを追加することができます

class Person(name: String, surname: String) { 

    constructor(name: String) : this(name, "") { 
     // constructor body 
    } 

    init { 
     Log.d("App", "Hello"); 
    } 
} 

しかし、2番目の引数の空の文字列を渡す必要はないため、正しく表示されません。ですから、次のようにコンストラクタを注文することができます:

class Person(name: String) { 

    constructor(name: String, surname: String) : this(name) { 
     // constructor body 
    } 

    init { 
     Log.d("App", "Hello"); 
    } 
} 

希望します。

+0

'this(name)'はプライマリコンストラクタを呼び出しますか?もしそうなら、我々は確認するためにログすることができますか? –

+0

'this()'はコンストラクタの呼び出しに使用され、パラメータのシグネチャはどのコンストラクタを呼び出すかを定義します。 – chandil03

+0

正しい。私たちはそれがプライマリコンストラクタを呼び出すかどうかを確認できますか? –

2

クラス内部では、補助コンストラクタを作成するためにconstructorキーワードを使用します。同様:

class Person(name: String, surname: String) { 
    init { 
     Log.d("App", "Hello"); 
    } 
    constructor(id: Int) { 

    } 
} 

詳細情報についてご確認Secondary Constructors

EDIT:
ルール:クラスは、プライマリコンストラクタを持つ場合は、各二次コンストラクタは、主コンストラクタに委任する必要があり、直接または他の2次コンストラクタを介して間接的に実行されます。同じクラスの別のコンストラクタへの委譲は、thisキーワードを使用して行われます。

class Person(val name: String) { 
    constructor(name: String, parent: Person) : this(name) { 
     parent.children.add(this) 
    } 
} 

セカンダリコンストラクタを呼び出すときに、それは名前を初期化するための主コンストラクタを呼び出し、そのあとは二コンストラクタで自分のものを実行します。上記の例では、名前はプライマリコンストラクタを呼び出すことによって初期化されています。

+0

私はこのコード 'クラスPerson(名称:文字列、姓:String)を持っている{ のinit { Log.d( "こんにちは"、 "こんにちは$名$姓を"); } コンストラクタ(名:文字列){ } は} '二コンストラクタを追加した後に、Androidのメーカーは、'エラーが私をほのめかし:(10、5)プライマリコンストラクタ呼び出しのexpected' –

+0

あなたが呼び出す必要がありますが、これはありますthisキーワードを使用するプライマリコンストラクタ。まもなく解答を書く。 –

1

セカンダリコンストラクタ

クラスは、コンストラクタが付いされた2次のコンストラクタを宣言することができる:

class Person { 
    constructor(parent: Person) { 
     parent.children.add(this) 
    } } 

クラスは、プライマリコンストラクタを持っている場合、各二コンストラクタは、プライマリコンストラクタに委任する必要があります、他の2次コンストラクタを介して直接的または間接的に実行されます。同じクラスの別のコンストラクタへの委任このキーワードを使用して行われます:

class Person(val name: String) { 
    constructor(name: String, parent: Person) : this(name) { 
     parent.children.add(this) 
    } } 

https://kotlinlang.org/docs/reference/classes.htmlセカンダリコンストラクタの一部を参照してください

1

これは、別のコンストラクタを作成する方法です。

class Person(name: String, surname: String) { 
    init { 
     Log.d("App", "Hello"); 
    } 
constructor(id: Int) : this("example name", "example surname") { 

} 
} 

は必ず二コンストラクタは、プライマリコンストラクタを参照する必要があります、それは、このキーワードを使用してパラメータだ、ということを覚えておいてください。 this(name,"")

If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the this keyword:

または

kotlinはそうタイプでnull値を表すために?を使用this(name,null)ようnullを使用することはできませんなぜ空の値

// (name: String, surname: String) default constructor signature 
class Person(name: String, surname: String) { 

    // init block , represents the body of default constructor 
    init { 
     Log.d("primary", "Hello"); 
    } 

    // secondary constructor 
    // this(name,"") call to default constructor 
    constructor(name : String):this(name,""){ 
     Log.d("secondary", "Hello"); 
    } 
} 

2

最初の方法、 surname: String?

class Person(name: String, surname: String?) { 

    init { 
     Log.d("primary", "Hello"); 
    } 

    constructor(name : String):this(name,null){ 
     Log.d("secondary", "Hello"); 
    } 
} 
関連する問題