Kotlinの構造方法

6714 ワード

0、作り方を言う前に、例を貼っておきましょう
class Cat {
    private var name: String? = null

    constructor() : super() {}

    constructor(temp: String) {
        this.name = temp           //  Kotlin         super()
    }
}

Javaの
public class Cat {
    private String name = null;

    public Cat(){
        super();
    }

    public Cat(String temp){     //       Object       
                                //      super()
        this.name = temp;         
    }
}

 
1、公式サイトがどのように説明されているか見てみましょうhttp://kotlinlang.org/docs/reference/classes.html
A class in Kotlin can have a primary constructor and one or more secondary constructors
Kotlinのclassには1つの主構造方法と1つ以上の二次構造方法がある
 The primary constructor is part of the class header: it goes after the class name
主な構造方法はclass頭部の一部である.classの名前の後ろに付いています.
 
2、primary constructor第一の文法
class Person constructor(firstName: String) {
}

3、primary constructor第二の文法
if the primary constructor does not have any annotations or visibility modifiers, the constructor keyword can be omitted
マスターコンストラクションメソッドに注釈修飾やアクセス権限の修飾がない場合は、constructorキーワードは省略できますよ
class Person (firstName: String) {
}

4、primary constructorを省略できない場合の一つ、見たでしょう、プライベートなconstructor
class Cat private constructor() {

}

5、primary constructorの特徴はコードブロックの内容がないことです.あなたも発見したでしょう.
The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, which are prefixed with the init keyword.
主な構築方法にはコードが含まれていません.初期化コードは初期化コードブロックで配置でき、キーワードinitを使用してコードブロックの前に配置できます.
class Cat  (var name : String, var age: Int) {
    init {
        println(name)
        println(age)
    }
}

6、コードの初期化順は、ここでは書かないでしょう.後でclassの初期化順で、まとめましょう
 
7、クラスを定義するとき、Primary Constructorを明示的に提供しない場合、Kotlinコンパイラはデフォルトで無パラメトリック構造を生成します.これはJavaと同じです.
class Cat() {  //                
    
}
class Cat{   //            , java   ,         
    
}

 
8、主な構造方法で、パラメータにvarやvalを記入しないとどうなりますか.注釈を見よう
class Cat(name:String){
    
    init {
        name = "Katty" #      ,name   var  ,    val,   
    }
}

 
9、Much the same way as regular properties, the properties declared in the primary constructor can be mutable (var) or read-only (val).
通常のプロパティと同様に、プライマリ構築メソッドで宣言されるパラメータは、可変(var)または読み取り専用であってもよく、次の例を参照してください.
class Person(val firstName: String, val lastName: String, var age: Int) { ... }

 
10、次の主な構造方法の注釈の例を見て、この時constructorキーワードは省略できない
class Customer public @Inject constructor(name: String) { ... }

 
10-1、主な构造方法の中のパラメータはvarあるいはvalの利益を添加して、varあるいはvalのパラメータをプラスして、実例の変数に相当して、ここで自分で研究しなければならなくて、形容が正确ではないかもしれなくて、公式も说明していません
もしかしてvarやvalを入れないとだめなの?だから
 
 
11、Secondary Constructorsの二次構造方法は、意外にもまとめてみるとこんなに多い
The class can also declare secondary constructors, which are prefixed with constructor:
クラスの中で二次構造の方法を書くことができて、constructorを接頭辞にして、次の例を見ます
class Person {
    constructor(parent: Person) {
        parent.children.add(this)
    }
}

 
12、ピットを踏んで、二次構造方法は普通の方法と同じで、パラメータはvarあるいはval修飾を加えることができません
class Cat(val name:String){

    var age : Int = 0

    constructor( name : String, age : Int) : this(name) { #               
                                                         #   var val,    ,      
        this.age = age
    }
}

 
13、つまりサブコンストラクションメソッドは必ずマスターコンストラクションメソッドを呼び出し、パラメータのないマスターコンストラクションメソッドでも暗黙的に呼び出されます.パラメータのあるマスターコンストラクションメソッドであれば、thisを明示的に使用して呼び出さなければなりません
a、二次構築方法は、パラメータを呼び出した主構築方法を表示しなければならない
b、又は他のサブコンストラクションメソッドを呼び出し、他のサブコンストラクションメソッドからメインコンストラクションメソッドを呼び出すことを表示する
c、キーワードthisを使う
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
クラスにプライマリ・コンストラクション・メソッドがある場合、各セカンダリ・コンストラクション・メソッドは、直接または間接的に別のセカンダリ・コンストラクション・メソッドを介してプライマリ・コンストラクション・メソッドに委任する必要があります.thisキーを使用して同じクラスに委任される別の構築方法
class Person {
    constructor(parent: Person) { #            ,       
        parent.children.add(this)
    }
}
class Person(val name: String) {
    constructor(name: String, parent: Person) : this(name) {      this
        parent.children.add(this)
    }
}

 
14、ピットを踏むのがまた来たことに注意して、それはinitコードブロックが必ず実行して、しかもあなたの二次構造方法の前に実行します
Note that code in initializer blocks effectively becomes part of the primary constructor. Delegation to the primary constructor happens as the first statement of a secondary constructor, so the code in all initializer blocks is executed before the secondary constructor body. Even if the class has no primary constructor, the delegation still happens implicitly, and the initializer blocks are still executed
なお、初期化コードブロックのコードは、主な構成方法の一部として有効である.マスターコンストラクションメソッドをサブコンストラクションメソッドの最初の文として委任するため、初期化コードブロックのコードは、サブコンストラクションメソッドの前に実行されます.クラスにマスターメソッドがなくても、依頼は暗黙的に行われ、初期化コードブロックのコードは実行されます.
 
15、1つのclassは主構造方法がなくて、また二次構造方法がなくて、デフォルトは1つのpublic無参の主構造方法を生成します
If a non-abstract class does not declare any constructors (primary or secondary), it will have a generated primary constructor with no arguments. The visibility of the constructor will be public. If you do not want your class to have a public constructor, you need to declare an empty primary constructor with non-default visibility:
非抽象クラスの場合、公開されていない構造メソッド(プライマリ構築メソッドまたはセカンダリ構築メソッド)は、パラメータのないプライマリ構築メソッドを生成します.このプライマリ・コンストラクション・メソッドの可視性はpublicです.classにpublicのプライマリ・コンストラクション・メソッドがあることを望んでいない場合は、デフォルトの可視性ではなく、空のプライマリ・コンストラクション・メソッドを宣言する必要があります.
class DontCreateMe private constructor () { ... }

 
16、一つの問題を考えて、二次構造方法だけで、主構造方法がなければ、Kotlinは無参の構造方法を生成しますか?
試してみるとjavaと同様にclassには構築方法、メイン構築方法、サブ構築方法がありますが、あればデフォルトでは無参のメイン構築方法は生成されません
class Cat{

    var age : Int = 0

    constructor( name : String, age : Int) { #        :this(),          ,       ,  ,       ,             
        this.age = age
    }

}

 
17、まとめ
a、意外にも構造方法の一環で、Kotlinは革新があって、実はコード構造がもっとはっきりしているので、いいでしょう
b、二次構造方法のみであってもよく、主構造方法がない
c、同時に2種類存在することができる
d、プライマリ・セカンダリ・コンストラクション・メソッドがない場合、コンパイラはデフォルトのパラメータのないプライマリ・コンストラクション・メソッドを生成する
e、半日は公式文書の紹介がはっきりしていると感じて、言語を勉強したほうがいいようです.
公式の住所をもう一度メモします.http://kotlinlang.org/docs/reference/classes.html