interface Study {//接口+interface
fun readbooks()
fun dohomework(){
println("do homework default implementation")//在接口中加函数体的内容,代表着dohomework这个函数默认实现,
//就是说在student继承哪里,可以不用override定义接口函数,不会报错,但readbook不行,因为没有加函数体
}
}
class Student (name :String,age :Int):person(name,age),Study{//接口加继承的函数,person是类的继承,Study是接口
override fun readbooks() {//override接口调用关键字,实现接口中的函数
println(name+"is reading")
}
/*override fun dohomework() {
println(name+"is doing homework.")
}*/
}
/*fun main() {
//val a =Student("123654",5,"nie",18)
//val a=Student()
//val a=Student("nie",18)
val a=Student("niw",5)
//a.name="nie"
//a.age=18
println(a.name+" is eating,he is "+a.age+" years old,他是 ")//+a.*grade+"年级的学生,学号是"+a.sno)
}*/
fun main(){
val student=Student("jack",19)
//doStudy(student)
student.readbooks()
student.dohomework()
}
/*fun doStudy(study: Study){
study.readbooks()
study.dohomework()
}*/
package com.example.myapplicationone
data class Cellphone (val brand:String,val price:Double)//定义数据类方法,关键字data,kotlin强大的推理能力,
// 只需要data关键字就可以实现数据类的定义,
//不需要其他的equals,hashcode,tostring
fun main(){
val cellphone1=Cellphone("samsung",1299.33)
val cellphone2=Cellphone("samsung",1299.33)
println(cellphone1)
println("cellphone1=cellphone2 " +(cellphone1==cellphone2))
}
package com.example.myapplicationone
object Singleton {//单例类关键字“object“
fun singletonTest(){
println("singletonTest is called")
}
}
fun main(){
Singleton.singletonTest()//单例类调用方法
}