为什么两个北京 字符串会不相等?
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, "北京" ); } } |
5
收起
正在回答
3回答
同学你好!
1.这里是adr对象中的address与"北京"比较,但是同学只是给add对象的address赋值了,并没有给adr对象中的address赋值,所以这里不相等呢
2.第二次同学给adr对象中的address赋值了,所以相等了
3.建议同学的包名不要使用中文呢,推荐使用英文~
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧