클래스와 자바코드
태그 :
- 클래스와 자바코드 사례들
|
|
class ReadLine { public void main(String[] args) { int counter =0; while(counter < 5){ System.out.println(“This is a line.”); counter++; } } }
|
- 실전문제에서 클래스 다이아그램
- 문제
public class Employee { String employeeName; String address; String phoneNumber; EmployeeInfo getEmployeeInfo() { return employeeInfo; } } public class Developer extends Employee { String developerID; String getDeveloperID() { return developerID; } } |
- 분석 및 답
- 문제
1. A[isDiscount-true]: getDiscountPrice() 2. A[isDiscount-false]: get Price() |
- 분석 및 답
- Order 클래스와 Product 클래스는 연관관계(Association Relationship)로 구성
- Order 클래스에서 Product 클래스를 product라는 인스턴스명으로 참조하여 사용
- Order 클래스(Order.java)
package test public class Order{ private Product product; /*생성자 메소드 */ public Order(){ } public int A( boolean isDiscount){ /* isDiscount가 true일 경우에는 getDiscountPrice()를 호출하고, false일 경우에는 getPrice()를 호출한다. */ //Product라는 클래스를 product라는 인스턴스 생성 product = new Product(); if( isDiscount ){ return product.getDiscountPrice(); }else{ return product.getPrice(); } } } |
- Product 클래스(Product.java)
package test public class Product{ /* 상품가격 */ private int productPrice; /* 할인율 – 예를 들어 20% 세일이라고 가정함 */ private float discountRate = 0.2f; /* 생성자 메소드 */ public Product(){ } public int getDiscountPrice(){ /* 상품의 할인된 가격을 리턴한다. return (int)productPrice * (1.0-discountPrice); } public int getPrice(){ return productPrice; } } |