고래씌
[JAVA] 7-4. 오버로딩 & 클래스 실습문제 본문
1. 메소드 오버로딩
- 하나의 클래스 내에 "같은"이름의 메소드명을 정의할 수 있는 방법
- 매개변수의 자료형의 개수, 순서가 다르게 작성하면 된다
- 단, 매개변수명이나 접근제한자 반환형은 오버로딩과 연관 X
=> 매개변수의 이름과 반환형, 접근제한자가 다르다고 오버로딩이 적용되지 않는 것을 확인
2. 클래스 실습문제
package com.kh.practice.snack.model.vo;
public class Snack {
private String kind; // 종류
private String name; // 이름
private String flavor; // 맛
private int numOf; // 개수
private int price; // 가격
public Snack() {
}
public Snack(String kind, String name, String flavor, int numOf, int price) {
this.kind = kind;
this.name = name;
this.flavor = flavor;
this.numOf = numOf;
this.price = price;
}
public String information() {
return kind+ "("+name+" - "+flavor+ ")"+" " + numOf + "개" + price+"원";
}
public void setKind(String kind) {
this.kind = kind;
}
public void setName(String name) {
this.name = name;
}
public void setFlavor(String flavor) {
this.flavor = flavor;
}
public void setNumOf(int numOf) {
this.numOf = numOf;
}
public void setPrice(int price) {
this.price = price;
}
public String getKind() {
return kind;
}
public String getName() {
return name;
}
public String getFlavor() {
return flavor;
}
public int getNumOf() {
return numOf;
}
public int getPrice() {
return price;
}
}
package com.kh.practice.snack.controller;
import com.kh.chap05_constructor.mdoel.vo.User;
import com.kh.practice.snack.model.vo.*;
import com.kh.practice.snack.view.SnackMenu;
public class SnackController {
private Snack s = new Snack();
public SnackController() {
}
public String saveData(String kind, String name, String flavor, int numOf, int price) {
s.setKind(kind);
s.setName(name);
s.setFlavor(flavor);
s.setNumOf(numOf);
s.setPrice(price);
return "저장 완료되었습니다.";
}
public String confrimData() {
return s.information();
}
}
package com.kh.practice.snack.view;
import java.util.Scanner;
import java.util.Stack;
import com.kh.chap05_constructor.mdoel.vo.User;
import com.kh.practice.snack.controller.*;
import com.kh.practice.snack.model.vo.Snack;
public class SnackMenu {
private Scanner sc = new Scanner(System.in);
private SnackController scr = new SnackController();
public void menu() {
System.out.println("스낵류를 입력하세요.");
System.out.print("종류 : ");
String kind = sc.nextLine();
System.out.print("이름 : ");
String name = sc.nextLine();
System.out.print("맛 : ");
String flavor = sc.nextLine();
System.out.print("개수 : ");
int numOf = sc.nextInt();
System.out.print("가격 : ");
int price = sc.nextInt();
sc.nextLine();
String result = scr.saveData(kind, name, flavor, numOf, price);
System.out.println(result);
System.out.println("저장한 데이터를 확인하시겠습니까?(y/n)");
char ch = sc.nextLine().charAt(0);
if(ch == 'y' || ch == 'Y') {
System.out.println(scr.confrimData());
}
}
}
package com.kh.practice.snack.run;
import com.kh.practice.snack.view.*;
public class Run {
public static void main(String[] args) {
// SnackMenu sn = new SnackMenu();
// sn.menu();
new SnackMenu().menu();
}
}
'JAVA > JAVA 기초' 카테고리의 다른 글
[JAVA] 9. 상속 & 오버라이딩 (0) | 2023.10.13 |
---|---|
[JAVA] 8. 객체 배열 & 실습문제 (0) | 2023.10.13 |
[JAVA] 7-3. 메소드(Method) (0) | 2023.10.12 |
[JAVA] 7-2. 생성자 (0) | 2023.10.12 |
[JAVA] 7-1. 클래스(Class) (0) | 2023.10.12 |