'object클래스'에 해당되는 글 1건

  1. 2012.07.29 object클래스

* Object class
- java.lang.Object : 모든 class의 최상위->모든 객체의 type이 될 수 있다.

* Object class는 추상적이어서 여기 있는 메소드를 직접 사용하는게 중요한게 아니라 하위 클래스에서 오버라이딩 해서 사용하는 것이 중요하다.
* +toString() : String -> 객체(Object)를 String으로 변환
- 메서드 이름이 객체.toXXXX()로 되면
to -> 변환

ex :
ProductDTO p = new ProductDTO();
p.toString(); <- p객체를 String으로 바꿔라.
* class이름@16진수 "ProductDTO@a567.." 이런식으로 바꿔준다. @a567...은 hashCode(객체의 가상의주소)로 바꿔준다.
->Object 타입의 toString()은 사용하지 않는다. 중요한 것은 오버라이딩 해서 사용하는 것이 중요한 것이다.
->toString()을 오버라이딩 할 시에는 Attribute들을 문자열로 return해줌.
(대표적인 toString()오버라이드한 객체들 String, StringBuffer, WrapperClass, Collection)

*equals(obj : Object) : boolean
- 매개변수로 받은 객체가 같은 객체인지 비교
ex)  obj1.equals(obj2); -> obj1==obj2


- overriding : 두객체의 attribute의 값이 같다면 true 리턴

equals overriding
public boolean equals(Object obj){
 1. obj가 null인지
 2. obj가 같은지
 3. 속성값들이 같은지
}
- equals()메소드를 오버라이딩 할때 반드시 hashCode()메소드도 같이 overriding한다.
-> + int hashCode()도 오버로딩 해야함.

o1.equals(o2); ==> true 면
o1.hashCode() ==> attribute가 같으면 hashCode()도 같게 나오게 해야한다.
o2.hashCode() ==> 디 둘도 true가 나와야 같은 객체라 생각한다.

* 값을 비교할때 equals를 쓴다. 따라서 attribute가 같으면 같은 객체다. equals메소드가 true가 나오고 hashCode()값도 true가 나와야 같은 객체다.
(equals overriding하면 hashCode()도 오버라이딩 해라.

project : day22
package : object.method
class :  ProductDTO

 

package object.method;

public class ProductDTO {
 private String productId;
 private String productName;
 private int price;
 private String productMaker;
 
 //생성자
 public ProductDTO() {
 }
 public ProductDTO(String productId, String productName, int price,
   String productMaker) {
  this.productId = productId;
  this.productName = productName;
  this.price = price;
  this.productMaker = productMaker;
 }
 
 //getter/setter
 public String getProductId() {
  return productId;
 }
 public void setProductId(String productId) {
  this.productId = productId;
 }
 public String getProductName() {
  return productName;
 }
 public void setProductName(String productName) {
  this.productName = productName;
 }
 public int getPrice() {
  return price;
 }
 public void setPrice(int price) {
  this.price = price;
 }
 public String getProductMaker() {
  return productMaker;
 }
 public void setProductMaker(String productMaker) {
  this.productMaker = productMaker;
 }
 //toString()
 @Override
 public String toString() {
  return "ProductDTO [productId=" + productId + ", productName="
    + productName + ", price=" + price + ", productMaker="
    + productMaker + "]";
 }
 //equals(), hashCode()
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + price;
  result = prime * result
    + ((productId == null) ? 0 : productId.hashCode());
  result = prime * result
    + ((productMaker == null) ? 0 : productMaker.hashCode());
  result = prime * result
    + ((productName == null) ? 0 : productName.hashCode());
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  ProductDTO other = (ProductDTO) obj;
  if (price != other.price)
   return false;
  if (productId == null) {
   if (other.productId != null)
    return false;
  } else if (!productId.equals(other.productId))
   return false;
  if (productMaker == null) {
   if (other.productMaker != null)
    return false;
  } else if (!productMaker.equals(other.productMaker))
   return false;
  if (productName == null) {
   if (other.productName != null)
    return false;
  } else if (!productName.equals(other.productName))
   return false;
  return true;
 }
}


-----------------

package object.method;

import java.util.HashSet;

public class TestEquals {
 public static void main(String[] args) {
  ProductDTO pto1 = new ProductDTO("p-1","TV",2000,"LG");
  ProductDTO pto2 = new ProductDTO("p-1","TV",2000,"LG");
  System.out.println("pto1 == pto2 - >"+(pto1==pto2));//객체의 주소값 비교
  boolean flag = pto1.equals(pto2);
  System.out.println("pto1.equals(pto2) -> "+flag);
  HashSet set = new HashSet(); //중복허용 X
  set.add(pto1);
  set.add(pto2);
  System.out.println(set);//equals와 hashCode를 둘다 오버라이딩 해야 같은 객체로 판단한다.
 }
}

'프로그래밍 > JAVA프로그래밍' 카테고리의 다른 글

String class, String buffer  (0) 2012.07.29
boxing, unboxing, wrapperClass  (0) 2012.07.29
제너릭  (0) 2012.07.29
collection값 빼오기  (0) 2012.07.28
map실습  (0) 2012.07.28
Posted by 조은성
,