100일 챌린지/빅데이터기반 인공지능 융합 서비스 개발자

Day 08 - 인터페이스와 java.lang 클래스

ksyke 2024. 7. 31. 14:01

Interface의 상속

  • Java는 다중상속이 허용되지 않기 때문에 Interface가 발달되어 있다.
    public class Ex05 extends Object implements Inter01{
      public static void main(String[] args) {
          Ex05 me = new Ex05();
          Object me1 = new Ex05();
          Inter01 me2 = new Ex05();

implements를 사용한 상속

interface Inter01{
    public static final int su = 1111;
    int su2 = 2222;
//    int su3; // 초기화를 하지 않으면 선언 불가능 (final이 숨겨져있기 때문에)

    public abstract void func01(); // interface는 추상 메서드만 가질 수 있다. 
    public void func02(); // abstract 키워드 생략가능
    void func03(); // override해서 사용해야 하기 때문에 public 생략가능
}

public class Ex05 implements Inter01{
    public static void main(String[] args) {
        Ex05 me = new Ex05();
        Inter01 me2 = new Ex05();
        me2.func01();
        me2.func02();
        me2.func03();

    }
    @Override
    public void func01(){

    }
    @Override
    public void func02(){

    }
    @Override
    public void func03(){

    }
}

클래스와 인터페이스의 상속이 가능해진다.

interface Inter02{
    void func01();
}

interface Inter03{
    void func02();
}

interface Inter04 extends Inter02{} // 인터페이스에서 상속할때는 extends 사용
interface Inter05 extends Inter02, Inter03{} // 여러개의 인터페이스 상속 가능

interface Inter06{} // 인터페이스는 추상메서드를 생성하지 않아도 가능하다.

public class Ex06 implements Inter05{

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    public void func01() {

    }
    public void func02() {

    }
}

-> 인터페이스는 여러개의 상속이 가능한다.
-> 인터페이스끼리는 extends로 상속한다.

  • 인터페이스는 추상메서드만 가질 수 있다.

    • 생성자와 일반 구현 메서드, 필드는 가질 수 없다.
      • 하지만 static final 필드는 가질 수 있다.
interface Inter01{
    public static final int su = 1111;
    int su2 = 2222;
//    int su3; // 초기화를 하지 않으면 선언 불가능 (final이 숨겨져있기 때문에)

    public abstract void func01(); // interface는 추상 메서드만 가질 수 있다. 
    public void func02(); // abstract 키워드 생략가능
    void func02(); // override해서 사용해야 하기 때문에 public 생략가능
}

Cloneable 인터페이스가 붙은 클래스는 clone이 가능함

class Lec08 implements Cloneable{
    int su = 1111;

    void func() {
        System.out.println(su);
    }
}

public class Ex08 {

    public static void main(String[] args) throws Exception{
        int[] arr = {1, 3, 5};
        Object obj1 = arr.clone();

        Lec08 lec1 = new Lec08();
        Object obj2 = lec1.clone(); // 
    }
}
public class Ex09 implements Cloneable{

    public static void main(String[] args) throws Exception {
        Ex09 me = new Ex09();
        Object obj = me.clone();
        System.out.println(me);
        System.out.println(obj);
    }
}

java.lang 클래스

public class Ex10 {
    int a = 111;
    String msg = "abcd";

    public static void main(String[] args) throws Exception {
        // System
        // 출력
        java.io.PrintStream ps = System.out;        
        ps.println("출력");

        // Scanner
        java.io.InputStream is = System.in;
        java.util.Scanner sc;
        sc = new java.util.Scanner(is);
        ps.println(sc.nextLine());

        // 시간
        long time = System.currentTimeMillis(); // January 1, 1970 UTC부터 흐른 현재시간 출력
        System.out.println(time / 1000);
        long before = System.currentTimeMillis();
        for(int i = 0; i < 1000000; i++) {
            new Ex10();
        }
        long after = System.currentTimeMillis();
        System.out.println(after - before);

        // java 강제종료 
//        System.exit(0); // 뒤의 숫자는 아무 의미가 없다. - java는 VM에서 실행되기 때문.
        System.out.println("종료");

        // garbage collector
        System.gc(); // 대체로 io때 자동적으로 실행된다.

        // Math
        System.out.println(Math.random()); // final static 메서드
        int su = -11;
        System.out.println(Math.abs(su));
        System.out.println(Math.min(3,  5));
        System.out.println(Math.max(3,  5));
        double su2 = 3.14;
        System.out.println(Math.ceil(su2)); // 올림
        System.out.println(Math.floor(su2)); // 버림
        System.out.println(Math.round(su2)); // 반올림 

        // Class
        // - 자바 리플렉션
        Class cls; // 클래스에 대한 정보를 갖고있는 클래스
        Ex10 me = new Ex10();
        cls = me.getClass(); 
        Class cls2 = Ex10.class;
        Class cls3 = Class.forName(cls.getName());
        System.out.println(cls.getName());
        System.out.println(cls.getCanonicalName());
        System.out.println(cls.getTypeName());
        System.out.println(java.util.Arrays.deepToString(cls.getAnnotations()));
        System.out.println(java.util.Arrays.deepToString(cls.getConstructors()));  
        System.out.println(java.util.Arrays.deepToString(cls.getDeclaredAnnotations()));
        System.out.println(java.util.Arrays.deepToString(cls.getDeclaredConstructors()));
        System.out.println(java.util.Arrays.deepToString(cls.getDeclaredFields()));
        System.out.println(java.util.Arrays.deepToString(cls.getDeclaredMethods()));
        Object obj = cls.newInstance();
        System.out.println(((Ex10)obj).msg);
        Object obj2 = cls3.newInstance();
        System.out.println(((Ex10)obj2).msg);
    }
}