给儿砸的第一个儿童节礼物
//类名:WeatherinfoModel
//主构造函数:constructor(val weatherinfo: WeatherinfoBean)
class WeatherinfoModel public constructor(val weatherinfo: WeatherinfoBean) { //类体 init { //主构造函数不能包含任何的代码, //初始化的代码可以放到 init 块中 }
}
class Person { constructor(parent: Person) { parent.children.add(this) }
}
val person = Person()
open class Base { open fun v() {
}
}
class Child : Base { override fun v() {
}
}
class MyView : View { constructor(ctx: Context) : super(ctx) constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
}
class ApiCallback<M>(m:M){}
fun <M> addSubscription(observable: Observable<M>, subscriber: Subscriber<M>) {
// ……
}
abstract class ApiCallback<M> { abstract fun onSuccess(model: M) abstract fun onFailure(msg: String?) abstract fun onFinish()
}
class A { val a: String = "a" class B { val b: String = "b" }
}
val a = A.B().b//a输出为“b”
class A { val a: String = "a" inner class B { val b: String = a }
}
val a = A().B().b//a输出为“a”
ApiClient.retrofit().loadData("101190201"), object : ApiCallback<WeatherinfoModel>() { override fun onSuccess(model: WeatherinfoModel) { //…… } override fun onFailure(msg: String?) { //…… } override fun onFinish() { //…… }}
object ApiClient { fun retrofit(): ApiStores { //…… return retrofit.create(ApiStores::class.java) }
}
ApiClient.retrofit()
class ApiStores { companion object A { //baseUrl val API_SERVER_URL = "http://www.weather.com.cn/" }
}
val url = ApiStores.API_SERVER_URL
data class WeatherinfoModel constructor(val weatherinfo: WeatherinfoBean) { data class WeatherinfoBean( val city: String, val cityid: String )
}
override fun onSuccess(model: WeatherinfoModel) { Log.d("wxl",model.toString())//输出“WeatherinfoModel(weatherinfo=WeatherinfoBean(city=无锡, cityid=101190201))” Log.d("wxl", "city=" + model.weatherinfo.city + ",cityid=" + model.weatherinfo.cityid)//输出“city=无锡,cityid=101190201” val weatherinfoBean = model.weatherinfo.copy(city = "上海") val weatherinfoModel = WeatherinfoModel(weatherinfoBean) Log.d("wxl", "city1=" + weatherinfoModel.weatherinfo.city + ",cityid1=" + weatherinfoModel.weatherinfo.cityid)//输出“city1=上海,cityid1=101190201”
}
interface Base { fun print()
}
class A(val a: Int) : Base { override fun print() { Log.d("wxl", "a=" + a) }
}
class B (val base: Base):Base by base
val a = A(1)
Log.d("wxl", "a=" + B(a).print())