1. 메소드 오버라이딩
import chap_07.camera.Camera;
import chap_07.camera.FactoryCam;
import chap_07.camera.SpeedCam;
public class _13_MethodOverriding {
public static void main(String[] args) {
// 메소드 오버라이딩 : 자식 클래스에서 부모 클래스의 메소드를 덮어쓰기 (재정의)
Camera camera = new Camera();
FactoryCam factoryCam = new FactoryCam();
SpeedCam speedCam = new SpeedCam();
camera.showMainFeature();
factoryCam.showMainFeature();
speedCam.showMainFeature();
}
}
2. 다형성
mport chap_07.camera.Camera;
import chap_07.camera.FactoryCam;
import chap_07.camera.SpeedCam;
public class _14_Polymorphism {
public static void main(String[] args) {
// 다형성
// class Person : 사람
// class Student extends Person : 학생 (학생은 사람이다. Student is a person)
// class Teacher extends Person : 선생님 (선생님은 사람이다. Teacher is a person)
Camera camera = new Camera();
Camera factoryCam = new FactoryCam();
Camera speedCam = new SpeedCam();
camera.showMainFeature();
factoryCam.showMainFeature();
speedCam.showMainFeature();
System.out.println("----------------");
Camera[] cameras = new Camera[3];
cameras[0] = new Camera();
cameras[1] = new FactoryCam();
cameras[2] = new SpeedCam();
for (Camera cam : cameras) {
cam.showMainFeature();
}
System.out.println("----------------");
// factoryCam.detectFire();
// speedCam.checkSpeed();
// speedCam.recognizeLicensePlate();
if (camera instanceof Camera) {
System.out.println("카메라입니다.");
}
if (factoryCam instanceof FactoryCam) {
((FactoryCam) factoryCam).detectFire();
}
if (speedCam instanceof SpeedCam) {
((SpeedCam) speedCam).checkSpeed();
((SpeedCam) speedCam).recognizeLicensePlate();
}
// Object[] objs = new Object[3];
// objs[0] = new Camera();
// objs[1] = new FactoryCam();
// objs[2] = new SpeedCam();
}
}
*부모 클래스의 메소드를 호출해서 배열 만들 수 있음 -> eg. for (Camera cam : cameras) {cam.showMainFeature();}
*자식 클래스의 메소드의 경우 형변환을 한 후 사용할 수 있음 -> ((FactoryCam) factoryCam).detectFire();
3. Super
_15_Super
import chap_07.camera.FactoryCam;
import chap_07.camera.SpeedCam;
public class _15_Super {
public static void main(String[] args) {
// Super
FactoryCam factoryCam = new FactoryCam();
SpeedCam speedCam = new SpeedCam();
factoryCam.recordVideo();
System.out.println("-----------------");
speedCam.takePicture();
}
}
부모 클래스
public void recordVideo() {
// 동영상 녹화
System.out.println(this.name + " : 동영상을 녹화합니다.");
}
자식 클래스
public void recordVideo() {
super.recordVideo();
detectFire();
*Super를 통해 부모 클래스의 메소드 바로 사용
4. 참조
import chap_07.camera.Camera;
public class _16_Reference {
public static void main(String[] args) {
// 참조
// 기본 자료형 (Primitive Data Types) : int, float, double, long, boolean, ...
int[] i = new int[3];
System.out.println(i[0]); // 0
double[] d = new double[3];
System.out.println(d[0]); // 0.0
// 참조 자료형 (Non-Primitive, Reference Data Types) : String, Camera, FactoryCam, SpeedCam, ...
String[] s = new String[3];
System.out.println(s[0]); // null
Camera[] c = new Camera[3];
System.out.println(c[0] == null); // true
/////////////////////////////////
System.out.println("-------------------");
int a = 10;
int b = 20;
b = a;
System.out.println(a);
System.out.println(b);
b = 30;
System.out.println(a);
System.out.println(b);
System.out.println("-------------------");
Camera c1 = new Camera();
Camera c2 = new Camera();
c1.name = "카메라1";
c2.name = "카메라2";
System.out.println(c1.name);
System.out.println(c2.name);
c2 = c1;
System.out.println(c1.name);
System.out.println(c2.name);
c2.name = "고장난 카메라";
System.out.println(c1.name);
System.out.println(c2.name);
System.out.println("--------------");
changeName(c2);
System.out.println(c1.name); // 잘못된 카메라
System.out.println(c2.name); // 잘몬된 카메라
c2 = null;
System.out.println(c2.name);
}
public static void changeName(Camera camera) {
camera.name = "잘못된 카메라";
}
}
5. Final
_17_Final
import chap_07.camera.ActionCam;
import chap_07.camera.SlowActionCam;
public class _17_Final {
public static void main(String[] args) {
// Final
// public (final) class ..
// public (final) void ...
// public > abstract > static > final > ...
ActionCam actionCam = new ActionCam();
// actionCam.lens = "표준렌즈";
actionCam.recordVideo();
actionCam.makeVideo();
System.out.println("------------------");
SlowActionCam slowActionCam = new SlowActionCam();
slowActionCam.makeVideo();
}
}
ActionCam
public final class ActionCam extends Camera {
public final String lens; // = "광각렌즈";
public ActionCam() {
super("액션 카메라");
lens = "광각렌즈";
}
public final void makeVideo() {
System.out.println(this.name + " : " + this.lens + "로 촬영한 영상을 통해 멋진 비디오를 제작합니다.");
}
}
*생성자 내에서도 처리할 수도 있음 (lens 값)
*public final String lens -> 렌즈 값을 고정
*값을 바꾸거나 메소드를 바꾸지 못하게 final로 고정 가능. 상속을 못하게 클래스 앞에 final을 붙일 수도 있음
6. 열거형
_18_Enum
public class _18_Enum {
public static void main(String[] args) {
// 열거형 (Enum)
// 달력 : JAN, FEB, MAR, ...
// 옷 사이즈 : S, M, L, XL
// OS 종류 : WIN10, WIN11, MACOS, LINUX, ...
// 해상도 : HD, FHD, QHD, UHD, ...
Resolution resolution = Resolution.HD;
System.out.println(resolution);
resolution = Resolution.FHD;
System.out.println(resolution);
System.out.print("동영상 녹화 품질 : ");
switch (resolution) {
case HD:
System.out.println("일반화질");
break;
case FHD:
System.out.println("고화질");
break;
case UHD:
System.out.println("초고화질");
break;
}
resolution = Resolution.valueOf("UHD");
System.out.println(resolution);
System.out.println("-------------------");
for (Resolution myRes : Resolution.values()) {
System.out.println(myRes.name() + " : " + myRes.ordinal());
}
System.out.println("-------------------");
for (Resolution myRes : Resolution.values()) {
System.out.println(myRes.name() + " : " + myRes.getWidth());
}
}
}
enum Resolution {
HD(1280), FHD(1920), UHD(3840);
private final int width;
Resolution(int width) {
this.width = width;
}
public int getWidth() {
return width;
}
}
'인강 보습 (인프런,유데미,패스트캠퍼스) > 나도코딩의 자바 기본편' 카테고리의 다른 글
제네릭스 - (1) (0) | 2023.01.18 |
---|---|
추상 클래스와 인터페이스 (0) | 2023.01.17 |
클래스 (2) - This, 생성자, Getter&Setter, 접근제어자, 패키지, 상속 (0) | 2023.01.17 |
클래스 (1) - 클래스, 인스턴스 변수, 클래스 변수, 메소드, 메소드 오버로딩, 클래스 메소드 (0) | 2023.01.17 |
메소드 (2) - Overloading, WhenToUse, VariableScope, MainMethod (0) | 2023.01.13 |