본문 바로가기

전체 글

(157)
인터페이스 활용 | 22/01/03 코드 작성 package ex_interface; public class Ex3 { public static void main(String[] args) { /* * 인터페이스 * - 인터페이스끼리도 상속이 가능 */ } } interface ParentInterface1 { public abstract void parentMethod1(); } interface ParentInterface2 { public abstract void parentMethod2(); } // ChildInterface 인터페이스 정의 - ParentInterface1, ParentInterface2 // 주의 1. 인터페이스간의 상속은 extends 키워드 사용 // 주의 2. 부모인터페이스를 상속받은 자식인터페이스에서 추..
인터페이스(Interface) | 22/01/03 코드 작성 package ex_interface; // 인터페이스 정의 public interface RemoteControl { /* * 인터페이스 * * [접근제한자] interface 인터페이스명 { * * // 상수 선언 * // 추상메서드 정의 * * // 1.8 이후 * // 디폴트 메서드 정의 * // 정적 메서드 정의 * * } */ // 상수 public static final int MAX_VOLUME = 100; public static final int MIN_VOLUME = 0; int MAX_CHANNEL = 100; // 상수(static final 생략되어 있음) public int MIN_CHANNEL = 1; // 상수 // => 인터페이스 내의 모..
상수 | 22/01/03 코드 작성 package ex_final; public class Ex2 { public static void main(String[] args) { System.out.println(Earth.EARTH_RADIUS + " km"); System.out.println(Earth.EARTH_AREA + " km^2"); System.out.println("----------------------------------------"); /* * 어떤 클래스에 데이터를 저장한 후 해당 데이터를 판별해야하는 경우 * 데이터 대한 문자열이나 정수 데이터 비교 시 비교 데이터를 잘못 지정하거나, * 데이터 저장 시 잘못 저장할 경우로 인해 실제 비교할 때 올바른 비교가 수행되지 않을 수 있다! */ Car car ..
final | 22/01/03 코드 작성 package ex_final; public class Ex1 { public static void main(String[] args) { /* * final 키워드 * - 클래스, 메서드, 멤버변수에 지정 가능 * * 1) 멤버변수에 final이 붙을 경우 * - 기본 문법 : final 데이터타입 변수명; * - ex) 원주율 계산을 위한 파이(PI)값은 변경되면 안되므로 변수에 final 표기 * * 2) 메서드에 final이 붙을 경우 * - 기본 문법 : [접근제한자] final 리턴타입 메서드명([매개변수...) {} * * 3) 클래스에 final이 붙을 경우 * - 기본 문법 : [접근제한자] final class 클래스명 {} * - 어떤 클래스 자체로 이미 완전한 클래스 기능..
싱글톤(Singleton) 활용 | 21/12/30 코드 작성 package ex_singleton; public class Test1 { public static void main(String[] args) { // 생성된 인스턴스를 두 번 가져오기 //SingletonTest st = new SingletonTest(); // 인스턴스 생성 불가 SingletonTest st = SingletonTest.getInstance(); SingletonTest st2 = SingletonTest.getInstance(); System.out.println(st.num + ", " + st2.num); // 10, 10 출력 // 인스턴스 내의 인스턴스 변수 값을 변경하면 나머지도 공유됨 st.num = 100; System.out.println(st.num..
상속(Polymorphism) 활용 | 21/12/30 코드 작성 package ex_polymorphism; public class Test { public static void main(String[] args) { Tv tv = new Tv(); Computer com = new Computer(); Buyer b = new Buyer(); System.out.println("현재 잔액은 " + b.getMoney() + " 원 입니다!"); System.out.println("현재 보너스 포인트는 " + b.getBonusPoint() + " 점 입니다!"); System.out.println("============================================================"); b.cart(com); b.cart(com)..
싱글톤(Singleton) 디자인 패턴 | 21/12/29 코드 작성 package ex_static; public class Ex4 { public static void main(String[] args) { /* * 싱글톤 디자인 패턴(Singleton Design Patten) * - 전체 프로그램에서 단 하나의 객체만 만들도록 보장하는 코딩 기법 */ //SingletonClass sc = new SingletonClass(); // // 접근제한자가 private으로 선언된 생성자로써 호출이 불가능하므로 // SingletonClass의 인스턴스 생성이 불가능해짐! // static으로 선언된 정적 멤버변수 instance에 접근하여 // 미리 생성되어 있는 인스턴스를 가져올 수 있다! //SingletonClass sc = SingletonClass...
static, main, instance 생성 시점 차이 | 21/12/29 코드 작성 package ex_static; public class Ex3 { /* * static 멤버와 인스턴스 멤버의 생성 시점 차이 * - static 멤버는 클래스 내에서 위치와 상관없이 순차적으로 로딩됨 * - static 멤버 로딩이 끝난 후 main() 메서드 호출됨 * - main() 메서드 내에서 인스턴스 생성시 인스턴스 멤버가 로딩 * - 인스턴스 멤버 로딩이 끝난 후 생성자 호출됨 */ public int b = callB(); // 인스턴스 변수 // => 인스턴스 b 생성 시점에 로딩되어 callB() 메서드를 호출 public int callB() { System.out.println("인스턴스 변수 b 로딩!"); return 0; } public static int a = ..