面向对象(中)-equals()练习1:代码实现
package top.qaqaq.java.P299;

public class OdrerTest {

	public static void main(String[] args) {

		Order order1 = new Order(1001, "AA");
		Order order2 = new Order(1001, "BB");

		System.out.println(order1.equals(order2));
		
		Order order3 = new Order(1001, "BB");
		System.out.println(order2.equals(order3));
		
//		String s1 = "BB";
//		String s2 = "BB";
//		System.out.println(s1 == s2);//true
		
	}
}

class Order {

	private int orderld;
	private String orderName;

	public Order(int orderld, String orderName) {
		super();
		this.orderld = orderld;
		this.orderName = orderName;
	}

	public int getOrderld() {
		return orderld;
	}

	public void setOrderld(int orderld) {
		this.orderld = orderld;
	}

	public String getOrderName() {
		return orderName;
	}

	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}

	@Override
	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		
		if(obj instanceof Order) {
			Order order = (Order)obj;
			//正确的:
			return this.orderld == order.orderld && 
					this.orderName.equals(order.orderName);
			//错误的:
//			return this.orderld == order.orderld && 
//					this.orderName == order.orderName;
		}
		
		return false;
	}
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇