[英]Variable and access in anonymous inner-class
I have some question about anonymous inner-class. 我对匿名内部类有一些疑问。 I just realize that in my anonymous inner-class can refer to two variable with the same name to outter class and in ineer-class it self, How do I know that what it refer to at that moment. 我只是意识到,在我的匿名内部类中,外部类可以引用两个具有相同名称的变量,而在内部类中可以将其自身引用为一个变量,我怎么知道它当时所指的是什么。
This is my code. 这是我的代码。
public class RealmUpdate {
private Realm realm;
public void upsertUserProfile( final String email, final String fname, final String lname,
final String gender, final String birthdate, final String tel){
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(@NonNull Realm realm){
try{
realm = Realm.getDefaultInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String[] arr_date = birthdate.split("/");
String real_date = arr_date[2] + "-" + arr_date[1] + "-" + arr_date[0];
Log.d( getClass().getSimpleName(), real_date);
Date date = format.parse(real_date);
UserProfile data = realm.where(UserProfile.class)
.equalTo("Email", email)
.findFirst();
if( data == null){
Log.d(RealmUpdate.class.getSimpleName(), "No account in DB.");
UserProfile userProfile = realm.createObject(UserProfile.class, email);
Log.d( getClass().getSimpleName(), "Value add: Start!?");
userProfile.setFName(fname);
Log.d( getClass().getSimpleName(), "Value add: fName!?");
userProfile.setLName(lname);
Log.d( getClass().getSimpleName(), "Value add: lName!?");
userProfile.setGender(gender);
userProfile.setBirthdate(date);
userProfile.setTel(tel);
Log.d( getClass().getSimpleName(), "Value add!?");
realm.close();
return;
}
Log.d(RealmUpdate.class.getSimpleName(), "Found account in DB");
data.setEmail(email);
data.setFName(fname);
data.setLName(lname);
data.setGender(gender);
data.setBirthdate(date);
data.setTel(tel);
realm.close();
} catch(ParseException exception) {
realm.close();
exception.printStackTrace();
Log.e( getClass().getSimpleName(), "Catch Error!!!");
}
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Log.d( getClass().getSimpleName(), "Add profile success");
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
realm.close();
Log.d( getClass().getSimpleName(), "Add profile not success");
}
});
realm.close();
}
From my code, I have 2 Realm object that name "realm" the first is declare in main class and others one is declare in anonymous inner-class. 从我的代码中,我有2个Realm对象,名称为“ realm”,第一个在主类中声明,其他一个在匿名内部类中声明。 When I try to change the name Realm object of main class to 当我尝试将主类的名称Realm对象更改为
private Realm realm_test;
it only show the error at 它只在显示错误
realm.executeTransactionAsync(new Realm.Transaction() {
, the code in anonymous inner-class still fine. ,匿名内部类中的代码仍然可以。 But when I change the name of variable in anonymous inner class like this, 但是当我像这样在匿名内部类中更改变量的名称时,
public void execute(@NonNull Realm realm_test_inner){
the code in anonymous inner-class got no error too. 匿名内部类中的代码也没有错误。
Now, I'm very confuse with these because it cause the error when I insert data to database(can fixed it now), but I'm still not sure what does it really happen? 现在,我对它们感到非常困惑,因为当我向数据库中插入数据时,它会导致错误(现在可以修复),但是我仍然不确定它到底会发生什么?
This feature is named shadowing. 此功能称为阴影。 You can read about it here: 你可以在这里读到它:
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.htm https://docs.oracle.com/javase/tutorial/java/javaOO/nested.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.