Kotlin novice learning teaching course

Original interface code:
public @interface ActivityScoped {
}

Kotlin interface code:

annotation class ActivityScoped

Original implementation method:

@Override
void inject(DaggerApplication instance);
AppComponent.Builder application(Application application);
Kotlin implementation method:
override fun inject(instance: DaggerApplication)
 
 
fun application(application: Application): AppComponent.Builder

1. Powerful use: colon connection

2.void replaced with fun

Originally defined controls:

ImageView mPlayingUserAvatar;

Kotlin definition control:

internal var mPlayingUserAvatar: ImageView? = null

1.internal means definition domain

2. All variables need to add var

3. Powerful class variables: class

4.? The question mark indicates whether it is empty 

The original code defines static:

private static final int CACHE_STRATEGY_FAST = 1; 

Kotlin defines static:

companion object {
    var mHandler: Handler? = null

    private val CACHE_STRATEGY_FAST = 1  //极速
    private val CACHE_STRATEGY_SMOOTH = 2  //流畅
    private val CACHE_STRATEGY_AUTO = 3  //自动
    private val CACHE_TIME_FAST = 1.0f
    private val CACHE_TIME_SMOOTH = 5.0f

}

1. The static static flag is added to the companion object for singleton

2. All variables that define variable types are replaced with var

Original power method:

private Map<String, String> mUrlMap = new HashMap<>();

Kotlin strength method:

private val mUrlMap = HashMap<String, String>()

1. The direct variable is equal to the one to be strengthened

Original initialization variables:

private int mCacheStrategy;
private String mPlayUrl;

Kotlin initialization variables:

private var mCacheStrategy: Int = 0
private var mPlayUrl: String? = null

1. Add var in front 

2. Need to add string type? Good to judge whether it is empty and assign null

Originally write a method with a return value:

public int getDeviceId() {
    return mEntity.getId();
}

Kotlin writes a method with a return value:

val deviceId: Int
    get() = mEntity!!.id

1.val to indicate new. Both variables and methods need to be added

2. The method of the get type removes the lower case of the first letter of the get as the method name and gives the method name a colon and the return value type

3.get () = Add the data to be returned after

4. If you need to add logic to get (), you need to get () {Add it here and then return it with return}

5. When writing methods, public void can be replaced with fun

6. All types of values ​​can be defined with var when assigning

7.! ! It means that the first load can be empty, and you need to add it when calling the methods inside the instantiated class !!.


The original switch uses multi-condition judgment

switch (action) {
    case AppConstant.UP:
        mGamePresenter.up();
        break;
    case AppConstant.DOWN:
        mGamePresenter.down();
        break;
}
Kotlin replaced it with when:
when (action) {
    AppConstant.UP -> mGamePresenter!!.up()
    AppConstant.DOWN -> mGamePresenter!!.down()
  
}

1. !! means that the first load can be empty, and you need to add it when calling the methods inside the instantiated class !!.

2. No need to add at the end;

Comparison of comprehensive methods
public void onFinish() {
    CountDownTimer timer = mTimerMap.get(action);
    if (timer != null) {
        timer.cancel();
    }
}

Kotlin:

 fun onFinish() {
    val timer = mTimerMap[action]
    timer?.cancel()
}

1. public void can be replaced with fun

2. When assignment is needed, var can be directly converted to the corresponding one according to the later assignment type, so all assigned variables can be defined with var

3.? The question mark symbol corresponds to whether the timer is empty

4. The get method can be changed to square brackets instead of arrays


The original need to force the assignment code of the object:

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

Kotlin needs to force the assignment code of the object:

val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
1. Forced change to add as followed by the type to be forced


Original start thread:

new Thread(new Runnable() {
    @Override
    public void run() {
        while (showDanmaku) {
            int time = new Random().nextInt(300);
            
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}).start();

Kotlin开启线程:

Thread(Runnable {
    while (showDanmaku) {
        val time = Random().nextInt(300)
   
        try {
            Thread.sleep(time.toLong())
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }

    }
}).start()

1.不需要实例化new

2.不需要实现run()方法


后续还有偶











发布了20 篇原创文章 · 获赞 2 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_28335347/article/details/79611432