package phonebook01;
public class Phonebook {
/*필드*/
//이름 전화번호 주소
String name;
int tel;
String add;
/*생성자*/
Phonebook(String name, int tel, String add){
this.name = name;
this.tel = tel;
this.add = add;
}
Phonebook(String name, int tel){
this(name, tel, null);
}
/*메소드*/
void print() {
System.out.printf("name: %s\n", name);
System.out.printf("phone: %d\n", tel);
if(add!=null) {
System.out.printf("addr: %s\n", add);
}
System.out.println();
}
}
package phonebook01;
public class PhoneBook01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Phonebook book1 = new Phonebook("아이유", 3821234, "대구시");
Phonebook book2 = new Phonebook("박효신", 3842222);
book1.print();
book2.print();
}
}
'IT > Java' 카테고리의 다른 글
Java (메소드 호출_Car) (0) | 2021.05.28 |
---|---|
Java (메소드 호출_주사위) (0) | 2021.05.28 |
Java (this생성자 공통메소드_Date) (0) | 2021.05.24 |
Java (객체지향메소드_Car) (0) | 2021.05.24 |
Java (객체지향_Car) (0) | 2021.05.21 |