728x90
아래 4개의 클래스 파일을 작성하고, 메인에서 아래와 같은 실행결과가 나오도록 코딩하시오.
Audio클래스
멤버변수
private String brand;
private String quality;
멤버함수
생성자 구현
getter, setter
Montior 클래스
멤버변수
private String company;
private int inch;
멤버함수
생성자 구현
getter, setter
Product<A, M> 클래스
멤버변수
private ? a;
private ? m;
멤버함수
생성자 구현
getter, setter
ProductExample(실행클래스)
실행화면
결과
내 오디오는 Sony 이며, 음질은 최상이며, 4채널이다.
모니터의 상세
브랜드 : 삼성
인치 : 27
가격 : 35000
코드
class Audio1 {
private String brand;
private String quality;
private String chanel;
public Audio1(String brand, String quality, String chanel) {
this.brand = brand;
this.quality = quality;
this.chanel = chanel;
}
public void setAudio(String brand, String quality, String chanel) {
this.brand = brand;
this.quality = quality;
this.chanel = chanel;
}
public String getBrand() {
return brand;
}
public String getQuality() {
return quality;
}
public String getChanel() {
return chanel;
}
}
class Monitor {
private String company;
private int inch;
private int price;
public Monitor(String company, int inch, int price) {
this.company = company;
this.inch = inch;
this.price = price;
}
public void setMonitor(String company, int inch, int price) {
this.company = company;
this.inch = inch;
this.price = price;
}
public String getCompany() {
return company;
}
public int getInch() {
return inch;
}
public int getPrice() {
return price;
}
}
class Product1<A, M> {
private A a;
private M m;
public Product1(A a, M m) {
this.a = a;
this.m = m;
}
public void setProduct(A a, M m) {
this.a = a;
this.m = m;
}
public A getA() {
return a;
}
public M getM() {
return m;
}
}
public class ProductExample1 {
public static void main(String[] args) {
Audio1 audio = new Audio1("Sony", "최상", "4채널"); // 제네릭 아닌 건 이렇게
Monitor monitor = new Monitor("삼성", 27, 35000); // set으로 해도 됨
Product1<Audio1, Monitor> product = new Product1<>(audio, monitor);
System.out.println("내 오디오는 " + product.getA().getBrand() + " 이며, 음질은 " + product.getA().getQuality() + "이며, "
+ product.getA().getChanel() + "이다.");
System.out.println("모니터의 상세");
System.out.println("브랜드 : " + product.getM().getCompany() + '\n' + "인치 : " + product.getM().getInch() + "\n"
+ "가격 : " + product.getM().getPrice());
}
}
set은 따로 만들어주는 게 낫다.
728x90
'Java' 카테고리의 다른 글
| [Java] 제네릭 클래스 예제 (21-1 수정 throw catch 학습 후 수정 필요) (2) | 2023.09.07 |
|---|---|
| [Java] 제네릭 클래스 예제 (20-5 확인 필요) (0) | 2023.09.07 |
| [Java] 제네릭 클래스 예제 (20-3) (0) | 2023.09.07 |
| [Java] 제네릭 클래스 예제 (20-2) (0) | 2023.09.07 |
| [Java] 제네릭 클래스 예제 (20-1) (0) | 2023.09.07 |