1. JAVA/4). 자바_실전_이클립스

자바실전_Day_19_01(추상메소드, 상속 - 제품출력하기)

구이제이 2024. 1. 25. 19:27

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



1번째 예제





●결과값

 

public abstract class Item {

 

//필드

String name; //이름

int price; //가격

 

 

 

 

public Item(String name, int price) {

super();

this.name = name;

this.price = price;

}

 

 

 

 

public abstract void printInfo();

 

}

 

 

 

public class Book extends Item {

 

//필드

private String author; //저자

private String publisher; //출판사

 

 

 

 

public Book(String name, int price, String author, String publisher) {

super(name, price);

this.author = author;

this.publisher = publisher;

}

 

 

 

 

@Override

public void printInfo() {

System.out.println("* 책 *");

System.out.println("이름 : " + super.name );

System.out.println("가격 : " + super.price );

System.out.println("저자 : " +this.author );

System.out.println("출판사" + this.publisher);

 

}

}

 

 

public class Cosmetics extends Item {

 

//필드

private String brand; //브랜드

private String cololr; //컬러

 

 

 

 

 

public Cosmetics(String name, int price, String brand, String cololr) {

super(name,price);

this.brand = brand;

this.cololr = cololr;

}

 

 

 

 

 

@Override

public void printInfo() {

System.out.println("* 화장품 *");

System.out.println("이름 : " + super.name );

System.out.println("가격 : " + super.price );

System.out.println("브랜드 : " +this.brand );

System.out.println("색상 : " + this.cololr);

 

 

}

 

}

 

 

public class Jeans extends Item {

//필드

private int size; //사이즈

 

 

 

 

public Jeans(String name, int price, int size) {

super(name, price);

this.size = size;

}

 

 

 

 

@Override

public void printInfo() {

System.out.println("* 청바지 *");

System.out.println("이름 : " + super.name );

System.out.println("가격 : " + super.price );

System.out.println("사이즈 : " +this.size );

 

}

}

 

 

 

import java.text.DecimalFormat;

import ex01_tea.Book;
import ex01_tea.Cosmetics;
import ex01_tea.Jeans;

public class webShopMain {

public static void main(String[] args) {


//1번 공통된 영역에 부모 클래스에 넣지 않았다.
//2번 private로 필드를 구성하고 getter로 값을 받아야 한다.
//3번 부모에다가 필드에 대한 getter을 사용했는데, 출력이 되었다.
//생성된 필드의 getter가 부모의 필드라서,호출을 요구하는 필드의gtter가 부모의 필드에 대한 getter만 필요했다.

//결국, 부모 객체의 필드와 자식의 필드를 구분하지 못하였다.
//부모는 자식을 통해서 필드의 값을 넘겨 받았다. 2개
//자식은 2개의 필드의 값을 받고 있었다.
//결국 호출을 요구하는 것은 부모의 필드이기에 부모에게만 getter를 사용하면 된다.
  int sum = 0;
      String nameTotal ="";
      
      Cosmetics cos = new Cosmetics("립스틱", 25000, "아모레퍼시픽","Red");
      Jeans jean = new Jeans("기모청바지", 35000, 77);
      Book book = new Book("JAVA", 35000, "홍길동", "중앙정보");
      
      cos.print();
      jean.print();
      book.print();
      System.out.println();
      
      System.out.println("=============================");
      //총합 구하기
      DecimalFormat f = new DecimalFormat("#,##0원"); //포맷 패턴
      
      sum = cos.getPrice() + jean.getPrice() + book.getPrice();
      System.out.println("오늘 판매 금액: " + f.format(sum));

      System.out.println("=============================");
      //제품의 이름 나열하기
      nameTotal = cos.getName() + " , " + jean.getName() + " , " + book.getName();
      System.out.println("판매 제품 이름 : " + nameTotal);
      
   }
}

 

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



1번째 예제 - 리팩토링(Teacher's 작품)





●결과값

 

 

 

public abstract class Item {

 

//#1. 필드 - 외부에 노출하지 않으려 함

private String name; //이름

private int price; //가격

 

 

 

//#2. 생성자 - 가격과 이름을 초기화 시키기

// 상속을 시킬 부모이기에 자식에서 생성자를 호출하면서

// name, price 넘겨 받을 수 있도록 처리

// 자식 - super(name, price);

public Item(String name, int price) {

this.name = name;

this.price = price;

}

//setter를 굳이 필요하지 않는다. 이미 생성자로 초기화 해주었다.

//그리고 다른 사람이 수정할수 있는 위험이 있기 때문에, setter를 쓰지않는다.

//그래서, getter만 생성해라.

 

//#3. 메소드 - private으로 설정된 필드의 값을 읽어 외부에 전달, getter

// private은 자신의 클래스 내에서는 자유롭게 사용하지만

// 외부에서는 사용할 수 없음

public String getName() {

return name;

}

 

public int getPrice() {

return price;

}

 

//#4. 출력용 메소드 - 전체가 아닌 부모인 Item이 가진 필드만 출력할 수 있는 프린트 메소드

public void print() {

System.out.println("이름 : " + this.name); //this.name

System.out.println("가격 : " + price); //this.price

};

 

//부모에서는 공통된 메소드, 필드를 가지고 있는 것이 좋다.

//상속은 다형성을 이용하고, 중복최소화 하기 위함.

}

 

 

public class Book extends Item {

 

//계산과 관련된 것들, private가 좋다.

//1. 필드 - 인스턴스필드, 되도록이면 외부 노출 되지 않도록 private으로 선언

private String author; //브랜드

private String publicher; //컬러

 

 

 

//#2. 생성자 - 부모 생성자를 호출, 부모를 초기화시킬 필드값과 자신을 초기화 시킬

// 필드값을 매개값으로 받을 수 있도록 설정

 

public Book(String name, int price, String author, String publicher) {

super(name,price);

this.author = author;

this.publicher = publicher;

}

 

 

 

 

//#3. 메소드 - 출력을 위한 메소드 필요

// 부모의 print()메소드 호출하여 자식의 출력 내용을 추가

 

 

 

 

 

 

@Override//재정의 - 부모의 것을 고쳐 쓴다

public void print() {

System.out.println("***************");

super.print();//부모 메소드 호출

System.out.println("저자 : " + author);

System.out.println("출판사 : " + publicher);

}

 

}

 

 

 

public class Cosmetics extends Item {

 

 

//계산과 관련된 것들, private가 좋다.

//1. 필드 - 인스턴스필드, 되도록이면 외부 노출 되지 않도록 private으로 선언

private String brand; //브랜드

private String color; //컬러

 

 

 

//#2. 생성자 - 부모 생성자를 호출, 부모를 초기화시킬 필드값과 자신을 초기화 시킬

// 필드값을 매개값으로 받을 수 있도록 설정

 

public Cosmetics(String name, int price, String brand, String cololr) {

super(name,price);

this.brand = brand;

this.color = cololr;

}

 

 

 

 

 

//#3. 메소드 - 출력을 위한 메소드 필요

// 부모의 print()메소드 호출하여 자식의 출력 내용을 추가

 

 

 

 

 

 

 

@Override//재정의 - 부모의 것을 고쳐 쓴다

public void print() {

System.out.println("***************");

super.print();//부모 메소드 호출

System.out.println("브랜드 : " + brand);

System.out.println("색상 : " + color);

}

 

 

 

 

 

 

 

}

 

 

public class Jeans extends Item{

//#1. 필드 - 인스턴스필드, 되도록이면 외부 노출 되지 않도록 private으로 선언

private int size;

 

//#2. 생성자 - 부모 생성자를 호출, 부모를 초기화시킬 필드값과 자신를 초기화 시킬

// 필드값을 매개값으로 받을 수 있도록 설정

public Jeans(String name, int price, int size) {

super(name, price);

this.size = size;

}

 

//#3. 메소드 - 출력을 위한 메소드 필요

// 부모의 print()메소드 호출하여 자식의 출력 내용을 추가

@Override //재정의 - 부모의 것을 고쳐 쓴다.

public void print() {

System.out.println("***************");

super.print();//부모 메소드 호출

System.out.println("사이즈 : " + size);

}

}

 

 

 

import java.text.DecimalFormat;

 

public class webShopMain {

 

public static void main(String[] args) {

int sum = 0;

String nameTotal ="";

 

Cosmetics cos = new Cosmetics("립스틱", 25000, "아모레퍼시픽","Red");

Jeans jean = new Jeans("기모청바지", 35000, 77);

Book book = new Book("JAVA", 35000, "홍길동", "중앙정보");

 

cos.print();

jean.print();

book.print();

System.out.println();

 

System.out.println("=============================");

//총합 구하기

DecimalFormat f = new DecimalFormat("#,##0원"); //포맷 패턴

 

sum = cos.getPrice() + jean.getPrice() + book.getPrice();

System.out.println("오늘 판매 금액: " + f.format(sum));

 

System.out.println("=============================");

//제품의 이름 나열하기

nameTotal = cos.getName() + " , " + jean.getName() + " , " + book.getName();

System.out.println("판매 제품 이름 : " + nameTotal);

 

}

}