Kotlin constructor with parameters

Classes

Classes in Kotlin are declared using the keyword class :

The class declaration consists of the class name, the class header (specifying its type parameters, the primary constructor, and some other things), and the class body surrounded by curly braces. Both the header and the body are optional; if the class has no body, the curly braces can be omitted.

Constructors

A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is a part of the class header, and it goes after the class name and optional type parameters.

If the primary constructor does not have any annotations or visibility modifiers, the constructor keyword can be omitted:

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

During the initialization of an instance, the initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers:

Primary constructor parameters can be used in the initializer blocks. They can also be used in property initializers declared in the class body:

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

Such declarations can also include default values of the class properties:

You can use a trailing comma when you declare class properties:

Much like regular properties, properties declared in the primary constructor can be mutable ( var ) or read-only ( val ).

If the constructor has annotations or visibility modifiers, the constructor keyword is required and the modifiers go before it:

Secondary constructors

A class can also declare secondary constructors, which are prefixed with constructor :

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:

class Person(val name: String) < val children: MutableList= mutableListOf() constructor(name: String, parent: Person) : this(name) < parent.children.add(this) >>

Code in initializer blocks effectively becomes part of the primary constructor. Delegation to the primary constructor happens at the moment of access to the first statement of a secondary constructor, so the code in all initializer blocks and property initializers is executed before the body of the secondary constructor.

Even if the class has no primary constructor, the delegation still happens implicitly, and the initializer blocks are still executed:

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 don’t want your class to have a public constructor, declare an empty primary constructor with non-default visibility:

On the JVM, if all of the primary constructor parameters have default values, the compiler will generate an additional parameterless constructor which will use the default values. This makes it easier to use Kotlin with libraries such as Jackson or JPA that create class instances through parameterless constructors.

Creating instances of classes

To create an instance of a class, call the constructor as if it were a regular function:

Kotlin does not have a new keyword.

The process of creating instances of nested, inner, and anonymous inner classes is described in Nested classes.

Class members

Inheritance

Classes can be derived from each other and form inheritance hierarchies. Learn more about inheritance in Kotlin.

Abstract classes

A class may be declared abstract , along with some or all of its members. An abstract member does not have an implementation in its class. You don’t need to annotate abstract classes or functions with open .

You can override a non-abstract open member with an abstract one.

Companion objects

If you need to write a function that can be called without having a class instance but that needs access to the internals of a class (such as a factory method), you can write it as a member of an object declaration inside that class.

Even more specifically, if you declare a companion object inside your class, you can access its members using only the class name as a qualifier.

Источник

Классы

Классы в Kotlin объявляются с помощью использования ключевого слова class .

Объявление класса состоит из имени класса, заголовка (указания типов его параметров, основного конструктора и т.п) и тела класса, заключённого в фигурные скобки. И заголовок, и тело класса являются необязательными составляющими. Если у класса нет тела, фигурные скобки могут быть опущены.

Конструкторы

Класс в Kotlin может иметь основной конструктор (primary constructor) и один или более дополнительных конструкторов (secondary constructors). Основной конструктор является частью заголовка класса, его объявление идёт сразу после имени класса (и необязательных параметров).

class Person constructor(firstName: String) < /*. */ >

Если у основного конструктора нет аннотаций и модификаторов видимости, ключевое слово constructor может быть опущено.

class Person(firstName: String) < /*. */ >

Основной конструктор не может содержать в себе исполняемого кода. Инициализирующий код может быть помещён в соответствующие блоки (initializers blocks), которые помечаются словом init .

При создании экземпляра класса блоки инициализации выполняются в том порядке, в котором они идут в теле класса, чередуясь с инициализацией свойств.

class InitOrderDemo(name: String) < val firstProperty = "Первое свойство: $name".also(::println) init < println("Первый блок инициализации: $") > val secondProperty = "Второе свойство: $".also(::println) init < println("Второй блок инициализации: $") > > 

Обратите внимание, что параметры основного конструктора могут быть использованы в инициализирующем блоке. Они также могут быть использованы при инициализации свойств в теле класса.

class Customer(name: String)

Для объявления и инициализации свойств основного конструктора в Kotlin есть лаконичное синтаксическое решение:

class Person(val firstName: String, val lastName: String, var age: Int) 

Такие объявления также могут включать в себя значения свойств класса по умолчанию.

class Person(val firstName: String, val lastName: String, var isEmployed: Boolean = true) 

Вы можете использовать завершающую запятую при объявлении свойств класса.

class Person( val firstName: String, val lastName: String, var age: Int, // завершающая запятая ) < /*. */ >

Свойства, объявленные в основном конструкторе, могут быть изменяемые ( var ) и неизменяемые ( val ).

Если у конструктора есть аннотации или модификаторы видимости, ключевое слово constructor обязательно, и модификаторы используются перед ним.

class Customer public @Inject constructor(name: String) < /*. */ >

Для более подробной информации см. «Модификаторы доступа».

Дополнительные конструкторы

В классах также могут быть объявлены дополнительные конструкторы (secondary constructors), перед которыми используется ключевое слово constructor .

class Person(val pets: MutableList = mutableListOf()) class Pet < constructor(owner: Person) < owner.pets.add(this) // добавляет этого питомца в список домашних животных своего владельца >> 

Если у класса есть основной конструктор, каждый дополнительный конструктор должен прямо или косвенно ссылаться (через другой(ие) конструктор(ы)) на основной. Осуществляется это при помощи ключевого слова this .

class Person(val name: String) < val children: MutableList= mutableListOf() constructor(name: String, parent: Person) : this(name) < parent.children.add(this) >> 

Обратите внимание, что код в блоках инициализации фактически становится частью основного конструктора. Дополнительный конструктор ссылается на основной при помощи своего первого оператора, поэтому код во всех блоках инициализации, а также инициализация свойств выполняется перед выполнением кода в теле дополнительного конструктора.

Даже если у класса нет основного конструктора на него все равно происходит неявная ссылка и блоки инициализации выполняются также.

class Constructors < init < println("Блок инициализации") >constructor(i: Int) < println("Constructor $i") >> 

Если в абстрактном классе не объявлено никаких конструкторов (основного или дополнительных), у этого класса автоматически сгенерируется пустой конструктор без параметров. Видимость этого конструктора будет public.

Если вы не желаете иметь класс с открытым public конструктором, вам необходимо объявить пустой конструктор с соответствующим модификатором видимости.

class DontCreateMe private constructor () < /*. */ >

On the JVM, if all of the primary constructor parameters have default values, the compiler will generate an additional parameterless constructor which will use the default values. This makes it easier to use Kotlin with libraries such as Jackson or JPA that create class instances through parameterless constructors. > > «`kotlin > class Customer(val customerName: String = «») > «` —>

В JVM компилятор генерирует дополнительный конструктор без параметров в случае, если все параметры основного конструктора имеют значения по умолчанию. Это делает использование таких библиотек, как Jackson и JPA, более простым с Kotlin, так как они используют пустые конструкторы при создании экземпляров классов.

class Customer(val customerName: String = "") 

Создание экземпляров классов

Для создания экземпляра класса конструктор вызывается так, как если бы он был обычной функцией.

val invoice = Invoice() val customer = Customer("Joe Smith") 

В Kotlin нет ключевого слова new .

Создание экземпляров вложенных, внутренних и анонимных внутренних классов описано в разделе Вложенные классы.

Члены класса

Классы могут содержать в себе:

Наследование

Классы могут быть производными друг от друга и формировать иерархии наследования. Узнайте больше о наследовании в Котлине.

Абстрактные классы

Класс может быть объявлен как abstract со всеми или некоторыми его членами. Абстрактный член не имеет реализации в своём классе. Обратите внимание, что нам не надо аннотировать абстрактный класс или функцию словом open — это и так подразумевается.

abstract class Polygon < abstract fun draw() >class Rectangle : Polygon() < override fun draw() < // рисование прямоугольника >> 

Можно переопределить неабстрактный open член абстрактным.

open class Polygon < open fun draw() < // некоторый метод рисования полигонов по умолчанию >> abstract class WildShape : Polygon() < // Классы, которые наследуют WildShape, должны предоставлять свой собственный // метод рисования вместо использования по умолчанию для полигона abstract override fun draw() >

Вспомогательные объекты

Если вам нужно написать функцию, которая может быть использована без создания экземпляра класса, имеющую доступ к данным внутри этого класса (к примеру, фабричный метод), вы можете написать её как член объявления объекта внутри этого класса.

В частности, если вы объявляете вспомогательный объект в своём классе, у вас появляется возможность обращаться к членам класса, используя только название класса в качестве классификатора.

© 2015—2023 Open Source Community

Источник

Читайте также:  Html site icon png
Оцените статью