Item 52. Overloading
λ€μ€μ μλ μ μ€ν μ¬μ©νλΌ
λ©μλ λ€μ€ μ μ(overloading)λ μ΄λ¦μ κ°μ§λ§ λ§€κ°λ³μκ° λ€λ₯Έ μ¬λ¬ λ©μλλ₯Ό λ§νλ€. λ€μ€μ μλ νλ‘κ·Έλλ¨Έμκ² νΈμλ₯Ό μ 곡ν΄μ£Όμ§λ§, μ΄λ λ©μλκ° νΈμΆλ μ§ νΌλμ μ€ μ μλ€.
class CollectionClassifier {
public static String classify(Set<?> s) {
return "μ§ν©";
}
public static String classify(List<?> lst) {
return "리μ€νΈ";
}
public static String classify(Collection<?> c) {
return "κ·Έ μΈ";
}
public static void main(String[] args) {
Collection<?>[] collections = {
new HashSet<String>(),
new ArrayList<BigInteger>(),
new HashMap<String, String>().values()
};
for (Collection<?> c : collections) {
// μ»΄νμΌνμμ cλ νμ Collection<?> νμ
μ΄λ―λ‘ λͺ¨λ "κ·Έ μΈ" μΆλ ₯
System.out.println(classify(c));
}
}
}
μ μ½λμ μλλ λ§€κ°λ³μμ λ°νμ νμ μ λ°λΌ λ€λ₯΄κ² λμνκΈ°λ₯Ό κΈ°λν μ μμ§λ§, κΈ°λμλ λ€λ₯΄κ² μ»΄νμΌνμμ cλ νμ Collection<?> νμ μ΄λ―λ‘, λͺ¨λ "κ·Έ μΈ"λ₯Ό μΆλ ₯νλ€. λ§μ½ μλλλ‘ λμλκΈΈ μνλ€λ©΄, λͺ¨λ classify λ©μλλ₯Ό νλμ λ©μλλ‘ ν©μΉ ν instanceofλ‘ λ§€κ°λ³μμ νμ μ νμΈνλ©΄ λλ€.
class CollectionClassifier {
public static String classify(Collection<?> c) {
return c instanceof Set ? "μ§ν©" :
c instanceof List ? "리μ€νΈ" : "κ·Έ μΈ";
}
// ...
}
λ€μ€μ μ νΌλ ννΌ λ°©λ²
μμ κ°μ΄ νλ‘κ·Έλλ¨Έκ° μλν λλ‘ λμνμ§ μκΈ° λλ¬Έμ, μ μ΄μ νΌλμ μΌμΌν€λ μν©μ νΌνλ κ²μ΄ μ’λ€. μ μΌ μμ ν λ°©λ²μ λ§€κ°λ³μ μκ° κ°μ λ€μ€μ μλ λ§λ€μ§ μκ³ , κ°λ³μΈμλ₯Ό μ¬μ©νλ λ©μλλ λ€μ€μ μνμ§ μλ κ²μ΄λ€. λ§μ½ λ€μ€μ μκ° νμνλ€λ©΄ λ€μ€μ μ λμ μ λ©μλ μ΄λ¦μ λ€λ₯΄κ² μ§μ΄μ£Όλ λ°©λ²μ κ³ λ €ν΄λ³΄λ κ²λ μ’λ€.
// λ§€κ°λ³μ νμ
μ λ°λΌ λ€λ₯Έ μ΄λ¦μ μ¬μ©ν ObjectInputStreamμ λ©μλ
public class ObjectOutputStream
extends OutputStream implements ObjectOutput, ObjectStreamConstants {
// ...
public void writeChar(int val) throws IOException {
bout.writeChar(val);
}
public void writeInt(int val) throws IOException {
bout.writeInt(val);
}
public void writeLong(long val) throws IOException {
bout.writeLong(val);
}
public void writeFloat(float val) throws IOException {
bout.writeFloat(val);
}
// ...
}
μμ±μ
λ©μλλ μμ κ°μ΄ λ€λ₯Έ μ΄λ¦μΌλ‘ μ§μ΄μ£Όλ κ²μΌλ‘ νΌλμ ννΌν μ μμ§λ§, μμ±μλ μ΄λ¦μ λ€λ₯΄κ² μ§μ΄μ€ μ μλ€. λλ¬Έμ μμ±μμμ μλμ κ°μ λ°©λ²μΌλ‘ νΌλμ ννΌν μ μλ€.
μ μ ν©ν°λ¦¬ λ©μλ μ¬μ©μΌλ‘ μμ±μ μ§μ μ¬μ©μ λ체
λ§€κ°λ³μκ° λͺ ννκ² κ΅¬λΆλλλ‘ νμ¬ μ΄λ κ²μ΄ νΈμΆλ μ§ νΌλλμ§ μλλ‘ ν¨
ex) ArrayList(int initialCapacity) vs ArrayList(Collection<? extends E> c)
λ€μ€μ μ νΌλμΌλ‘ μκΈ°λ μμ
List.remove(int index) vs List.remove(Object o)
μ λ€λ¦κ³Ό μ€ν λ°μ±μ λ±μ₯μΌλ‘ λ€μ€μ μ νΌλμ΄ λμ± μ¬ν΄μ‘λλ°, λνμ μΌλ‘ Listμ remove λ©μλκ° μλ€.
class SetList {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();
List<Integer> list = new ArrayList<>();
for (int i = -3; i < 3; i++) {
set.add(i);
list.add(i);
}
for (int i = 0; i < 3; i++) {
set.remove(i);
list.remove(i);
}
System.out.println(set + " " + list);
// μμ: [-3, -2, -1] [-3, -2, -1]
// μ€μ : [-3, -2, -1] [-2, 0, 2]
}
}
setμ remove(Object o)
λ©μλκ° νΈμΆλμ΄ μλλλ‘ ν΄λΉ κ°μ²΄λ₯Ό μμ νμ§λ§,
listλ remove(Object o)
κ° μλ remove(int index)
λ©μλκ° λ€μ€μ μλμ΄ μκΈ° λλ¬Έμ μ§μ ν μΈλ±μ€μ κ°μ²΄λ₯Ό μμ νκ² λλ€.
(λ§μ½ μλλλ‘ λμνκΈΈ μνλ€λ©΄, list.remove(i)
λμ list.remove((Integer) i)
λ₯Ό μ¬μ©νλ©΄ λλ€.)
Last updated
Was this helpful?