android - 如果我们对 Android Views 使用私有(private)访问修饰符有什么坏处吗?

标签 android access-modifiers

例如,下面的代码对吗?

private LinearLayout layout1;
private LinearLayout layout2;
private LinearLayout layout3;

最佳答案

不,它没有害处。但是,如果您正在使用某种 View 注入(inject)库,例如 ButterKnife,它不会让您将 View 定义为 private。原因如下:

The reason that Butter Knife requires views not be private is that it actually generates code which sets the fields. The code that it generates lives in the same package as your class which is why the field must be package-private, protected, or public. If the field was private the generated code would fail to compile since it cannot access the private field.

来源:Butterknife View injection

关于android - 如果我们对 Android Views 使用私有(private)访问修饰符有什么坏处吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40586817/

相关文章:

c# - 与私有(private)访问修饰符相反

java - Tomcat 8 无法通过反射访问 protected 成员

android - Firestore 只更新一个字段

android - 具有 Rails 后端的 Android/iOS 应用程序上的即时消息

java - SavedInstanceState Null fragment 之间为 null

c# - 如何使聚合根方法仅可用于领域事件,而无其他。

c# - 为什么抽象类的构造函数应该被保护,而不是公共(public)的?

C# 私有(private)、静态和只读

android - 日期/时间比 Android 晚 6 小时

android - Cordova 安卓支付插件