[英]Getter/Setter Problem in Java?
I want to pass the String value between the classes both are same package. 我想在两个类之间传递String值,它们都是同一包。 so i created the the classs like the code:
所以我创建了类似于代码的类:
public class Map_Delegate {
String myLatitude;
String myLongitude;
String myName;
private String TAG = "Map_Delegate";
public String getMyName() {
return this.myName;
}
public void setMyName(String value) {
Log.v(TAG, value);
this.myName = value;
}
public String getMyLatitude() {
return this.myLatitude;
}
public void setMyLatitude(String value) {
Log.v(TAG, value);
this.myLatitude = value;
}
public String getMyLongitude() {
return this.myLongitude;
}
public void setMyLongitude(String value) {
Log.v(TAG, value);
this.myLongitude = value;
}
}
But it can't pass the value. 但是它不能传递值。 I done like this code to set the value:
我喜欢这样的代码来设置值:
Map_Delegate map = new Map_Delegate();
map.setMyName(first_name_temp + " " + last_name_temp);
map.setMyLatitude(homeLatitude_temp);
map.setMyLongitude(homeLongitude_temp);
code to get the value: 获取值的代码:
Map_Delegate map = new Map_Delegate();
name_val = getMyName();
lat_val = getMyLatitude();
long_val = getMyLongitude();
Why get the Null value can you Guess? 为什么您可以猜到Null值? All classes in the same package and public .AnyIdea?
所有类都在同一个包中并与public .AnyIdea?
Well, you set the values on one instance of Map_Delegate
. 好了,您可以在一个
Map_Delegate
实例上设置值。 Later you make a new, blank instance and retrieve its values. 稍后, 您将创建一个新的空白实例并检索其值。 Of course they are null.
当然,它们为空。 You need a reference to the same object you filled in earlier, not make a new one.
您需要引用之前填充的同一对象,而不是创建新对象。
I'm not sure I entirely understand what you are doing, but from what you wrote it looks like you are creating a new Map_Delegate each time. 我不确定我是否完全了解您在做什么,但是从您写的内容来看,您似乎每次都在创建一个新的Map_Delegate。 So you create one object and set the values then you create another object and try to get the values (but by creating a new object the values are initialised to null).
因此,您创建一个对象并设置值,然后创建另一个对象并尝试获取值(但是通过创建一个新对象,值将初始化为null)。 You should be calling get on the object you first created, which requires holding a reference to it and passing it around to wherever it is needed.
您应该在第一次创建的对象上调用get,这需要持有对该对象的引用并将其传递到需要的任何地方。
Alternatively it might be that you are trying to implement the Singleton design pattern. 或者,您可能正在尝试实现Singleton设计模式。 Using the Singleton design pattern means that there will only ever be one instance of a given class so you don't need to pass the reference around you can just get it by calling getInstance again.
使用Singleton设计模式意味着给定类将永远只有一个实例,因此您不需要传递引用,只需再次调用getInstance即可获取该引用。 Basically to achieve singleton for your case you would do:
基本上要为您的案例实现单例,您可以执行以下操作:
public class Map_Delegate {
private static Map_Delegate instance;
private Map_Delegate() {
// Private constructor so nobody can create an instance of your class.
}
public static Map_Delegate getInstance() {
if (instance == null) {
instance = new Map_Delegate();
}
return instance;
}
// All the rest of your code can go here.
}
Now you can do: 现在您可以执行以下操作:
Map_Delegate a = Map_Delegate.getInstance();
a.setMyName("Name");
// Whatever else.
Then later on you can do: 然后,您可以执行以下操作:
Map_Delegate b = Map_Delegate.getInstance();
String name = b.getMyName();
and name won't be null becuase you are using the same instance from before. 并且名称不会为空,因为您使用的是之前的相同实例。
The reason for the null values that you are creating two map_delegate instances. 您正在创建两个map_delegate实例的空值的原因。 A new instnace has null values for the properties (unless you specify a default value - not the case here.) You set the values on the first instance, but then retrieve from second - the second instance is newly constructed with null values.
一个新的实例的属性具有空值(除非您指定默认值-此处不是这种情况。)您在第一个实例上设置了值,然后从第二个实例检索-第二个实例是使用空值重新构造的。
I would avoid the singleton pattern. 我会避免使用单例模式。 Instead, ensure that your code passes the same instance to the parts that need them.
相反,请确保您的代码将同一实例传递给需要它们的部分。 Consider what you want do with the map_delegate, and then add a way to pass in the appropriate delegate - usually as a property or a constructor to the collaborator.
考虑您想对map_delegate做些什么,然后添加一种方法来传递适当的委托-通常作为属性或构造函数传递给协作者。
Map_Delegate map = new Map_Delegate();
map.setMyName(first_name_temp + " " + last_name_temp);
map.setMyLatitude(homeLatitude_temp);
map.setMyLongitude(homeLongitude_temp);
String name_val;
String lat_val;
String long_val;
name_val = map.getMyName();//<-Not Null
lat_val = map.getMyLatitude();//<-Not Null
long_val = map.getMyLongitude();//<-Not Null
map = new new Map_Delegate();//Another memory area is newly generated.
name_val = map.getMyName();//<- Null
lat_val = map.getMyLatitude();//<- Null
long_val = map.getMyLongitude();//<- Null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.