为什么两个北京 字符串会不相等?

为什么两个北京 字符串会不相等?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package 反射.Method;
 
public class Address {
    private int aId;
    private String name;
    private String address;
    private String phone;
    public Address(int aId, String name, String address, String phone) {
        super();
        this.aId = aId;
        this.name = name;
        this.address = address;
        this.phone = phone;
    }
    public Address() {
        super();
    }
     
    public int getaId() {
        return aId;
    }
    public void setaId(int aId) {
        this.aId = aId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    @Override
    public String toString() {
        return "Address [aId=" + aId + ", name=" + name + ", address=" + address + ", phone=" + phone + "]";
    }
    public void display(){
        System.out.println("这是一个地址");
    }
    private void info(){
        System.out.println("我是私有方法");
    }
    private void equalsAddress(String address){
        if(address.equals(this.address)) {
            System.out.println("相等");
        }else {
            System.out.println("不相等");
        }
         
    }
}
 
package 反射.Method;
 
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
public class AddressTest {
    public static void demo1() throws Exception {
        Class<?> class1=Class.forName("反射.Method.Address");
        Address adr=(Address)class1.newInstance();
        Method method=class1.getMethod("display");
        method.invoke(adr);
    }
    public static void demo2() throws Exception {
        Class<?> class1=Class.forName("反射.Method.Address");
        Address adr=(Address)class1.newInstance();
        Method method=class1.getDeclaredMethod("info");
        method.setAccessible(true);
        method.invoke(adr);
    }
 
    public static void main(String[] args) throws Exception  {
         
        demo1();
        demo2();
        Class<?> class1=Class.forName("反射.Method.Address");
        
        Address adr=(Address)class1.newInstance();
        Method method=class1.getDeclaredMethod("equalsAddress",String.class);
        method.setAccessible(true);
        Address add=new Address();
        add.setAddress("北京");
        method.invoke(adr,"北京");
 
 
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

3回答

同学你好!

1.这里是adr对象中的address与"北京"比较,但是同学只是给add对象的address赋值了,并没有给adr对象中的address赋值,所以这里不相等呢

http://img1.sycdn.imooc.com//climg/5dbcf4ad09e6842005760141.jpg

2.第二次同学给adr对象中的address赋值了,所以相等了

http://img1.sycdn.imooc.com//climg/5dbcf56409616f6805180187.jpg

3.建议同学的包名不要使用中文呢,推荐使用英文~

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

提问者 电磁护盾 2019-11-02 01:24:15

http://img1.sycdn.imooc.com//climg/5dbc6a29094309ea08090402.jpg

我这么写就相等了

  • 提问者 电磁护盾 #1
    但是为什么那么写,就不相等?
    2019-11-02 01:24:53
提问者 电磁护盾 2019-11-02 01:20:53
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码