-
5. 상속(Inheritance)BackEnd/JAVA 2022. 2. 21. 14:22728x90
HelloJava
public class HelloJava { // 함수 오버로딩(Function Overloading) // 1. 같은 함수명 // 2. 매개변수의 갯수가 다른 경우 // 3. 매개변수의 자료형이 다른 경우 public static void foo() { System.out.println("foo"); } public static void foo(int _val) { System.out.println("foo(int)"); } public static void foo(int _lhs, int _rhs) { System.out.println("foo(int, int)"); } public static void foo(float _val) { System.out.println("foo(float)"); } public static int sum(int _lhs, int _rhs) { return _lhs + _rhs; } public static float sum(float _lhs, float _rhs) { return _lhs + _rhs; } // Design Pattern // Factory Pattern public enum EVehicle { Robot, Tank } public static Vehicle CreateVehicle(EVehicle _vehicle) { Vehicle vehicle = null; switch (_vehicle) { case Robot: vehicle = new Robot(); // vehicle.로봇세팅 case Tank: vehicle = new Tank(); // vehicle.탱크세팅 } return vehicle; } public static void main(String[] args) { // 상속(Inheritance) //People kim = new People(); //People choi = new People("Choi"); // is-a 관계 SuperMan clack = new SuperMan("Clack"); clack.Walk(); clack.Fly(); //clack.name = "Kim"; clack.MyName(); // 함수 오버로딩 //foo(1.2F); System.out.println("---------------"); Robot robot = new Robot(); robot.Drive(); System.out.println("---------------"); // 추상, 인터페이스 클래스는 객체 생성불가 // 다형성(Polymorphism) // Vehicle vehicle = new Robot(); // vehicle.Drive(); // vehicle = new Tank(); // vehicle.Drive(); clack.RideTo(CreateVehicle(EVehicle.Robot)); clack.Drive(); clack.RideTo(CreateVehicle(EVehicle.Tank)); clack.Drive(); } // main } // class
People.java
// Super Class public class People { // Fields: 정보 // protected: 동일한 패키지 내에서는 접근이 가능 // 다른 패키지에서는 상속을 받아야만 접근가능 protected String name = "unKnown"; // private이면 아무리 자식이라도 상속받지 못함 private int eyeCnt = 2; private int armCnt = 2; private int legCnt = 2; //Has-A private Vehicle vehicle = null; // Constructor(생성자) // Default Constructor(기본생성자) - 생성자를 정의하지 않았을 때 자동을 생성되는 생성자 // 생성자가 하나라도 정의가 되었다면 기본 생성자는 만들어지지 않음 public People() { System.out.println("People Constructor Call"); } // 생성자 오버로딩(Constructor Overloading) public People(String _name) { System.out.println( "People Overloading Constructor call" ); this.name = _name; } // Methods: 기능 public void Walk() { System.out.println("People Walking"); } public void Run() { System.out.println("People Running"); } public void MyName() { System.out.println("My name is " + name); } public void RideTo(Vehicle _vehicle) { this.vehicle = _vehicle; System.out.println("Ride Success"); } public void Drive() { if (this.vehicle != null) this.vehicle.Drive(); } }
SuperMan.java
// Sub, Derived //SuperMan은 People의 속성을 다 가져옴 public class SuperMan extends People { // People의 속성을 확장해서 SuperMan을 추가! // 특별한 추가 기능이 없는 경우 객체로 만들고 끝 // People SuperMan = new People(); // SuperMan이 People에 속한다. 이렇게하면 Superman의 속성을 People에서도 쓸수있기 떄문에 적절치 않다. public SuperMan(String _name) { System.out.println("SuperMan constructor Call"); this.name = _name; } // 함수 오버라이딩(Function Overriding) -> 부모가 가진 형식을 재정의 public void Walk() { // 부모 메소드 호출 super.Walk(); System.out.println("SuperMan Fast Walking"); } public void Fly() { System.out.println("SuperMan Flying"); } }
Vehicle.java
// 부모는 추상적으로 설계 // Abstract: 추상 클래스 // Interface class: 필드없이 메소드만 가지고 가이드라이만 제공하는 클래스 public abstract class Vehicle { public class Controller{} private int wheelCnt = 0; private int seatCnt = 0; private float speed = 0.0F; private Controller controller = null; public Vehicle(int _wheelCnt, int _seatCnt, float _speed){ System.out.println("Vihicle Constructor Call"); wheelCnt = _wheelCnt; seatCnt = _seatCnt; speed = _speed; controller = new Controller(); } // 추상 메소드(Abstract Methods) public abstract void Drive(); }
Tank.java
public class Tank extends Vehicle{ public Tank() { super(20, 2, 0.5F); System.out.println("Tank Constructor Call"); } public void Drive() { System.out.println("Tank Driving"); } public void Fire() { System.out.println("Tank Fire"); } }
Robot.java
public class Robot extends Vehicle{ public Robot() { // 부모 생성자 호출 // 부모의 생성자가 매개변수를 받는 형태로만 정의되어 있다면 // 반드시 자식측에서 수동을 호출 해줘야 함 super(4, 1 ,1.3F); System.out.println("Robot Constructor call"); } // 추상 메소드는 자식측에서 반드시 재정의 public void Drive() { System.out.println("Robot Driving"); } }
'BackEnd > JAVA' 카테고리의 다른 글
6. MyArrayList (0) 2022.02.23 5-1. 상속 연습 예제 - 네비게이션 (0) 2022.02.22 4. 배열(Array) (0) 2022.02.18 3. 정적 Static (0) 2022.02.18 2. 클래스(Class) 및 함수 (0) 2022.02.17