I am also a beginner in Kotlin, if there is any mistake, please help to point out, continue to update

Android: Kotlin Tutorial – Classes and Objects – Basic Syntax (5)

Android: Kotlin Learning Guide – Basic Syntax (1)

Android: Kotlin Tutorial – Basic Types – Basic Syntax (2)

Android: Kotlin – Package – Control Flow – Return and jump – Basic syntax (3)

Android: Kotlin Tutorial – Classes and Objects – Basic Syntax (4)

This article to share more content, it is recommended to pay attention to the collection, and then view, so as not to get lost
An abstract class

A class or members may be declared as abstract. An abstract method does not implement a method in its class. Remember that we don’t need to add the open annotation to an abstract class or function; it comes with it by default.

abstract class Derived : Base() { 
	override abstract fun f(a) 
}
Copy the code
Along with the object

Unlike Java or C#, there are no static methods in kotlin. In most cases, we recommend using only package-level functions. In Kotlin we can add the @jVMStatic annotation if we want to call a method directly or use a Companion object. If we want to use a property directly, we use @jVMField

class StringUtils{
    companion object {
        fun isEmpty(str:String):Boolean{
            return "".equals(str)
        }
    }
}
Copy the code
Properties and fields
Attribute declarations

In Kotlin, classes can have properties. We can declare mutable properties using the var keyword, or read-only properties using the val keyword. You can use an attribute directly by name, as you would with a field in Java:

val result = Address() // There is no new keyword in kotlin
Copy the code
Getters and Setters

Initializers, getters, and setters in the syntax are optional. If the attribute type can be inferred from the initializer or from the class’s member function, its type is also ignored. The declaration syntax for read-only attributes differs from that for mutable attributes in two ways: it begins with val instead of var, and it does not allow setter functions.

Compile-time constant

Properties whose values are known at compile time can be marked as compile-time constants using the const modifier

const val SUBSYSTEM_DEPRECATED: String = "This subsystem is depr ecated
Copy the code
Lazy initialization of properties

This modifier can only be used in the variable property definition of a class’s var type, not in the constructor. And properties cannot have custom getter and setter accessors. The type of this attribute must be non-empty, and you cannot add the LateInit modifier to a primitive attribute.

interface

Kotlin’s interface is much like Java 8. They can both contain abstract methods, as well as implementations of the methods. Unlike abstract classes, interfaces cannot hold state. It can have attributes but must be abstract, or provide an implementation of the accessor. An interface is defined by the keyword interface:

interface MyInterface { 
	fun bar(a) 
	fun foo(a) { 
	// The function body is optional}}Copy the code
Implementing an interface

A class or object can implement one or more interfaces

class Child : MyInterface { 
	fun bar (a) { / / the function body}}Copy the code
Attributes in interfaces

Attributes can be declared in the interface. Properties in an interface are either abstract or provide an implementation of the accessor. Interface properties cannot have backup fields. And accessors cannot refer to them.

Visibility modifiers

Classes, objects, interfaces, constructors, properties, and their setter methods can all have visibility modifiers. Getters have the same visibility as corresponding properties. There are four modifiers in Kotlin: private, protected, internal, and public. The default modifier is public.

package

Functions, properties and classes, objects and interfaces can be declared at “top-level”, that is, they can belong directly to packages:

// File name: example.kt
package foo 
fun baz(a) {} 
class bar {}
Copy the code
  • If no visibility modifiers are specified, public is used by default, which means your declaration is visible everywhere.
  • If you declare it private, it is visible only in the file that contains the declaration;
  • If declared as internal, visible anywhere in the same module;
  • Protected is not available in “top-level”
// File name: example.kt
package foo 
private fun foo(a) {} // visible in example.kt
public var bar: Int = 5 // Attributes are visible in approved places
private set // Setters are only visible in example.kt
internal val baz = 6 // Visible in the same module
Copy the code
The constructor

Specify the visibility of the main constructor (which must be displayed using the constructor keyword) by using the following syntax:

class C private constructor(a: Int) {... }Copy the code

Here the constructor is private. All constructors are public by default, and in fact are visible as long as the class is visible. (Note that the public property of an internal class can only be accessed in the same module.)

Function extension

To declare a function extension, we need to prefix the function with a receiver type. We’ll add a swap function to MutableList:

fun MutableList<Int>.swap(x: Int, y: Int) { 
	val temp = this[x] // this corresponds to list
	this[x] = this[y] 
	this[y] = tmp 
}
Copy the code

The this keyword in the extension function corresponds to the receiver object. Now we can use this function in any MutableList instance:

val l = mutableListOf(1.2.3) 
l.swap(0.2)// In the 'swap()' function 'this' holds the value' l '
Copy the code
Attribute extensions

Like functions, Kotlin also supports attribute extension:

val <T> List<T>.lastIndex: Int 
get() = size-1
Copy the code

Note that since the extension does not actually add member attributes to the class, there is no way for the extension attributes to have a backup field. This is why initializer functions are not allowed to have extended attributes. Extended attributes can only be defined by explicitly providing getter and setter methods.

Adjoint object extension

If an object defines an adjoint, you can also add an extension function or extension attribute to the adjoint:

class MyClass { 
	companion object{}}fun MyClass.Companion.foo(a){ 
}

MyClass.foo()
Copy the code
Data classes

Similar to Java beans

data class User(val name: String, val age: Int)
Copy the code
The generic

Like Java, classes in Kotlin can have type arguments:

class Box<T>(t: T){ 
	var value = t 
}
Copy the code
Nested classes

Classes can be nested within other classes

class Outer { 
	private val bar: Int = 1 
	class Nested { 
	fun foo(a) = 2}}val demo = Outer.Nested().foo() / / = = 2
Copy the code
The inner class

Classes can be marked inner so that they can access the members of the external class. The inner class has an object reference to the outer class:

class Outer { 
	private val bar: Int = 1 
	inner class Inner { 
		fun foo(a) = bar 
		} 
}
val demo = Outer().Inner().foo() / / = = 1
Copy the code
Anonymous inner class

Instances of anonymous inner classes are created through object expressions

window.addMouseListener(object: MouseAdapter() { 
	override fun mouseClicked(e: MouseEvent) { 
	// ... 
	}
	override fun mouseEntered(e: MouseEvent) { // ... }})Copy the code
Object statement

The singleton pattern is a very useful pattern, and it is easy to declare in Kotln

object DataProviderManager { 
	fun registerDataProvider(provider: DataProvider) { // ... 
	}
val allDataProviders: Collection<DataProvider> 
	get() = // ... 
}
	
DataProviderManager.registerDataProvider(...)
Copy the code

This is called an object declaration, and the object keyword is followed by the object name. Like variable declarations, object declarations are not expressions and cannot be used as rvalues in assignment statements.

Along with the object

Object declarations can be marked with the companion keyword inside class declarations:

class MyClass { 
	companion object Factory { 
		fun create(a): MyClass = MyClass() 
	} 
}

val instance = MyClass.create()
Copy the code

This article focuses on the classes, objects, interfaces, and other attributes in Kotlin.

I am also a beginner in Kotlin, if there is any mistake, please help to point out, continue to update