자바_기초
상속 : 문제3) 핸드폰 모델명, 번호, 벨소리. 추가 : 사이즈, pixel
잠수콩
2019. 5. 27. 13:12
//기본 : 모델명, 번호, 벨소리
//생성자 생성
//기능 : set number, get model, get chord
// 추가 : 사이즈
// 추가 : pixel
package Inheritance;
class Cellphone{
String model; //모델명
String number; //폰번호
int chord; //벨소리
//생성자 생성
public Cellphone(String model, String number, int chord) {
this.model = model;
this.number = number;
this.chord = chord;
}
void setNumber(String number) { //번호 재저장
this.number = number;
}
String getModel() {
return this.model;
}
String getNumber() {
return number;
}
int getChord(int b) {
this.chord = b;
return chord;
}
@Override
public String toString() {
return "Cellphone [model=" + model + ", number=" + number + ", chord=" + chord + "]";
}
}
class MP3Phone extends Cellphone{
int size;
public MP3Phone(String mod, String num, int cho, int size) {
super(mod, num, cho); //부모 생성자 호출 - 초기화
this.size = size;
}
}
class DicaPhone extends Cellphone{
String pixel;
public DicaPhone(String mod, String num, int cho, String pixel) {
super(mod, num, cho); //부모 생성자를 호출하는 super는 맨 상위에 작성!
this.pixel = pixel;
}
}
public class exam_CellPhoneMain {
public static void main(String[] args) {
Cellphone c1 = new Cellphone("aaaa", "2222222", 333333);
System.out.println(c1);
System.out.println("==============");
DicaPhone d1 = new DicaPhone("삼성 갤럭시", "010-1111-2222", 24, "100px");
System.out.println(d1.getModel());
System.out.println(d1);
System.out.println("==============");
MP3Phone m1 = new MP3Phone("LG V50", "010-3333-4444", 24, 600);
System.out.println(m1.getNumber());
System.out.println(m1);
}
}