我在一篇文章中找到了这个。它实现了Parcelable,用于在Android
中的活动之间传递数据public class Student implements Parcelable {
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Student createFromParcel(Parcel in) {
return new Student(in);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
private long id;
private String name;
private String grade;
// Constructor
public Student(long id, String name, String grade){
this.id = id;
this.name = name;
this.grade = grade;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
// Parcelling part
public Student(Parcel in){
this.id = in.readLong();
this.name = in.readString();
this.grade = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(this.id);
dest.writeString(this.name);
dest.writeString(this.grade);
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", grade='" + grade + '\'' +
'}';
}
}
在此示例中,声明了一个字段CREATOR并实现了Parcelable.Creator Interface.This是一个匿名类。这是否意味着匿名类也可以创建为类的成员?我从其他来源获悉,匿名类不能是静态的,但在这里它被声明为静态。我不明白这个例子中匿名类的上下文。有人解释一下吗?
答案 0 :(得分:1)