Java 从一个类访问另一个类的变量
现在,我想在另一个类中访问变量latitude和longitude。这是我需要访问这些变量的类。请注意:由于我得到了当前位置,所以在这个函数中正确设置了latitude和longitude的值(我无法在这里粘贴整个代码,因为这样做没有意义) 下面是我在类中编写的代码,我想在其中访问这些变量Java 从一个类访问另一个类的变量,java,android,class,variables,Java,Android,Class,Variables,现在,我想在另一个类中访问变量latitude和longitude。这是我需要访问这些变量的类。请注意:由于我得到了当前位置,所以在这个函数中正确设置了latitude和longitude的值(我无法在这里粘贴整个代码,因为这样做没有意义) 下面是我在类中编写的代码,我想在其中访问这些变量 public class Page3 extends Activity { double latitude; double longitude; /** * Called when the activit
public class Page3 extends Activity {
double latitude;
double longitude;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page3);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
try {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
} catch (SecurityException e) {
e.printStackTrace();
}
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
double a = loc.getLatitude();
latitude=a;
double b = loc.getLongitude();
longitude=b;
String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
String x = getCompleteAddressString(a, b);
TextView text = (TextView) findViewById(R.id.tv_address);
text.setText(x);
}
同样,我没有复制粘贴整个代码。此代码本身运行良好,但在修改后,它会发送消息“Help0.0”虽然根据我的说法,纬度值应该已更改为我的当前位置。请确实帮助我。您的问题基本上是在方法中创建一个实例:
public class Page2 extends Activity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
Button btn = (Button) findViewById(R.id.help);
Page3 a=new Page3();
final double lati=a.latitude;
double longi=a.longitude;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS("9740641023", "Help"+lati+"");
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
相反,您应该将其设置为该类的字段
而且,如果将该字段设置为公共静态字段,则其他类可以使用
LocationListener mLocListener = new MyLocationListener();
但这只是一种非常“暴力的方式”。所以,你可以用它来看看你是否能从那里取得进展;但问题是:直接从其他类访问静态字段是不好的做法;你应该避免
真正的教训是:这是非常基本的“java知识”。你现在应该从“安卓”退后一步;并且研究那些基本的Java东西(比如:“访问其他对象中的信息的合理方法是什么”)。否则,你会撞上一堵又一堵墙
然后,当你了解了这些基本知识;然后你再看看有关Android的书籍/教程,这些书/教程向你解释了“Android世界”是如何运作的。因为Android有时会使用非常特殊的方式来完成任务。在First.java类中将variavle声明为
公共静态双格子
现在,您可以先使用在任何类中获取该变量的值。lattitude
良好的数据抽象和封装允许类的客户端只查看类允许客户端查看的内容。类Page3中的数据成员(如变量纬度和经度)不应由其他类直接访问。您应该有公共getter(accessor)和setter(mutator)方法来限制对应该声明为私有的数据成员的“访问”
在Java中只能继承一个类,但可以实现任意多个接口。因此,在Page3类中不需要内部公共类MyLocationListener。只需使用implement关键字并重写接口的方法
LocationListener theListner = Classname.mLocListener;
现在,在第二个活动中使用公共方法访问数据成员
public class Page3 extends Activity implements LocationListener { // implement the interface instead of creating an inner class
private double latitude; // hide your data members from the client
private double longitude;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page3);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); // pass in this referring to the current class since implementing the interface LocationListener
} catch (SecurityException e) {
e.printStackTrace();
}
@Override
public void onLocationChanged(Location loc) {
setMyLatitude(loc.getLatitude()); // use mutator method to change value of your private data member
setMyLongitude(loc.getLongitude()); // use mutator method to change value of your private data member
String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
String x = getCompleteAddressString(a, b);
TextView text = (TextView) findViewById(R.id.tv_address);
text.setText(x);
}
public void setMyLatitude(double a) {
this.latitude = a;
}
public void setMyLongitude(double b) {
this.longitude = b;
}
public double getMyLatitude() {
return latitude;
}
public double getMyLongitude() {
return longitude;
}
}
}
这是为了良好的编程实践。在两个活动之间进行通信的侦听器可能是不需要获得初始化的双精度值0.0的侦听器。监听器应在Page2类中实现,并且数据成员应设置为Page3类中Page2中的监听器。监听器将有一些方法来传递所需的数据,或者告诉类Page2信息已以某种方式修改
public class Page2 extends Activity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
Button btn = (Button) findViewById(R.id.help);
Page3 a=new Page3();
final double lati=a.getMyLatitude();
double longi=a.getMyLongitude();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS("9740641023", "Help"+lati+"");
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
您还可以将经度和纬度直接传递到Page3中的“DataListener”方法“someMethod”,甚至不需要Page3类的getter和setter以及私有数据成员。提示:使用变量名,如a或b。。。也许可以为你节省5秒钟的打字时间;但为了理解他们的意思,每一位读者都要花费数分钟的时间。换句话说:使用有意义的名称。始终如此。据我从显示的代码片段中所了解,您正在“实例化一个新的Page3
”,但是,它的对象.property
a尚未准备好使用,您正在使用它。您的MyLocationListener
没有时间更新正确的变量。要么保存值,使用单例保存正确的值,要么在值准备好时仅使用该值。@GhostCat将在下次记住这一点。如果我将LocationListener设置为本质上的公共静态,那么我也必须将所有方法更改为静态。这是你想说的吗?当然不是。您可以从静态和“普通”方法访问静态字段,但不能从其他方法访问!谢谢你支持我的观点:在编写程序之前,你应该认真研究这些东西。否则,您将从一个问题运行到下一个问题!当然我知道,我以为你想让locationListener类成为静态的,如果我这样做了,因为静态类只能访问静态方法,所以我必须让所有其他方法都成为静态的。不管怎样,谢谢您CatationListener是一个界面。对于普通的类和接口,不存在“使静态化”的问题。你的话根本没有任何意义*我的位置对不起
public class Page2 extends Activity implements DataListener {
.......
@Override
public void someMethod() {
// do something with the data longitude and latitude as their values have changed
}
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
Button btn = (Button) findViewById(R.id.help);
Page3 a=new Page3();
a.setDataListener(this); // pass listener to the other class
/* code in Page2 */
}
public class Page3 extends Activity implements LocationListener {
private DataListener myListener;
/* code in Page3 */
@Override
public void onLocationChanged(Location loc) {
setMyLatitude(loc.getLatitude()); // use mutator method to change value of your private data member
setMyLongitude(loc.getLongitude()); // use mutator method to change value of your private data member
myListener.someMethod(); // call this method to inform the other class that information has changed
String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
String x = getCompleteAddressString(a, b);
TextView text = (TextView) findViewById(R.id.tv_address);
text.setText(x);
}
public void setDataListener(DataListener listener) {
this.myListener = listener;
}