How to Create Entity Classes in Kotlin
- 2021-11-24 02:48:34
- OfStack
Basic format of class
class class name {
}
Basic format of attributes
var Attribute Name: Type
The following is the entity class code
package com.dldw.entity
import java.util.*
class Demo {
//var Declared properties can be 2 Sub-assignment val Declare an immutable attribute, and you can't assign it after assignment, otherwise compile an error
// Long integer 64 Bit Pay attention to the extra writing at the back L
var height: Long? = 0L
// Integer type 32 Bit
var id : Int?= 0
// Short integer 16 Bit
var short :Short? =0
// 8 Bit
var name : Byte? = null
// Floating point type Double precision 64 Bit
var level: Double = 0.0
// Single precision Add after f
var levelf: Float = 0.0f
// Time type member attributes ? Indicates that the property can be null
var createTime: Date?=null
// Initialization, kotlin No new Keyword, direct Object () Is to create 1 New objects
var time: Date?=Date()
// String type
var introduction: String? = null
// Boolean type as attribute
var isHide: Boolean? = null
// Variable collection as attribute
var child: MutableList<Demo> ?= null
// Char Yes 1 Individual types It does not mean numbers, but needs to use ' ' Close it up, or it will report an error
var char : Char?= '1'
}
Supplementary knowledge: classes, constructors, objects in Kotlin
1. Definition of class in 1. Kotlin
Composition of class:
Class consists of constructor and initialization code block, attribute (field), function (method), inner class (nested class) and object declaration
Classes are also created using the class keyword in//Kotlin
class Empty
2. Create an object for a class
fun main(args: Array<String>) {
val empty=Empty() //Kotlin Objects created in the new Keyword
}
3. Create a class constructor
Kotlin When no constructor is created, like Java, like Java, Kotlin generates a parameterless construct for it when the main function is not displayed.
/**
* Constructor constructor Kotlin Among them 1 Main constructors and multiple 2 Level constructor
* The main constructor is the 1 Section, following the class name
* In Java The constructor must be the same as the class name, while in the Kotlin In, it is through constructor Keyword to indicate
*/
class Student constructor(name: String, age: Int) {
private val username: String
private var age: Int
// In Kotlin Use in init Initialization code block, its function is for the main constructor service, because the main constructor is placed at the head of the class, it can't contain any initialization execution statement, this is the syntax, then this time there is init The place where it can be used,
// We can place the initialization execution statement here to assign values to properties
init {
this.username = name
this.age = age
}
}
// Writing style 2 : Can be constructor Keyword removal
class Student2(username: String, age: Int) {
private val username: String
private val age: Int
init {
this.username = username
this.age = age
}
}
// Writing style 3 Initialization statements are not required to be placed in init Code block, you can also initialize when defining attributes
class Student3(username: String, age: Int) {
private val username: String = username
private var age: Int = age
}
// Writing style 4 Defining the properties of the class directly in the constructor
class Student4(private val username: String,private val age: Int){}
// Sub-constructor , The difference with the main constructor is that the secondary constructor is defined in the class and can have more than one, but the main constructor will only have 1 A
class Users{
private val username:String
private val age:Int
constructor(username: String,age: Int){
this.username=username
this.age=age
}
}
4. Create abstract classes
Used to create base classes, encapsulate common methods, and handle common logic, and this so-called base class 1 is generally an abstract class.
Definition of Abstract Class in Kotlin
Abstract class can be understood as a template defined by the class. All subclasses are populated with their own code according to this template.
Keyword: abstract
Abstraction can be divided into abstract classes, abstract functions and abstract attributes. The difference between an abstract class and an ordinary class is that an abstract class can not only have its own attributes, constructors, methods and other components, but also contain abstract functions and abstract attributes.
Definition of abstract class and concrete implementation class
class TestAbstractA :Lanauge(){
override var name: String="Kotlin"
override fun init() {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
println(" I am ${name}")
}
}
class TestAbstractB :Lanauge(){
override var name: String
get() = "Java"
set(value) {}
override fun init() {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
println(" I am ${name}")
}
}
fun main(args: Array<String>) {
val ta=TestAbstractA()
val tb=TestAbstractB()
println(ta.name)
println(tb.name)
ta.init()
tb.init()
}
Characteristics of abstract classes:
Abstract classes themselves have common class characteristics and components. However, it is worth noting that abstract classes cannot be instantiated directly
It abstracts that subclasses of the class must all override abstract-decorated properties and methods.
Abstract members have only definitions, not implementations. Are all modified by abstract modifiers.
An abstract class defines a template for its subclasses. The difference is that the class implements different functions
*** Abstract classes can also inherit from 1 inherited class
// Use open This class declares that there are open Property that can be inherited by other classes
open class Base {
open fun init() {}
}
// Abstract classes can also inherit from 1 Inheriting classes
abstract class Lanauge2 : Base() {
override fun init() {
super.init()
}
abstract class Name() {} // Nesting of abstract classes
}
5. Nested classes
/**
* Nested class
*/
class Other {
val numOuther = 1
class Nested{
fun init(){
println(" Executed init Method ")
}
}
}
// Use of nested classes
Other.Nested().init() // The invocation format is: external class . Nested class (). Nested class Method / Attribute
6. Nested inner classes
// Nested inner class
class Othert{
val mumOther=1
inner class InnerClass{
val name="InnerClass"
fun init(){
println(" I am an inner class ")
}
}
}
Called in the main function
Othert().InnerClass().init() // The invocation format is: external class (). Inner class (). Internal class method / Attribute
7. Definition of anonymous inner classes
fun main(args: Array<String>) {
// Testing inner classes
val inner=Inner()
inner.setOnClickListener(object : OnClickListener{
override fun onItemClick(str: String) {
println(str)
}
})
inner.testListener()
}
class Inner{
//lateini( Delayed loading ) Modify only , Non kotlin Basic type
// Because Kotlin Will use null To treat every 1 Personal use lateinit Modified properties are initialized, while the underlying type is not null Type, you cannot use the lateinit .
lateinit private var listener:OnClickListener
fun setOnClickListener(listener: OnClickListener){
this.listener=listener
}
fun testListener(){
listener.onItemClick(" I am a test method for anonymous inner classes ")
}
}
interface OnClickListener{
fun onItemClick(str:String)
}
8. Define local classes
class Local{ // External class
val numOther = 1
fun partMethod(){
var name : String = "partMethod"
class Part{
var numPart : Int = 2
fun test(){
name = "test"
numPart = 5
println(" I am a method in a local class ")
}
}
val part = Part()
println("name = $name \t numPart = " + part.numPart + "\t numOther = numOther")
part.test()
println("name = $name \t numPart = " + part.numPart + "\t numOther = numOther")
}
}
fun main(args: Array<String>) {
// Test local class
Local().partMethod()
}