set与get方法;调用get失败求大神解?

lilintong 2019-04-25 07:13:54
package net.xianrenwang.window;

class Location_get {
private String location_get;

// public Location_get(String location_get) {
// this.location_get = location_get;
// }

String getLocation_get() {
return location_get;
}

void setLocation_get(String location_get) {
this.location_get = location_get;
}
@Override
public String toString() {
return "location_get{" +
"location_get='" + location_get + '\'' +
'}';
}
}


我从另外一个类设置setLocation_get()的值,又从另外一类去获取getLocation_get()的值,可getLocation_get()获取不到数据,求大神?
...全文
2118 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lilintong 2019-05-01
  • 打赏
  • 举报
回复
引用 17 楼 qq_39994930的回复:
简单粗暴的讲,在你的变量前加上static修饰即可
为OK最终解决就是这样的,
qq_39994930 2019-04-30
  • 打赏
  • 举报
回复
简单粗暴的讲,在你的变量前加上static修饰即可
mr_foxsand 2019-04-28
  • 打赏
  • 举报
回复
1.代码中看到你得有参构造器注释掉了,这个是为了测试才这么写吗? 2.引用类型,每个创建出来的对象内存地址指向都不一样,你得代码截图没看错的话,你是创建了两个对象,第二个对象没有赋值,所以取到是null
Wbw Belief 2019-04-27
  • 打赏
  • 举报
回复
如果不是静态的,那么你就是使用了两个对象。
gxh_apologize 2019-04-27
  • 打赏
  • 举报
回复
你这个main方法不是入口吗。。。为什么还要在GetDemoA类来获取GetDemo类里面创建的对象的属性值。。。。你在GetDemoA创建Student对象就好了啊
Q125655211 2019-04-26
  • 打赏
  • 举报
回复
6666666666666666
lilintong 2019-04-26
  • 打赏
  • 举报
回复
class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

public class GetDemo {
public static void main(String[] args) {
Student st = new Student("tom",10);
String name = st.getName();
int age = st.getAge();
System.out.println(name+" "+age);
}


}

上面这种Set和get方法我会用,如果在GetDemo类里传值过去如下:
public class GetDemo {
public static void main(String[] args) {
Student st = new Student("tom",10);

}

那么我另外建一个GetDemoA类来获取GetDemo类里面传的值,应那么来获取?我这个GetDemoA类应那个写?
weixin_40343906 2019-04-26
  • 打赏
  • 举报
回复
get方法前面没有加访问修饰符,默认的访问修饰符不同的包下是访问不了的
lilintong 2019-04-26
  • 打赏
  • 举报
回复
谢谢
gxh_apologize 2019-04-26
  • 打赏
  • 举报
回复
引用 2 楼 lilintong 的回复:
我设置类的:new location_get(location_get);是这样传的值;
我获取:...setText(new location_get().getlocation_get()),可Tex得到的为null

你这种写法,是两个对象了。第二个对象里面的属性本来就没赋值,所以是null。
location_get loaction=new location_get();
loaction.setLocation_get(“hahah”);//赋值
String result=loaction.getlocation_get();//取值
setText(result);

还有,你这个命名有点......
fei得更高 2019-04-25
  • 打赏
  • 举报
回复
估计你是用了不同的对象了
jklwan 2019-04-25
  • 打赏
  • 举报
回复
先补补java知识,new Location_get ()是新对象了,不同的对象肯定取不到值的。 这样才行
Location_get  l1 = new Location_get(location_get);
l1.getlocation_get();// 取值
lilintong 2019-04-25
  • 打赏
  • 举报
回复
但是获取是在fragment的布局里面
lilintong 2019-04-25
  • 打赏
  • 举报
回复
我设置类的:new location_get(location_get);是这样传的值; 我获取:...setText(new location_get().getlocation_get()),可Tex得到的为null
usecf 2019-04-25
  • 打赏
  • 举报
回复
把你set和get值的代码贴出来
lilintong 2019-04-25
  • 打赏
  • 举报
回复
引用 4 楼 jklwan的回复:
先补补java知识,new Location_get ()是新对象了,不同的对象肯定取不到值的。 这样才行
Location_get  l1 = new Location_get(location_get);
l1.getlocation_get();// 取值
新手啊!能否在清楚点
雕·不懒惰 2019-04-25
  • 打赏
  • 举报
回复
set的跟get的Location_get不是同一个对象,肯定是去不到值的
要么把Location_get单例,或者在A里面set的时候public static Location_get lg=new Location_get ; lg.setlocation_get();
在B中get就是A.lg.getlocation_get()

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧