- Today
- Total
- tree
- μ‘Έμ μν
- μΈν΄
- λ°±μλ
- 그리λ
- λ°μ΄ν°λ² μ΄μ€
- νλ‘κ·Έλλ¨Έμ€
- MST
- PS
- Graph
- μλ°μμ μ
- leetcode
- dp
- μμμ λ ¬
- μλ£κ΅¬μ‘°
- BFS
- μλ°
- spring
- array
- Algorithm
- CS
- java
- database
- OOP
- 벨λ§ν¬λ
- ꡬν
- λ°±μ€
- λ¬Έλ²
- pytorch
- λ€μ΅μ€νΈλΌ
Partially Committed
[CH12] Generics, Enumeration, Annotation λ³Έλ¬Έ
λ³Έ ν¬μ€ν μ μλ°μ μ μ κ΅μ¬λ₯Ό 곡λΆνλ©°, κ°λ¨ν μ 리/κΈ°λ‘ μ©λλ‘ μμ±νμμ΅λλ€. νΉμ¬, μλͺ»λ λ΄μ©μ΄ μλ€λ©΄ μ§μ ν΄μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€.
1. Generics
Generics λ λ€μν νμ μ κ°μ²΄λ€μ λ€λ£¨λ λ©μλλ Collection class μ compile μ type checking μ ν΄μ£Όλ κΈ°λ₯μ΄λ€. κ°μ²΄ νμ μ compile - time μ check νλ―λ‘ νλ‘κ·Έλ¨ μμ μ±(μλμΉ μμ νμ μ κ°μ²΄κ° μ μ₯λκ³ , λΆλ¬μ¬ λ μλͺ»λ μΊμ€ν μΌλ‘ μΈν΄ λ°μνλ μ€λ₯κ° μ€μ΄λ¦)μ΄ λμμ§κ³ νλ³ν λ±μ μμ μ μμ μ€λ€. λ€λ£¨κ³ μ νλ κ°μ²΄μ νμ μ 미리 λͺ μνμ¬ νμ μμ μ±μ λμ΄κ³ μ½λκ° κ°κ²°ν΄μ§λ€.
class Box<T> {
T item;
void setItem (T item) { this.item = item; }
T getItem() { return item; }
}
μμ κ°μ Generic class μμ T λ type variable μ΄λΌκ³ νλ€. μμ κ°μ΄ μ μΈν Box Class μ Object λ μλμ κ°μ΄ μμ±ν μ μλ€.
Box<String> b = new Box<String>();
b.setItem(new Object());
b.setItem("ABC");
string item = b.getItem();
Box ν΄λμ€λ class Box<T> { } μ κ°μ΄ μ μΈλμ΄ μλ€. Box<T> λ Generic class μ΄λ©° Tμ Box νΉμ T Box λΌκ³ μ½λλ€. T λ type variable νΉμ type parameter μ΄λ©° Box λ raw type(μμ νμ ) μ΄λΌκ³ λΆλ₯Έλ€. Box κ°μ²΄ μμ± μ, <T> μ리μ <String> μ μμλ‘ μ μΈνμλλ°, μ΄λ String μ parameterized type μ΄λΌκ³ νλ€.
Limitiation of Generics
T[] itemArr; μ κ°μ΄ λ°°μ΄ νμ μ μ°Έμ‘° λ³μλ₯Ό μ μΈνλ κ²μ κ°λ₯νμ§λ§ new T[10] κ³Ό κ°μ΄ λ°°μ΄μ μμ±νλ κ²μ νμ©λμ§ μλλ€. Generic μ run - time μ€μ κ²°μ λλλ°, new operator μ compile λ¨κ³μμ type κ° λ¬΄μμΈμ§ μ νν μμμΌλ§ νλ―λ‘ νμ©νμ§ μλ κ²μ΄λ©°, instanceof operator λ λμΌν μλ¦¬λ‘ μλλ€. generic array λ₯Ό μμ±ν΄μΌ νλ νμκ° μλ€λ©΄, new operator λμ μ Reflection API μ newInstance() μ²λΌ λμ κ°μ²΄ μμ± λ©μλλ‘ λ°°μ΄μ λ§λ€κ±°λ, Object λ°°μ΄μ μμ±ν λ€μμ T[] λ‘ νλ³ννλ κ²μ΄ λμμ΄λ€.
Generic class μ κ°μ²΄ μμ±κ³Ό μ¬μ©
import java.util.ArrayList;
class source{
public static void main(String[] args) {
Box<Fruit> fruitBox = new Box<Fruit>();
Box<Apple> appleBox = new Box<Apple>();
Box<Toy> toyBox = new Box<Toy>();
fruitBox.add(new Fruit());
fruitBox.add(new Apple());
appleBox.add(new Apple());
appleBox.add(new Apple());
toyBox.add(new Toy());
System.out.println(fruitBox);
System.out.println(appleBox);
System.out.println(toyBox);
}
}
class Fruit { public String toString() { return "Fruit"; }}
class Apple extends Fruit { public String toString() { return "Apple"; }}
class Grape extends Fruit { public String toString() { return "Grape"; }}
class Toy { public String toString() { return "Toy"; }}
class Box<T>{
ArrayList<T> list = new ArrayList<T>();
void add(T item) { list.add(item);}
T get(int i) { return list.get(i);}
int size() { return list.size();}
public String toString(){ return list.toString();}
}
μ νλ Generic class
type λ¬Έμλ‘ μ¬μ©ν type λ₯Ό λͺ μνλ©΄ ν μ’ λ₯μ type λ§ μ μ₯ν μ μλλ‘ μ νν μ μλ€. νμ§λ§ type μλ μ νμ΄ μλλ°, νμ 맀κ°λ³μ T μ μ§μ ν μ μλ νμ μ μ’ λ₯λ₯Ό μ νν μ μμκΉ? μ΄λ generic νμ μ extends ν€μλλ₯Ό μ¬μ©νλ©΄ νΉμ νμ μ μμλ€λ§ λμ ν μ μλλ‘ μ νν μ μλ€. ν΄λμ€κ° μλ μΈν°νμ΄μ€λ₯Ό ꡬνν΄μΌ νλ€λ μ μ½μ΄ νμνλλΌλ implements ν€μλκ° μλ extends ν€μλλ₯Ό μ¬μ©ν¨μ μ£Όμνμ.
import java.util.ArrayList;
class Fruit implements Eatable{
public String toString() { return "Fruit"; }
}
interface Eatable{ }
class FruitBox<T extends Fruit & Eatable> extends Box<T> {}
class source{
public static void main(String[] args) {
FruitBox<Fruit> fruitBox = new FruitBox<Fruit>();
FruitBox<Apple> appleBox = new FruitBox<>();
FruitBox<Grape> grapeBox = new FruitBox<>();
fruitBox.add(new Fruit());
fruitBox.add(new Apple());
fruitBox.add(new Grape());
appleBox.add(new Apple());
grapeBox.add(new Grape());
System.out.println("fruitBox-" + fruitBox);
System.out.println("appleBox-" + appleBox);
System.out.println("grapeBox-" + grapeBox);
}
}
μμΌλ μΉ΄λ
Generic νμ μ΄ λ€λ₯Έ κ² λ§μΌλ‘λ μ€λ²λ‘λ©μ΄ μ±λ¦½νμ§ μλλ€.Generic νμ μ λ€λ₯΄κ² λ©μλλ₯Ό ꡬννλ €λ©΄ μμΌλ μΉ΄λλ₯Ό μ¬μ©νλ€.
<? extends T> μμΌλ μΉ΄λμ μν μ ν. Tμ κ·Έ μμλ€λ§ κ°λ₯
<? super T> μμΌλ μΉ΄λμ νν μ ν. Tμ κ·Έ μ‘°μλ€λ§ κ°λ₯
<?> μ ν μμ. λͺ¨λ νμ μ΄ κ°λ₯νλ©° <? extends Object> μ λμΌν¨
import java.util.ArrayList;
class Fruit implements Eatable{
public String toString() { return "Fruit"; }
}
interface Eatable{ }
class FruitBox<T extends Fruit & Eatable> extends Box<T> {}
class Juicer {
static Juice makeJuice(FruitBox<? extends Fruit> box){
String tmp = "";
for(Fruit f : box.getList())
tmp += f + " ";
return new Juice(tmp);
}
}
class Juice{
String name;
Juice(String name){this.name = name + "Juice";}
public String toString() {return name;}
}
class source{
public static void main(String[] args) {
FruitBox<Fruit> fruitBox = new FruitBox<Fruit>();
FruitBox<Apple> appleBox = new FruitBox<>();
fruitBox.add(new Apple());
fruitBox.add(new Grape());
appleBox.add(new Apple());
appleBox.add(new Apple());
System.out.println(Juicer.makeJuice(fruitBox));
System.out.println(Juicer.makeJuice(appleBox));
}
}
class Apple extends Fruit { public String toString() { return "Apple"; }}
class Grape extends Fruit { public String toString() { return "Grape"; }}
class Toy { public String toString() { return "Toy"; }}
class Box<T>{
ArrayList<T> list = new ArrayList<T>();
void add(T item) { list.add(item);}
T get(int i) { return list.get(i);}
int size() { return list.size();}
ArrayList<T> getList() { return list; }
public String toString(){ return list.toString();}
}
Generic method
λ©μλ μ μΈλΆμ generic type μ΄ μ μΈλ method λ₯Ό λ§νλ€. νλ μ μν μ μ Generic method λ₯Ό νΈμΆν λ, λμ λ type λ₯Ό μλ΅ν μ μλ κ²½μ°μλ μ°Έμ‘°λ³μλ ν΄λμ€ μ΄λ¦λ μλ΅ν μ μλ€λ κ²μ΄λ€. Generic method λ₯Ό μ΄μ©νλ©΄ 맀κ°λ³μμ type μ΄ λ³΅μ‘ν κ²½μ° μ μ©νλ€.
2. Enumeration
μ΄κ±°ν Direction μ΄ μλμ²λΌ μ μλμ΄μλ€κ³ κ°μ νμ.
enum Direction { EAST, SOUTH, WEST, NORTH }
μμ μ΄κ±°ν μμ κ°κ°μ΄ Direction κ°μ²΄μ΄λ©°, μλ₯Ό class λ‘ μ μνκ³ μ νλ€λ©΄ μλμ κ°μ κ²μ΄λ€.
class Direction {
static final Direction EAST = new Direction("EAST");
static final Direction SOUTH = new Direction("SOUTH");
static final Direction WEST = new Direction("WEST ");
static final Direction NORTH = new Direction("NORTH ");
private String name;
private Direction(String name) { this.name = name; }
}
Direction Class μ static μμ EAST, SOUTH, WEST, NORTH μ κ°μ κ°μ²΄μ μ£Όμμ΄κ³ λ°λμ§ μμΌλ―λ‘ == λ‘ λΉκ΅κ° κ°λ₯νλ€. λν λͺ¨λ μ΄κ±°νμ abstract class Enum μ μμμ΄λ―λ‘, λλ΅ μλμ κ°μ΄ ꡬνλμ΄μλ€.
abstract class MyEnum <T extends MyEnum<T>> implements Comparable<T> {
static int id = 0;
int ordinal;
String name = "";
public int ordinal() { return ordinal; }
MyEnum(String name){
this.name = name;
ordinal = id++;
}
public int compareTo(T t){
return ordinal - t.ordinal();
}
}
MyEnum ν΄λμ€λ₯Ό <T extends MyEnum<T>> μ κ°μ΄ ν΄μ£ΌμκΈ° λλ¬Έμ, νμ T λ 무쑰건 MyEnum<T> μ μμμ΄μ΄μΌ νλ μ μ½μ΄ κ±Έλ¦¬κ² λκ³ , μ΄λ¬ν μ΄μ λ‘ compareTo λ₯Ό κ°λ¨νκ² μμ±ν μ μμλ κ²μ΄λ€.
3. Annotation
μμ€μ½λ μμ μ£Όμμ²λΌ νλ‘κ·Έλ¨μ μν₯μ μ£Όμ§ μμΌλ©΄μλ λ€λ₯Έ νλ‘κ·Έλ¨μκ² μ μ©ν μ 보λ₯Ό μ 곡νλ λ©λͺ¨λ₯Ό λ§νλ€.
νμ€ μ λν μ΄μ
1. @Override
2. @Deprecated
3. @SuppressWarnings
4. @SafeVarargs
5. @Functionalinterface
6. @Native
λ©ν μ λν μ΄μ
7. @Taget
8. @Documented
9. @Inherited
10. @Retention
11. @Repeatable
References
http://www.yes24.com/Product/Goods/24259565
'π» Study ! > JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[CH14] Lambda & Stream (0) | 2022.09.03 |
---|---|
[CH11] Collections framework (2) | 2022.08.27 |
[CH09] java.lang ν¨ν€μ§ (0) | 2022.08.21 |
[CH08] μμΈμ²λ¦¬(Exception handling) (0) | 2022.08.02 |
[CH07] κ°μ²΄μ§ν₯νλ‘κ·Έλλ°4 (OOP) (0) | 2022.08.02 |