Java/Inheritance(상속)
상속으로 핸드폰 만들기
Bohemian life
2012. 4. 11. 16:30
package com.ext3;//상속으로 핸드폰 만들기1 public class CellPhone { //휴대폰 기본 기능을 제공하는 클래스 //private면은 가져다 쓸수 없지만 protected는 사용가능 protected String model; protected String number; protected int chord; public String getModel(){ return model; } public int getChord(){ return chord; } public String getNumber(){ return number; } }
package com.ext3; public class CellPhoneMain { public static void main(String[] args){ DicaPhone dca = new DicaPhone("IN-7600","N235",60,"400만"); MP3Phone mp = new MP3Phone("KN-600","S300",60,256); System.out.println("============================="); System.out.println("모델\t번호\t코드\t옵션(화소수/사이즈)"); System.out.println("============================="); System.out.print(dca.getModel()+"\t"); System.out.print(dca.getNumber()+"\t"); System.out.print(dca.getChord()+"\t"); System.out.print(dca.getPixel()+"\n"); System.out.print(mp.getModel()+"\t"); System.out.print(mp.getNumber()+"\t"); System.out.print(mp.getChord()+"\t"); System.out.print(mp.getSize()+"\n"); } }
package com.ext3;//디카폰 class DicaPhone extends CellPhone{ private String pixel; public DicaPhone(String model,String number, int chord, String pixel){//cellphone에 있는것들 호출 this.model = model; this.number = number; this.chord = chord; this.pixel = pixel; //this라 붙이면 CellPhone의 protected에 접근하게됨 }//set메소드는 생성자를 통해서 구현 public String getPixel(){ return pixel; } }
package com.ext3; public class MP3Phone extends CellPhone{ //부모 클래스인 cellphone를 상속받아 기능을 확장한 MP3Phone private int size; //저장용량 public MP3Phone(String model,String number, int chord, int size){ this.model = model; this.number = number; this.chord = chord; this.size = size; } public int getSize() { return size; } }