1) 상속구조
2) Java소스
① Cellphone : tell과 call의 메소드를 가지고 있다.
package phone;
public class Cellphone {
/**
* @param args
*/
void tell(){
System.out.println("전화기능.");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Cellphone c1 = new Smartphone();
Cellphone c2 = new Featurephone();
Cellphone c3 = new IPhone();
Cellphone c4 = new Android();
call(c1);
call(c2);
call(c3);
call(c4);
}
public static void call(Cellphone m){
m.tell();
}
}
package phone;
public class Featurephone extends Cellphone {
void cacao(){
System.out.println("카톡안됌 ㅅㄱ");
}
@Override
void tell() {
// TODO Auto-generated method stub
System.out.println("2G 또는 3G");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Featurephone().cacao();
}
}
③ Smartphone 클래스 : Smartphone클래스는 Cellphone클래스의 필드와 메소드를 상속받는다.
package phone;
public class Smartphone extends Cellphone {
void cacao(){
System.out.println("카톡가능");
}
@Override
void tell() {
// TODO Auto-generated method stub
System.out.println("3G or 4G");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Smartphone().tell();
Cellphone c = new Smartphone();
System.out.println(c);
}
}
④ Iphone 클래스 : Iphone 클래스는 Smartphone클래스의 필드와 메소드를 상속받는다.
package phone;
public class IPhone extends Smartphone{
void market(){
System.out.println("앱스토어");
}
void cacao() {
System.out.println("앱스토어에서 구매");
}
@Override
void tell() {
// TODO Auto-generated method stub
System.out.println("아직 3G ㅠㅠ");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
⑤ Adroid 클래스 : Android 클래스는 Smartphone 클래스의 필드와 메소드를 상속받는다.
package phone;
public class Android extends Smartphone {
void market() {
System.out.println("안드로이드마켓");
}
void cacao() {
System.out.println("안드로이드마켓에서 구매");
}
@Override
void tell() {
// TODO Auto-generated method stub
System.out.println("LTE로 한다.");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Android c = new Android();
Cellphone.call(c);
}
}
'Java > Java Advanced' 카테고리의 다른 글
Java Advanced 1일차 첫번째) Inheritance (0) | 2012.09.10 |
---|