Ex01_inheritance
package com.mywork.ex;
class Parent {
int number;
void doParent() {
System.out.println("doParent() 호출");
}
}
class Child extends Parent {
void doChild() {
System.out.println("doChild() 호출");
}
}
public class Ex01_inheritance {
public static void main(String[] args) {
Child child = new Child();
child.number = 10;
System.out.println(child.number);
child.doParent();
child.doChild();
}
}
Ex02_inheritance
package com.mywork.ex;
class Person{
void sleep(){
System.out.println("잔다.");
}
void eat(String food) {
System.out.println(food + " 먹는다.");
}
}
class Student extends Person {
void study() {
System.out.println("공부한다.");
}
}
public class Ex02_inheritance {
public static void main(String[] args) {
Student student = new Student();
student.sleep();
student.eat("빵");
student.study();
}
}
Ex03_inheritance
package com.mywork.ex;
class Car{
void drive() {
System.out.println("차가 달린다.");
}
}
class Ev extends Car{
void charging() {
System.out.println("전기를 충전한다.");
}
}
public class Ex03_inheritance {
public static void main(String[] args) {
Ev ev = new Ev();
ev.drive();
ev.charging();
}
}
Ex04_constructor
package com.mywork.ex;
class Mother{
Mother(){
System.out.println("Mother 생성!");
}
}
class Son extends Mother{
Son(){
System.out.println("Son 생성!");
}
void doSon() {
System.out.println("doSun() 호출!");
}
}
public class Ex04_constructor {
public static void main(String[] args) {
Son son = new Son();
son.doSon();
}
}
Ex05_contructor
package com.mywork.ex;
class Animal{
String name;
Animal() {}
Animal(String name){
this.name = name;
}
}
class Dog extends Animal{
String personName;
Dog(String personName){
super();
this.personName = personName;
}
Dog(String name, String personName){
super(name);
this.personName = personName;
}
void whoAmI() {
System.out.println("내 이름은 " + name + "이고, 주인은 " + personName + "입니다.");
}
}
public class Ex05_contructor {
public static void main(String[] args) {
Dog dog = new Dog("alice");
dog.whoAmI();
Dog dog2 = new Dog("이쁜이", "이말자");
dog2.whoAmI();
}
}
Ex06_has_a
package com.mywork.ex;
class Engine{
String fuel;
Engine(String fuel){
this.fuel = fuel;
}
void engineInfo() {
System.out.println("사용 연료 : " + fuel);
System.out.println(fuel.equals("가솔린") ? "가솔린엔진" : "디젤엔진" );
}
}
class Suv extends Engine{
String model;
Suv(String fuel, String model){
super(fuel);
this.model = model;
}
void suvInfo() {
System.out.println("모델 : "+ model);
super.engineInfo();
}
}
public class Ex06_has_a {
public static void main(String[] args) {
Suv suv1 = new Suv("가솔린", "싼타페");
Suv suv2 = new Suv("디젤", "쏘렌토");
suv1.suvInfo();
suv2.suvInfo();
}
}
Ex07_method_override
package com.mywork.ex;
class Espresso {
String origin;
Espresso(String origin) {
this.origin = origin;
}
void taste() {
System.out.println("강렬하다");
}
}
class Latte extends Espresso {
String milk;
Latte(String origin, String milk){
super(origin);
this.milk = milk;
}
void taste() {
System.out.println("부드럽다");
}
}
public class Ex07_method_override {
public static void main(String[] args) {
Latte latte = new Latte("인도네시아", "서울우유");
latte.taste();
}
}