티스토리 뷰
1. Person 클래스를 설계하라. Person 클래스는 이름, 주소, 전화 번호를 필드로 가진다. 하나 이상의 생성자를 정의하고 각 필드에 대하여 접근자와 설정자 메소드를 작성하라. 이어서 Person을 상속받아서 Customer를 작성. Customer는 고객 번호와 마일리지를 필드로 가지고 있다. 한 개 이상의 생성자를 작성하고 적절한 접근자 메소드와 설정자 메소드를 작성한다. 이들 클래스들의 객체를 만들고 각 객체의 모든 정보를 출력하 는 테스트 클래스를 작성하라.
>> 부모 클래스 - Person : name, address, tel
>> 자식 클래스 - Customer : cno, mileage
>> Test : 메인 함수, 객체 생성해 출력
>> Person class
package no6_1;
public class Person {
private String name;
private String address;
private String tel;
//생성자
public Person (){}//default
public Person(String name, String address, String tel) {
this.name = name;
this.address = address;
this.tel = tel;
}
//접근자 메소드 : getter, setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
//메소드
@Override
public String toString() {
return "고객이름 : "+name+"\t|고객주소 : "+address+"\t|전화번호 : "+tel+"\t|";
}
}
└toString(); : 상속받는 자식 클래스에서 메소드를 재정의할 때, Override와 toString 이용.
처음엔 display() 메소드 만들어서 했는데 Override 안 되서 toString으로 넘어간 것임.
>> Customer class
package no6_1;
public class Customer extends Person {
private int cno;
private int mileage;
//생성자
public Customer() {} //default
public Customer(int cno, String name, String address, String tel, int mileage) {
super(name, address, tel);
this.cno = cno;
this.mileage = mileage;
}
//getter, setter
public int getCno(){
return cno;
}
public void setCno(int cno) {
this.cno = cno;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
//method
@Override
public String toString() {
return "고객번호 : "+cno+"\t|"+super.toString()+"마일리지 : "+mileage;
}
}
>> Test
package no6_1;
public class Test {
public static void main(String[] args) {
Customer p1 = new Customer(1000, "미미", "포포레스1", "010-1234-5678", 965);
Person p2 = new Customer(1010, "로빈", "포포레스2", "010-9876-5432", 1056);
//toString 호출하는 방법1 : System.out
System.out.println(p1.toString());
//toString 호출하는 방법2 : 변수 출력
String str = p2.toString();
System.out.println(str);
}
}
객체 생성할 때,
자식 클래스(Customer)에만 있는 메소드를 불러야 한다면 Customer로 객체 생성해야 한다.
부모 클래스로 객체 생성하면 자식 클래스에만 생성한 메소드를 호출할 수 없다.
'수업 > └Java' 카테고리의 다른 글
| [CH14_1]컬렉션 프레임워크 : List<E> (0) | 2022.02.08 |
|---|---|
| [CH12]Thread (0) | 2022.02.07 |
| [CH10]이너클래스(innner class) (0) | 2022.02.04 |
| [CH09]추상 클래스(abstract class)와 인터페이스(interface) (0) | 2022.02.03 |
| [Ch08]자바 제어자 : final (0) | 2022.02.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- selcetor
- 기본선택자
- 변수
- html pre
- A%B
- border-spacing
- input type 종류
- html input type
- 입력양식
- ScriptTag
- improt
- Java
- typeof
- JavaScript
- 외부구성요소
- css
- caption-side
- 미디어 태그
- html atrribute
- CascadingStyleSheet
- scanner
- BAEKJOON
- initialized
- html
- html a tag
- empty-cell
- html base tag
- html layout
- 스크립태그
- text formatting
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함