ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 13. Review
    개발자 수업/Java 2021. 10. 11. 12:56

    1. 왜 OOP 사용할까?
        1) 요구사항 변경에 유연하게 대처하고 기능 변경, 재사용을 쉽게 하기 위한 것

    2. 클래스(Class)란?
        1) 부류, 종류, 등급, ... 분류하다, 분류되다
        2) 템플릿(청사진)
        3) 속성과 기능을 묶는 것
            - 속성 : attribute, fields, value, property, ...
            - 동작 : operation, function, method, ...
        4) 개발자가 정의한 형식(Type)으로 개념일 뿐이며 객체를 통해서 메모리에 할당될 때 비로소 사용 가능해짐

    3. 클래스 선언 방법
        1) 클래스 선언 방법
            [가시성 지시자] class 클래스명 {
                // 필드들
                [가시성 지시자] 자료형 필드명;
                ...
                // 메서드들
                [가시성 지시자] 반환자료형 메서드명([매개변수들]) {
                    // 메서드 블록에 동작에 관한 내용 작성
                }
            }
        2) 사용 방법
            클래스명 객체명 = new 클래스명();

    public class Cat {
    	// Fields
    	String name;
    	int age;
    	
    	// methods
    	public void hunt() {
    		// do something ...
    	}
    	
    	public void run() {
    		// do something ...
    	}
    }



    4. 클래스와 객체(Object)
        1) 클래스
            - 객체를 만들어내기 위한 일종의 템플릿
            - 메모리에 존재하지 않는다
            - 보통 class 키워드를 사용해 정의
        2) 객체
            - 클래스로부터 만들어지며 메모리를 차지함
            - 최초에 만들어질 때 자신을 초기화하는 생성자(Constructor)를 가짐
        3) new 키워드
            - 객체가 생성되면 그 객체는 참조형이 되어 동적 메모리 공간(Heap)을 할당 받아 내용을 사용할 수 있음

    5. 생성자(Constructor)
        1) 객체 생성 시 
            - 속성(필드)에 기본값을 주고 객체를 생성하고 싶은 경우
            - 생성자의 이름은 클래스 이름과 같음
            - 생성자는 반환값을 사용하지 않음
            - 클래스로부터 인스턴스화된 객체의 기본 초기화

    6. 클래스 구성의 3가지 개념(원칙)
        1) 캡슐화(Encapsulation)
            - 정보를 하나로 묶어주는 것
                예) 클래스(속성과 동작을 묶어놓음)
        2) 정보 은폐(Information hiding)
            - 사용자가 알 필요 없는 정보를 감추기
        3) 추상화(Abstraction)
            - 시스템의 세부사항 중에서 중요한(관심 있는) 것과 중요하지 않은(관심 없는) 것을 가려내는 과정
            
    7. 캡슐화와 정보 은폐의 예
        1) - (private) : 나만 볼 수 있는 것
        2) # (protected) : 부모와 자식에게만 접근 가능
        3) + (public) : 누구나 접근 가능
        4) ~ (packege)

    8. 클래스의 상속 (Inheritance)
        1) 상위의 특징을 그대로 다시 재사용
        2) 상위의 속성이나 기능은 하위에서 새로운 것으로 대체될 수 있음 (Overriding)
        3) 하위 클래스에서는 새로운 속성이나 기능을 추가할 수 있음 (확장)
        4) this와 super
            - 현재 사용 기준 클래스는 this를 통해서 접근하고 기준 클래스의 상위는 super를 통해서 접근할 수 있음
            - this()와 super() : 해당 자신 혹은 상위 생성자를 호출할 수 있음

    9. 자바의 최상위 클래스
        1) Object 클래스
            - 명시적으로 Object 클래스를 상속하지 않아도 Java의 모든 클래스는 Object 클래스의 하위 클래스가 됨

    10. 자바 언어는 단일 상속만을 지원
        1) 프로그램의 복잡성을 피하기 위해
        2) 다중 상속과 같은 형태를 위해서 인터페이스 사용

     

    /*
     * 고객 아이디, 고객 이름,
     * 고객 등급 : 기본 생성자에서 기본 등급 silver
     * 보너스 포인트,
     * 보너스 포인트 적립 비율 : 기본 생성자에서 적립비율 1%
     */
    
    public class Customer {
    	protected int customerID;
    	protected String customerName;
    	protected String customerGrade;
    	int bonusPoint;
    	double bonusRatio;
    	
    	public Customer() {
    		customerGrade = "SILVER";
    		bonusRatio = 0.01;
    	}
    	
    	public int calcPrice(int price) {
    		bonusPoint += price * bonusRatio;
    		return price;
    	}
    
    	public int getCustomerID() {
    		return customerID;
    	}
    
    	public void setCustomerID(int customerID) {
    		this.customerID = customerID;
    	}
    
    	public String getCustomerName() {
    		return customerName;
    	}
    
    	public void setCustomerName(String customerName) {
    		this.customerName = customerName;
    	}
    
    	public String getCustomerGrade() {
    		return customerGrade;
    	}
    
    	public void setCustomerGrade(String customerGrade) {
    		this.customerGrade = customerGrade;
    	}
    	
    	public String showInfo() {
    		return this.getCustomerName() + "님의 등급은 " + this.getCustomerGrade() + "이며, 보너스 포인트는 " +  bonusPoint + "입니다.";
    	}
    }
    /*
     * 1) Customer클래스가 VIPCustomer에게 상속한다
     * VIPCustomer클래스가 Customer를 상속받는다
     * 
     * 2) 상위 클래스는 하위 클래스보다 일반적인 개념과 기능을 가짐
     * 	  하위 클래스는 상위 클래스보다 구체적인 개념과 기능을 가짐
     * 
     * 3) 고객의 등급에 따른 차별화된 서비스를 제공할 수 있음
     *    고객의 등급에 따라 할인율, 적립금이 다르게 적용됨
     *    
     * 4) 제품 구매 시 10% 할인
     *    보너스 포인트 5% 적립
     *    담당 상담원 배정
     */
    public class VIPCustomer extends Customer {
    	private int agentID;
    	double salesRatio;
    	
    	public VIPCustomer() {
    		// super();
    		this.customerGrade = "VIP";
    		this.bonusRatio = 0.05;
    		this.salesRatio = 0.1;
    	}
    
    	public int getAgentID() {
    		return agentID;
    	}
    	
    }
    /*
     * 일반 고객 1명과 VIP 고객 1명이 있다
     * 일반 고객 ~
     * VIP 고객 ~
     * 두 고객을 생성하고 해당 고객 정보를 출력하시오
     */
    
    public class CustomerTest {
    
    	public static void main(String[] args) {
    		Customer customerLee = new Customer();
    		customerLee.setCustomerName("이순신");
    		customerLee.setCustomerID(1001);
    		customerLee.bonusPoint = 1000;
    		System.out.println(customerLee.showInfo());
    		System.out.println();
    		
    		VIPCustomer customerLee2 = new VIPCustomer();
    		customerLee2.setCustomerName("이도");
    		customerLee2.setCustomerID(1002);
    		customerLee2.bonusPoint = 10000;
    		System.out.println(customerLee2.showInfo());
    	}
    }



    11. 하위 클래스가 생성되는 과정
        1) 하위 클래스가 생성될 때 상위 클래스가 먼저 생성됨
        2) 상위 클래스의 생성자가 호출되고 하위 클래스의 생성자가 호출됨
           하위 클래스의 생성자에서는 무조건 상위 클래스의 생성자가 호출되어야 함
        3) 하위 클래스에서 상위 클래스의 생성자를 호출하는 코드가 없는 경우 컴파일러는 상위 클래스의 기본 생성자를 호출하기 위한 super()를 추가함
        4) super()로 호출되는 생성자는 상위 클래스의 기본 생성자임
        5) 만약 상위 클래스의 기본 생성자가 없는 경우 (매개변수가 있는 생성자만 존재하는 경우) 하위 클래스는 명시적으로 상위 클래스의 생성자를 호출해야 함

     

    /*
     * 고객 아이디, 고객 이름,
     * 고객 등급 : 기본 생성자에서 기본 등급 silver
     * 보너스 포인트,
     * 보너스 포인트 적립 비율 : 기본 생성자에서 적립비율 1%
     */
    
    public class Customer {
    	protected int customerID;
    	protected String customerName;
    	protected String customerGrade;
    	int bonusPoint;
    	double bonusRatio;
    	
    	public Customer() {
    		customerGrade = "SILVER";
    		bonusRatio = 0.01;
    		System.out.println("Customer() 생성자 호출");
    	}
    	
    	public Customer(int customerID, String customerName) {
    		this.customerID = customerID;
    		this.customerName = customerName;
    		
    		customerGrade = "SILVER";
    		bonusRatio = 0.01;
    		System.out.println("Customer(int, String) 생성자 호출");
    	}
    		
    	public int calcPrice(int price) {
    		bonusPoint += price * bonusRatio;
    		return price;
    	}
    
    	public int getCustomerID() {
    		return customerID;
    	}
    
    	public void setCustomerID(int customerID) {
    		this.customerID = customerID;
    	}
    
    	public String getCustomerName() {
    		return customerName;
    	}
    
    	public void setCustomerName(String customerName) {
    		this.customerName = customerName;
    	}
    
    	public String getCustomerGrade() {
    		return customerGrade;
    	}
    
    	public void setCustomerGrade(String customerGrade) {
    		this.customerGrade = customerGrade;
    	}
    	
    	public String showInfo() {
    		return this.getCustomerName() + "님의 등급은 " + this.getCustomerGrade() + "이며, 보너스 포인트는 " +  bonusPoint + "입니다.";
    	}
    }
    public class VIPCustomer extends Customer {
    	private int agentID;
    	double salesRatio;
    	
    	public VIPCustomer() {
    		// super();
    		this.customerGrade = "VIP";
    		this.bonusRatio = 0.05;
    		this.salesRatio = 0.1;
    		
    		System.out.println("VIPCustomer() 생성자 호출");
    	}
    	
    	public VIPCustomer(int customerID, String customerName) {
    		super(customerID, customerName);
    		
    		this.customerGrade = "VIP";
    		this.bonusRatio = 0.05;
    		this.salesRatio = 0.1;
    		System.out.println("VIPCustomer(int, String) 생성자 호출");
    	}
    
    	public int getAgentID() {
    		return agentID;
    	}
    }
    public class CustomerTest {
    
    	public static void main(String[] args) {
    		Customer customerLee = new Customer(1001, "이순신");
    		customerLee.bonusPoint = 1000;
    		System.out.println(customerLee.showInfo());
    		System.out.println();
    		
    		VIPCustomer customerLee2 = new VIPCustomer(1002, "이도");
    		customerLee2.bonusPoint = 10000;
    		System.out.println(customerLee2.showInfo());
    		
    
    	}
    
    }

    '개발자 수업 > Java' 카테고리의 다른 글

    15. 추상 클래스와 인터페이스  (0) 2021.10.13
    14. 다형성  (0) 2021.10.11
    12. 상속과 오버라이딩  (0) 2021.10.08
    과제1 - TwoDArrayTest03  (0) 2021.10.08
    11. 컬렉션 프레임워크  (0) 2021.10.07

    댓글