当前位置:网站首页>Kotlin learning II: classes and objects
Kotlin learning II: classes and objects
2022-07-22 20:07:00 【Wu Qingsen】
class
Create a class MyUtils, There is a value and the method of obtaining :
class MyUtils {
var a = 0
fun get(): Int {
return a
}
}
stay MainActivity Class to get a And assign :
val myUtils = MyUtils()
Log.w("MyUtils", "" + myUtils.get())
myUtils.a = 9527
Log.w("MyUtils", "" + myUtils.get())
Print the results :
MyUtils: 0
MyUtils: 9527
Inherit
When you create a new project, you will find that the inheritance relationship is represented by a colon :
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
MainActivity Inherit AppCompatActivity , Remember that there are parentheses after the parent class .
Write your own parent class and remember to add open keyword , Only in this way can it be inherited :
open class A {
}
B Inherit A:
class B : A() {
}
Constructors
kotlin Provides init Method , This is the main constructor , You can write some logic in it :
class B(var num: Int) : A() {
init {
num = num + 1
Log.w("num", "" + num)
}
}
establish B Class, give an initial value num:
val b = B(30)
take A Also add a parameter :
open class A(pos: Int) {
}
stay B You can pass a parameter to A:
class B(var num: Int) : A(num) {
init {
num = num + 1
Log.w("num", "" + num)
}
}
边栏推荐
- Docker - DB2 database deployment through container installation tutorial
- nacos权限与数据库
- Decimal operation in shell (BC)
- Foundation type
- 最佳实践|用腾讯云AI文字识别实现企业资质证书识别
- [database basic dry goods] MySQL foundation and slow query optimization practice
- The difference between location.replace and location.href
- 深入浅出ES6(四):模板字符串
- NFT挖矿分红系统开发模式定制
- 人和产品的五个层次
猜你喜欢
Creation of sparksql dataset
Datablau5.0 data asset management product series heavy release
Oracle 11g installs and starts EM based on centos7
Spark: graph
ping: www.baidu.com: 未知的名称或服务原因分析
Spark loads CSV and JSON files (attached to the virtual machine to execute the jar package)
Vs Code common shortcut keys
NFT卡牌链游系统Dapp开发搭建
Pregel function in spark graphx (Reprint)
plsql不能初始化
随机推荐
Insert, modify and delete table data of MySQL
leetcode 22. 括号生成
When the appsstore transporter uploads, it is stuck in the process of verifying the problem
Kotlin学习三:页面的常用写法
【读书笔记】《微习惯:瘦身篇》
Spark RDD算子:分区操作,mapPartitions和mapPartitionsWIthIndex
shell script “<< EOF”我的用途和遇到的问题
Daily work specifications
NC26 括号生成
AttributeError: module ‘tensorflow.keras.utils‘ has no attribute image_dataset_from_directory——解决方法
Flutter development (30): simple app building of flutter
Spark RDD operator: RDD partition, hashpartition, rangepartition, custom partition
SparkSQL Dataset的创建
ld: framework not found Pods_XXX clang: error: linker command failed with exit code 1 (use -v to see
ES6 usage
Flutter 2进阶(六):回调函数的使用
ES6 new features sharing (III)
leetcode 32. 最长有效括号
Vs Code common shortcut keys
人和产品的五个层次