Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2019-11-25:Kotlin中可见型修饰符有哪些?相比于Java有什么区别? #196

Open
Moosphan opened this issue Nov 25, 2019 · 2 comments

Comments

@Moosphan
Copy link
Owner

No description provided.

@wedotang
Copy link

wedotang commented Sep 6, 2023

在Kotlin中,可见性修饰符用于控制类、接口、函数、属性和构造函数的可见性。Kotlin中的可见性修饰符有以下几种:
public:默认的可见性修饰符,表示对所有地方可见。
private:表示只在同一个文件中可见。
protected:表示只在同一个类或子类中可见。
internal:表示在同一个模块中可见。模块是指编译在一起的一组Kotlin文件。

相比于Java,Kotlin的可见性修饰符有以下几个区别:
Kotlin中的默认可见性是public,而Java中的默认可见性是包级可见性。
Kotlin中的private修饰符的作用范围是文件级别的,而Java中的private修饰符的作用范围是类级别的。
Kotlin中的protected修饰符只在同一个类或子类中可见,而Java中的protected修饰符在同一个包中也可见。
Kotlin中引入了internal修饰符,表示在同一个模块中可见,而Java中没有类似的修饰符。
这些可见性修饰符可以帮助开发者控制代码的可见性,提高代码的封装性和安全性。

FROM chatGPT

@AndroidJiang
Copy link

AndroidJiang commented Apr 12, 2024

修饰符 Java Kotlin
public 所有类可见 所有类可见(默认)
private 当前类可见 当前类可见
protected当前类、子类、同个包下的类可见 当前类、子类可见
default 同个包下的类可见(默认) 无
internal 无 同一模块的类可见

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants