중복메소드(오버로딩)
오버로딩 한클래스안에 같은이름의 다양한 매개변수를 가진 메서드를 정의하는것
오버로딩이 성립하기 위한 조건
1.메서드 이름이 같아야한다.
2.매개변수의 개수 또는 타입이 달라야한다.
3.반환 타입은 영향이 없다.
package javajungsuck;
public class C6중복매서드 {
//메소드 중복정의 (오버로딩): 한클래스안에 같은이름의 다양한 매개변수를 가진 메서드를 정의하는것
//오버로딩이 성립하기 위한 조건
//1.메서드 이름이 같아야한다.
//2.매개변수의 개수 또는 타입이 달라야한다.
//3.반환 타입은 영향이 없다.
int s = 0;
void add(int a){
s += a;
return;
}
//위와 같은 이름의 add지만 매개변수가 a,b 2개로 다르다
void add(int a,int b) {
s = a+b;
}
//반환타입은 영향이 없다.
//반환타입이 int지만 영향을 주지않고 매개변수의 수가 겹치기 때문에 오류가난다.
//int add(int a,int b) {
// s = a+b;
//}
long add (long a, int b,int c) {
long x = a + b + c;
return x;
}
}
중복정의 이용 (사원 정보 입력 기능 구현)
P2_Employee
package javajungsuckP;
public class P2_Employee {
int id; //필수 입렧값
String name; // 필수 입력값
String email;
String phoneNumber;
String department;
String position;
int salary;
double commisionPct;
//메서드 중복정의 같은 employee이름 메서드
//신입사원용 //사원번호 이름 전화번호 부서 월급
void employee(int id,String name,String PhoneNumber,String department, int salary) {
//this 인스턴스 변수 사용 this()생성자 메서드
this.id = id;
this.name = name;
this.phoneNumber = PhoneNumber;
this.department = department;
this.salary = salary;
}
//대리사원용 //사원번호 이름 전화번호 부서 직책 월급
void employee(int id,String name,String PhoneNumber,String department,String position, int salary) {
this.id = id;
this.name = name;
this.phoneNumber = PhoneNumber;
this.department = department;
this.position = position;
this.salary = salary;
}
//팀장용 //사원번호 이름 전화번호 부서 직책 이메일 월급
void employee(int id,String name,String PhoneNumber,String department,String position,String email, int salary) {
this.id = id;
this.name = name;
this.phoneNumber = PhoneNumber;
this.department = department;
this.position = position;
this.email = email;
this.salary = salary;
}
//사원 정보 조회 메서드
//사원번호 이름 전화번호 부서 직책 이메일 월급
void employeeSearch() {
System.out.println("---------------------------------------");
System.out.println("사원번호 : "+id);
System.out.println("이름 : "+name);
System.out.println("전화번호 : "+phoneNumber);
System.out.println("부서 : "+department);
System.out.println("직책 : "+position);
System.out.println("월급 : "+salary);
System.out.println("---------------------------------------");
}
}
p2_App
package javajungsuckP;
public class p2_App {
public static void main(String[] args) {
P2_Employee emp = new P2_Employee();
P2_Employee emp1 = new P2_Employee();
P2_Employee emp2 = new P2_Employee();
//신입사원용 //사원번호 이름 전화번호 부서 월급
emp.employee(1, "김민재", "010-47-39", "전산팀", 10000);
//대리사원용 //사원번호 이름 전화번호 부서 직책 월급
emp1.employee(2, "황희찬", "010-2-34", "마케팅팀", "대리", 30000);
//팀장용 //사원번호 이름 전화번호 부서 직책 이메일 월급
emp2.employee(3, "손흥민", "010-2930-323", "개발팀", "팀장", "otoutu@naver.con", 50000);
//출력
emp.employeeSearch();
emp1.employeeSearch();
emp2.employeeSearch();
}
}
실행결과
생성자 : 인스턴스가 생성될때마다 호출 되는 인스턴스 초기화 수행
1. 생성자의 이름은 클래스의 이름과 같아야한다.
2. 리턴값이 없다(void 안붙임)
3. 모든 클래스는 반드시 생성자를 가져야 한다.
ex Card c = new Card(); ->Card(); -> 생성자
Class Card{
Card() {
} //매개변수 없는 생성자
Card(string kind, int number){
}
}
기본생성자
1.매개변수가 하나도 없는 생성자
2. 생성자가 하나도 없을때만 컴파일러가 자동으로 추가해줌
3. 생성자가 하나라도 있으면 사용자가 기본생성자를 추가해줘야한다.
클래스이름(){} // 기본생성자
point(){} // 포인트 클래스의 기본생성자
생성자 this()
같은 클래스 내에서 씀
코드중복 제거를 위해 씀
***다른생성자 호출시 첫줄에서만 사용가능
this()
this는 참조변수 this()는 생성자
package javajungsuck;
public class C7기본생성자 {
public static void main(String[] args) {
Construc1 con = new Construc1();
Construc con1 = new Construc(1);
System.out.println(con.value);
System.out.println(con1.value);
System.out.println("------------------------");
//Robot
Robot r1 = new Robot("r1",5,2);
System.out.println(r1.name);
System.out.println(r1.damege);
System.out.println(r1.heart);
System.out.println("------------------------");
//Robot this() 호출
Robot r2 = new Robot();
System.out.println(r2.name);
System.out.println(r2.damege);
System.out.println(r2.heart);
}
}
class Construc1 {
int value;
//Construc1(){}; 자동추가해줌
}
class Construc{
int value;
//사용자가 직접 기본생성자를 추가해줘야함
Construc(){};
Construc(int x){//매개변수가 있는 생성자 하나존재
value = x;
}
}
class Robot{
String name;
int damege = 0;
int heart = 0;
Robot(){//기본생성자 생성 아무런 값도 주지 않았을때
this("r2",5,6); //this() 생성자 사용
System.out.println("이객체는 로봇입니다");
}
//매개변수를 받는 코드중복이 있어서 좋은 코드는 아니다.
Robot(String n,int d, int h){
name = n;
damege = d ;
heart = h;
}
}
실행결과
'개발공부 2023 ~03~13' 카테고리의 다른 글
변수초기화, 오버라이딩(메소드 재정의), (0) | 2023.03.28 |
---|---|
is-a,has-a(상속,포함관계) ,단일상속, super,super() (0) | 2023.03.27 |
변수와 메서드(인스턴트, 클래스, 참조형 매개변수, 메서드) (0) | 2023.03.26 |
객체지향 (클래스와 객체) (0) | 2023.03.23 |
7일차 복습 ( 은행 객체 생성,코딩 시 신경 써야 할 점 ) (0) | 2023.03.21 |