标签:static 多参数 make 异步 而不是 额外 c# 对象创建 books
class Thermosiphon
@Inject constructor(
private val heater: Heater
) : Pump {
// ……
}
@Module
abstract class PumpModule {
@Binds
abstract fun providePump(pump: Thermosiphon): Pump
}
@Module(includes = arrayOf(PumpModule::class))
class DripCoffeeModule {
@Provides @Singleton
fun provideHeater(): Heater = ElectricHeater()
}
@Singleton
@Component(modules = arrayOf(DripCoffeeModule::class))
interface CoffeeShop {
fun maker(): CoffeeMaker
}
fun main(args: Array<String>) {
val coffee = DaggerCoffeeShop.builder().build()
coffee.maker().brew()
}
dependencies {
...
annotationProcessor "com.google.dagger:dagger-compiler:$dagger-version"
}
apply plugin: ‘kotlin-kapt‘
dependencies {
...
kapt "com.google.dagger:dagger-compiler:$dagger-version"
}
apply plugin: ‘kotlin-kapt‘
dependencies {
...
compile "com.jakewharton:butterknife:$butterknife-version"
kapt "com.jakewharton:butterknife-compiler:$butterknife-version"
}
@BindView(R2.id.title) TextView title;
@BindView(R2.id.title)
lateinit var title: TextView
@OnClick(R2.id.hello)
internal fun sayHello() {
Toast.makeText(this, "Hello, views!", LENGTH_SHORT).show()
}
hello.setOnClickListener {
toast("Hello, views!")
}
android {
...
dataBinding {
enabled = true
}
}
apply plugin: ‘kotlin-kapt‘
dependencies {
kapt "com.android.databinding:compiler:$android_plugin_version"
}
<data>
<variable name="data" type="org.example.kotlin.databinding.WeatherData"/>
</data>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@{data.imageUrl}"
android:contentDescription="@string/image" />
class MainActivity : AppCompatActivity() {
// ……
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding =
DataBindingUtil.setContentView(this, R.layout.activity_main)binding.data = weather
//等同于
binding.setData(weather)}
}
<Button
android:text="@string/next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="startOtherActivity" />
class MainActivity : AppCompatActivity() {
// ……
fun startOtherActivity(view: View) = startActivity<OtherActivity>()
}
startActivity<OtherActivity>("KEY" to "VALUE").
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> presenter.onSaveClick(task)}" />
// 用 Kotlin 代码写的相同逻辑
button.setOnClickListener { presenter.onSaveClick(task) }
apply plugin: ‘kotlin-kapt‘
dependencies {
kapt "com.github.raizlabs.dbflow:dbflow-processor:$dbflow_version"
compile "com.github.raizlabs.dbflow:dbflow-core:$dbflow_version"
compile "com.github.raizlabs.dbflow:dbflow:$dbflow_version"
}
@Table(name="users", database = AppDatabase::class)
class User: BaseModel() {
@PrimaryKey(autoincrement = true)
@Column(name = "id")
var id: Long = 0
@Column
var name: String? = null
}
@Table(database = KotlinDatabase::class)
data class User(@PrimaryKey var id: Long = 0, @Column var name: String? = null)
dependencies {
compile "com.github.raizlabs.dbflow:dbflow-kotlinextensions:$dbflow_version"
}
apply plugin: ‘kotlin-kapt‘
dependencies {
...
kapt "frankiesardo:auto-parcel:$latest-version"
}
@AutoValue
abstract class Address : Parcelable {
abstract fun coordinates(): DoubleArray
abstract fun cityName(): String
companion object {
fun create(coordinates: DoubleArray, cityName: String): Address {
return builder().coordinates(coordinates).cityName(cityName).build()
}
fun builder(): Builder = `$AutoValue_Address`.Builder()
}
@AutoValue.Builder
interface Builder {
fun coordinates(x: DoubleArray): Builder
fun cityName(x: String): Builder
fun build(): Address
}
}
由于 Kotlin 中没有 static 方法,因此相应的方法会在 companion object中生成。 如果仍然需要从 Java 中调用这些方法,需要添加@JvmStatic注解。
标签:static 多参数 make 异步 而不是 额外 c# 对象创建 books
原文地址:http://www.cnblogs.com/baiqiantao/p/744da998733677d2549bdb4a275ff1c1.html