OverloadingEx04-생성자 오버로딩
CODEDRAGON ㆍDevelopment/Java
반응형
생성자 오버로딩
- 인자값을 가진 생성자를 통해 다양한 인스턴스 생성
출력결과
소스코드
public class OverloadingEx04 { public static void main(String[] args) { //3개 인자값을 가진 생성자를 통해 인스턴스 생성 Person man=new Person("일지매", 880102, 32); //2개 인자값을 가진 생성자를 통해 인스턴스 생성 Person idol=new Person("홍길동", 18); man.showInfo(); idol.showInfo(); } } class Person{ private String personName; private int personID; private int personAge; //생성자 오버로딩 public Person(String name, int pID, int age) { personName =name; personID=pID; personAge=age; } public Person(String name, int age) { personName=name; personID=0; personAge=age; } public void showInfo() { System.out.println(personName + "님 안녕하세요!!!"); if(personID!=0){ System.out.println("당신의 ID: "+ personID); System.out.println("입장하세요\n"); } else{ System.out.println("당신의 나이는: "+ personAge); System.out.println("미성년자 입니다 \n"); } } } |
'Development > Java' 카테고리의 다른 글
static 메소드(클래스 메소드) (0) | 2015.08.19 |
---|---|
All that 사과(Apple) (0) | 2015.08.18 |
ConsoleOutputEx05-서식문자 확인 (0) | 2015.08.17 |
ConsoleInputEx04-next() vs nextLine() (0) | 2015.08.16 |
Warning-The static field StaticEx02.s2 should be accessed in a static way (0) | 2015.08.15 |