Quiz 3. 오버로딩 성립하지 않는 메소드 설명하기
접근제한자, 반환형, 메소드명은 오버로딩 충족 조건에 해당하지 않는다. 오버로딩은 메소드 시그니처에 들어있는 (매개변수) 타입, 갯수, 순서에 따라 정의된다.
public void test() {}public void test(int var1, int var2) {}매개변수명은 오버로딩 충족 조건에 해당하지 않는다.public void test(int var2, int var1) {}따라서 (int, int)형 중복되므로 수정 또는 삭제하여야 컴파일 에러에서 벗어날 수 있다.
public void test(int var1, String var2) {}
public void test(String var1, int var2) {}
Quiz 2. VO(DTO) Destination.class 생성하기
Desination.class의 매개변수 있는 생성자에서 String[] trans를 선언했다. 이는 Application.class에서 출력 시에 배열 작성법에 맞춰 (new String[] {"plane", "tram"}); 처럼 작성해야 컴파일 에러 코드가 사라지는 것을 알 수 있었다.
=== Destination A ===
목적지 : Seoul
거리 : 140.5km
소요시간 : 2hr
이동수단 : car taxi subway train bus
=== Destination B ===
목적지 : Hawaii
거리 : 7617.5km
소요시간 : 8hr
이동수단 : plane tram
package com.reminder.class_and_object_quiz;
public class Application {
public static void main(String[] args) {
/* Destination */
/* 초기화A. 매개변수 있는 생성자 */
System.out.println("=== Destination A ===");
Destination desA = new Destination("Seoul", 140.5, 2, new String[] {"car", "taxi", "subway", "train", "bus"});
desA.printInformation();
/* 초기화B. 기본 생성자 + 설정자 */
System.out.println("=== Destination B ===");
Destination desB = new Destination();
desB.setDesName("Hawaii");
desB.setDistance(7617.5);
desB.setSpendTime(8);
desB.setTrans(new String[] {"plane", "tram"});
desB.printInformation();
}
}
package com.reminder.class_and_object_quiz;
public class Destination {
/* 필드 */
private String desName;
private double distance;
private int spendTime;
private String[] trans;
/* 생성자 */
public Destination() {
}
public Destination(String desName, double distance, int spendTime, String[] trans) {
this.desName = desName;
this.distance = distance;
this.spendTime = spendTime;
this.trans = trans;
}
/* 설정자와 접근자 */
public void setDesName(String desName) {
this.desName = desName;
}
public void setDistance(double distance) {
this.distance = distance;
}
public void setSpendTime(int spendTime) {
this.spendTime = spendTime;
}
public void setTrans(String[] trans) {
this.trans = trans;
}
public String getDesName() {
return desName;
}
public double getDistance() {
return distance;
}
public int getSpendTime() {
return spendTime;
}
public String[] getTrans() {
return trans;
}
/* 출력 메소드 */
public void printInformation() {
System.out.println("목적지 : " + desName);
System.out.println("거리 : " + distance + "km");
System.out.println("소요시간 : " + spendTime + "hr");
System.out.print("이동수단 : ");
for(int i=0; i < trans.length; i++) {
System.out.print(trans[i] + " ");
}
System.out.println();
}
}
Quiz 1. VO(DTO) Product.class 생성하기
초기화 방법 두 가지 통해 출력까지 만들어봤다. A. 매개변수 있는 생성자, B. 기본생성자 + 설정자.
===== Product A =====
제품명 : Sweatshirts
제품번호 : 99
가격 : 25000
사이즈 : M
===== Product B =====
제품명 : Sweatpants
제품번호 : 100
가격 : 30000
사이즈 : M
package com.reminder.class_and_object_quiz;
public class Application {
public static void main(String[] args) {
/* Product */
/* 초기화A. 매개변수 있는 생성자 */
Product prodA = new Product("Sweatshirts", 99, 25000, 'M');
System.out.println("===== Product A =====");
prodA.printInformation();
/* 초기화B. 기본생성자 + 설정자 */
Product prodB = new Product();
prodB.setProductName("Sweatpants");
prodB.setProductNum(100);
prodB.setPrice(30000);
prodB.setSize('M');
System.out.println("===== Product B =====");
prodB.printInformation();
}
}
package com.reminder.class_and_object_quiz;
public class Product {
/* 필드 */
private String productName;
private int productNum;
private int price;
private char size;
/* 생성자 */
public Product() {
}
public Product(String productName, int productNum, int price, char size) {
this.productName = productName;
this.productNum = productNum;
this.price = price;
this.size = size;
}
/* 설정자와 접근자 */
public void setProductName(String productName) {
this.productName = productName;
}
public void setProductNum(int productNum) {
this.productNum = productNum;
}
public void setPrice(int price) {
this.price = price;
}
public void setSize(char size) {
this.size = size;
}
public String getProductName() {
return productName;
}
public int getProductNum() {
return productNum;
}
public int getPrice() {
return price;
}
public char getSize() {
return size;
}
/* 출력 메소드 */
public void printInformation() {
System.out.println("제품명 : " + productName);
System.out.println("제품번호 : " + productNum);
System.out.println("가격 : " + price);
System.out.println("사이즈 : " + size);
}
}
'Java' 카테고리의 다른 글
[자바의 정석] Ch 5. 배열 예제 응용 학습 (0) | 2022.01.02 |
---|---|
[자바의 정석] Ch 5. 배열 연습문제 풀이 (0) | 2022.01.01 |
[JAVA/수업 과제 practice] 클래스와 객체 Lv. 3~4 (0) | 2021.12.30 |
[JAVA] 6-3. 오버로딩, 파라미터, static, final, 싱글톤 패턴 (0) | 2021.12.30 |
[JAVA/수업 과제 practice] 클래스와 객체 Lv. 1~2 (0) | 2021.12.29 |