[英]Is it possible to extends a class that has a non empty constructor with a companion object
[英]Is it possible to create a Parcelable Class that has an empty constructor?
我正在嘗試使用帶有可打包 class 的 Firestore 回收器適配器,但它需要有一個空的構造函數。
我現在的解決方案是創建一個帶有空構造函數的常規 class 並在獲取數據后立即將 map 對象放入可打包的副本中。
但是是否可以使用空構造函數創建 Parcelable Class ? 在 Android Studio 中,當我右鍵單擊 -> 生成 -> 我沒有看到輔助構造函數選項,所以我想這是不可能的,對吧?
是的,這是可能的。 Parcelable object 將被序列化和反序列化沒有任何問題。
在 Android Studio 中,當我右鍵單擊 -> 生成 -> 我沒有看到輔助構造函數選項,所以我想這是不可能的,對吧?
不,它在 Android Studio 代碼完成功能中沒有作為建議出現這一事實並不意味着它不可能。
以Android 文檔中的 Parcelable 實現作為參考。 然后,您應該需要添加一個空的構造函數。 只寫代碼,不要使用代碼生成器。
public MyParcelable(){
}
class 應如下所示:
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
public MyParcelable(){
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.