728x90
반응형
제어자 사용가능 범위 정리 |
||
접근제어자 (Access Modifier) | 기타제어자(Modifier) | |
public, protected, default, private | final, static, abstract | |
클래스 | public, default만 | static제외한 모두 (단, 이너 클래스 경우 예외) |
지역변수 | x | final만 |
멤버필드 | public, protected, default, private | final, static |
메소드 | public, protected, default, private | final, static, abstract |
생성자 | public, protected, default, private | x |
초기화 블록 | x | static만 |
동시 사용불가 | private(상속제한) abstract (상속필수) final(상속불가) abstract(상속필수) static(상속제한) abstract (상속필수) |
generic_000_none
package generic_000_none; public class BoxExample { public static void main(String[] args) { Box box = new Box(); box.set("홍길동"); String name = (String) box.get(); box.set(new Apple()); Apple apple = (Apple) box.get(); apple.setNum(100); System.out.println(apple.getNum()); } }
package generic_000_none; public class Apple { int num; public int getNum() { return num; } public void setNum(int num) { this.num = num; } }
package generic_000_none; public class Box { private Object object; public Object get() { return object; } public void set(Object object) { this.object = object; } }
100
generic_001_yes
package generic_001_yes; public class BoxExample { public static void main(String[] args) { Box<String> box1 = new Box<String>(); box1.set("hello"); System.out.println(box1.get()); Box<Integer> box2 = new Box<Integer>(); //Integer int Wrapper Class box2.set(6); System.out.println(box2.get()); } }
package generic_001_yes; public class Box<T>{ private T t; public T get() { return t; } public void set (T t) { this.t=t; } }
hello 6
generic_002_yes
package generic_002_yes; public class ProductExample { public static void main(String[] args) { Product<Tv, String> product1 = new Product<Tv, String> (); product1.setKind(new Tv("런닝맨", 6, 15, 50)); product1.setModel("Tv자료형"); Tv tv = product1.getKind(); System.out.println(product1.getModel()); System.out.println(tv.toString()); System.out.println(tv); //주소값만 출력해도 Override된 toString이 자동실행됨. Product<Car, String> product2 = new Product<Car, String> (); product2.setKind(new Car()); product2.setModel("Car자료형"); Car car = product2.getKind(); System.out.println(product2.getModel()); System.out.println(car); } }
package generic_002_yes; public class Car { String carName; String carProduct; int carYear; int maxLoad; public Car() { } public Car(String carName, String carProduct, int carYear, int maxLoad) { super(); this.carName = carName; this.carProduct = carProduct; this.carYear = carYear; this.maxLoad = maxLoad; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public String getCarProduct() { return carProduct; } public void setCarProduct(String carProduct) { this.carProduct = carProduct; } public int getCarYear() { return carYear; } public void setCarYear(int carYear) { this.carYear = carYear; } public int getMaxLoad() { return maxLoad; } public void setMaxLoad(int maxLoad) { this.maxLoad = maxLoad; } @Override public String toString() { return "Car [carName=" + carName + ", carProduct=" + carProduct + ", carYear=" + carYear + ", maxLoad="+ maxLoad + "]"; } }
package generic_002_yes; public class Product <T, M>{ private T kind; private M model; public T getKind() { return kind; } public void setKind(T kind) { this.kind = kind; } public M getModel() { return model; } public void setModel(M model) { this.model = model; } }
package generic_002_yes; public class Tv { String programName; int channel; int volumn; int light; public Tv() {} public Tv(String programName, int channel, int volumn, int light) { this.programName = programName; this.channel = channel; this.volumn = volumn; this.light = light; } public String getProgramName() { return programName; } public void setProgramName(String programName) { this.programName = programName; } public int getChannel() { return channel; } public void setChannel(int channel) { this.channel = channel; } public int getVolumn() { return volumn; } public void setVolumn(int volumn) { this.volumn = volumn; } public int getLight() { return light; } public void setLight(int light) { this.light = light; } @Override public String toString() { return "Tv [programName=" + programName + ", channel=" + channel + ", volumn=" + volumn + ", light=" + light + "]"; } }
Tv자료형 Tv [programName=런닝맨, channel=6, volumn=15, light=50] Tv [programName=런닝맨, channel=6, volumn=15, light=50] Car자료형 Car [carName=null, carProduct=null, carYear=0, maxLoad=0]
728x90
반응형
'[JAVA]' 카테고리의 다른 글
[17일차] Generic Wildcard Collection Framework (0) | 2024.01.04 |
---|---|
[16일차 오후] 초기화블록 Generic Type (0) | 2024.01.03 |
[16일차 오전] lotto Calendar SimpleDateFormat LocalDateTime DateTimeFormatter (0) | 2024.01.03 |
[15일차 오후] Scanner StringTokenizer Arrays binarySearch (0) | 2024.01.02 |
[15일차 오전] Math random split StringBuffer (0) | 2024.01.02 |