public class Camera {
public void picture() {
System.out.println("사진을 찍습니다.");
}
public void save() {
System.out.println("사진을 저장합니다.");
}
public void delete() {
System.out.println("사진을 삭제합니다.");
}
}
public class WebCamera extends Camera {
// 메소드 재정의
public void save() {
System.out.println("클라우드에 사진을 저장.");
}
// 메소드 재정의
public void delete() {
System.out.println("클라우드에서 사진을 삭제.");
}
}
public class CameraApp {
public static void main(String[] args) {
WebCamera c = new WebCamera();
c.picture(); // 부모로부터 상속받은 기능 사용
c.save(); // WebCamera에 재정의된 기능 사용
c.delete(); // WebCamera에 재정의된 기능 사용
}
}
public class Phone {
String tel;
public void connect() {
System.out.println(tel + "에서 전화를 겁니다.");
}
public void disconnect() {
System.out.println(tel + "에서 전화를 끊습니다.");
}
}
public class SmartPhone extends Phone {
String ipAddress;
void facetime() {
System.out.println(tel + "로 화상통화를 시작합니다.");
}
void internet() {
System.out.println(ipAddress + "로 인터넷에 접속합니다.");
}
}
public class PhoneApp {
public static void main(String[] args) {
Phone p1 = new Phone();
p1.tel = ("010-2345-5678");
p1.connect();
p1.disconnect();
System.out.println();
SmartPhone p2 = new SmartPhone();
p2.tel = ("010-1111-2222");
p2.connect();
p2.disconnect();
p2.ipAddress = ("192.168.10.254");
p2.internet();
p2.facetime();
}
}
public class Car {
// 자식 클래스에 상속되지 않음
private String name;
private int speed;
public Car() {
super();
System.out.println("Car() 생성자가 실행됨");
}
public Car(String name, int speed) {
super();
this.name = name;
this.speed = speed;
System.out.println("Car(String, int) 생성자 실행됨");
}
// Getter/Setter 자식 클래스에 상속됨
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
// 자식 클래스에 상속됨 (public 접근 제한이 적용된 필드와 메소드)
public void Start() {
System.out.println("출발한다.");
}
public void Stop() {
System.out.println("멈춘다.");
}
public void carInfo() {
System.out.println("### 차량 정보 ###");
System.out.println("모델명 : " + name);
System.out.println("속 도 : " + speed);
}
}
public class Sonata extends Car {
public Sonata() {
System.out.println("Sonata() 생성자가 실행됨");
}
public void currentSpeed() {
System.out.println("현재속도 : " + getSpeed());
}
public void speedUp() {
int current = getSpeed();
setSpeed(current + 20);
}
}
public class CarApp {
public static void main(String[] args) {
/*
Sonata 객체를 생성할 때 부모 객체인 Car가
먼저 생성되고 Sonata 객체가 생성된다.
*/
Sonata car1 = new Sonata();
car1.speedUp();
car1.currentSpeed();
Sonata car2 = new Sonata();
}
}
public class Genesis extends Car {
private int price;
public Genesis() {
super();
System.out.println("Genesis() 생성자가 실행됨");
}
public Genesis(String name, int speed, int price) {
super(name, speed);
this.price = price;
System.out.println("Genesis(String, int, int) 생성자 실행됨");
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public void carInfo() {
/*
System.out.println("### 차량 정보 ###");
System.out.println("모델명 : " + getName());
System.out.println("속 도 : " + getSpeed());
*/
super.carInfo(); // 부모 클래스에 정의된 carInfo() 호출
System.out.println("가 격 : " + price);
}
}
public class CarApp2 {
public static void main(String[] args) {
Genesis car1 = new Genesis();
car1.setName("G90");
car1.setSpeed(350);
car1.setPrice(100000000);
car1.carInfo();
Genesis car2 = new Genesis("G90L", 400, 150000000);
car2.carInfo();
}
}
'자바 > oop4' 카테고리의 다른 글
Tire, PhotoShop.java (0) | 2019.06.07 |
---|