第一步,写kotlin代码:
class MainActivity : AppCompatActivity() {
companion object {
const val REQUEST_CODE_TAKE_PHOTO = 101
const val REQUEST_CODE_PICK_PHOTO = 102
}
...
第二步,转成Java代码: tools->Kotlin->show Kotlin Bytecode可以查看kotlin代码的字节码 然后点击字节码右上角的Decompile可以将字节码转为java代码,如图: 第三步,查看java代码:
public final class MainActivity extends AppCompatActivity {
private List pictures;
private BaseQuickAdapter pictureAdapter;
private Uri currentPictureUri;
private File currentPictureFile;
private final Set pictureAddressSet;
public static final int REQUEST_CODE_TAKE_PHOTO = 101;
public static final int REQUEST_CODE_PICK_PHOTO = 102;
@NotNull
public static final MainActivity.Companion Companion = new MainActivity.Companion((DefaultConstructorMarker)null);
private HashMap _$_findViewCache;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(1300075);
this.initListener();
...
第四步,得出结论 1.companion object的作用 使用companion object会在原类中生成一个内部类:public static final class Companion,同时在原类中创建这个内部类的对象(这么看,伴生对象是不是就理解了)
|