美文网首页
三、比较kotlin项目和java项目的区别

三、比较kotlin项目和java项目的区别

作者: 緦菍亭芷 | 来源:发表于2019-10-31 13:55 被阅读0次

    A.工程的根目录配置build.gradle

    buildscript {
       **ext.kotlin_version = '1.3.50'**
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.5.0'
          **classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"**
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    

    B.moudle中配置build.gradle

    apply plugin: 'com.android.application'
    **apply plugin: 'kotlin-android'**
    **apply plugin: 'kotlin-android-extensions'**
    
    ....
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
      **implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"**
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.core:core-ktx:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
        implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.2.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    }
    

    C.比较Activity

    kotlin 代码

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.main_activity)
            if (savedInstanceState == null) {
                supportFragmentManager.beginTransaction()
                    .replace(R.id.container, MainFragment.newInstance())
                    .commitNow()
            }
        }
    }
    

    java 代码

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, MainFragment.newInstance())
                        .commitNow();
            }
        }
    }
    

    D.比较Fragment

    kotlin 代码

    class MainFragment : Fragment() {
        companion object {
            fun newInstance() = MainFragment()
        }
    
        private lateinit var viewModel: MainViewModel
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View {
            return inflater.inflate(R.layout.main_fragment, container, false)
        }
    
        override fun onActivityCreated(savedInstanceState: Bundle?) {
            super.onActivityCreated(savedInstanceState)
            viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
            // TODO: Use the ViewModel
        }
    }
    

    java 代码

    public class MainFragment extends Fragment {
        private MainViewModel mViewModel;
    
        public static MainFragment newInstance() {
            return new MainFragment();
        }
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.main_fragment, container, false);
        }
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
            // TODO: Use the ViewModel
        }
    }
    

    区别:

    1. 类的修饰符
    2. 继承方式
    3. 方法修饰符
    4. 方法参数(onCreate)
    5. 一行代码结束(;)
    6. 单例的定义(MainFragment.newInstance())
    7. 方法返回值(onCreateView方法)

    相关文章

      网友评论

          本文标题:三、比较kotlin项目和java项目的区别

          本文链接:https://www.haomeiwen.com/subject/yfizvctx.html