-
728x90๋ฐ์ํ
Polymorphism
<1> ์ ์
๋คํ์ฑ์ด๋ ํ๋์ ๊ฐ์ฒด๊ฐ ๋ง์ ํ(ํ์ )์ ๊ฐ์ง ์ ์๋ ์ฑ์ง์ด๋ค. ๋คํ์ฑ์, ์์๊ด๊ณ์ ์์ ๋ ์กฐ์ํด๋์ค์ ํ์ ์ผ๋ก ์์ ํด๋์ค ๊ฐ์ฒด๋ฅผ ๋ ํผ๋ฐ์ค ํ ์ ์๋ค.
ex) ํฅ๋ถ์ด๋นต is a ๋ถ์ด๋นต | ์ํฌ๋ฆผ๋ถ์ด๋นต is a ๋ถ์ด๋นต | ํผ์ ๋ถ์ด๋นต is a ๋ถ์ด๋นต
<2> ํ์ฉ
- ๋คํ์ฑ์ผ๋ก ๋ค๋ฅธ ํ์
์ ๋ฐ์ดํฐ๋ฅผ ํ๋์ ๋ฐฐ์ด๋ก๊ด๋ฆฌํ ์ ์๋ค
public ArrayList(int initialCapacity){ if(initialCapcity>0){this.elementData = new Object [initialCapacity];} else if (initialCapacity ==0 ){this.elementData = EMPTY_ELEMENTDATA;} else{throw new IllegalArgumentException ("Illegal Capcity: " + initialCapacity);}}
- ๋ํ์ ์ธ ์๋ก, ์ค๋ธ์ ํธ ํด๋์ค๋ฅผ ๋ณผ ์ ์๋ค. ์ค๋ธ์ ํธ ํด๋์ค๋ ๋ชจ๋ ํด๋์ค์ ์กฐ์์ธ๋ฐ, ์ค๋ธ์ ํธ์ ๋ฐฐ์ด์ ์ด๋ค ํ์ ์ ๊ฐ์ฒด๋ผ๋ ๋ค ์ ์ฅํ ์ ์๊ธฐ ๋๋ฌธ์ ์๋ฐ์ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๊ฐ๋จํ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค.
- ๋งค๊ฐ๋ณ์์ ๋คํ์ฑ
public void println (Object x){ //Object ๋ฅผ ํ์ฑํ๊ณ ์์ด์ ์ถ๋ ฅ์ด ๋๊ณ ์๋๊ฑฐ์ String s = String.valueOf(x); synchronized(this){print(s); newLine();}}
- ๋ฉ์๋๊ฐ ํธ์ถ๋๊ธฐ ์ํด์๋ ๋ฉ์๋ ์ด๋ฆ๊ณผ ํ๋ผ๋ฏธํฐ๊ฐ ๋ง์์ผ ํ๋๋ฐ, println ์๋ ๋ฌด์์ ๋ฃ์ด๋ ์ถ๋ ฅ์ด ๋๋ค → ์๊ทธ๋ด๊น? ์๋๋ println์ ์์ค ์ฝ๋์ด๋ค
๋คํ์ฑ๊ณผ ์ฐธ์กฐํ ๊ฐ์ฒด์ ํ๋ณํ
๋ฉ๋ชจ๋ฆฌ์ ์๋๋ผ๋ ์ฐธ์กฐํ๋ ๋ณ์์ ํ์ ์ ๋ฐ๋ผ ์ ๊ทผํ ์ ์๋ ๋ด์ฉ์ด ์ ํ๋๋ค
Method Area: ํด๋์ค ๋ก๋ฉ ์ ์์ฑ - ํด๋์ค ์์ฒด ์ ๋ณด๋ง ์ฌ๋ผ๊ฐ๋ค JVM Stacks : static ๋ณ์/ ๋ฉ์๋๊ฐ ์์ธ๋ค Heap : non-static ๋ณ์/ ๋ฉ์๋๊ฐ ์์ธ๋ค. new keyword ๊ฐ ์ฐ์ธ ์ ๋ค์ด ์ฌ๊ธฐ๋ก ์จ๋ค → ๋ ๋ฆฝ์
https://junebee.tistory.com/6 ์ฐธ๊ณ
Variables
1. ์์ (1) ์ ์ ์์๋ ํ๋ฒ ์ ์ฅํ ๋ฐ์ดํฐ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค format: final datatype VARNAME; public static void main(String[] args){ //๋ณ์ int age = 30; String name = June; /*age = 20; ๋ก ์์ ๊ฐ..
junebee.tistory.com
- ์ฐธ์กฐํ ๊ฐ์ฒด์ ํ ๋ณํ
- ๋ฌต์์ ์บ์คํ
: child → super
์์ ํ์ ์ ๊ฐ์ฒด๋ฅผ ์กฐ์ ํ์ ์ผ๋ก ์ฐธ์กฐํ ๋์๋ ํ๋ณํ ์๋ต์ด ๊ฐ๋ฅํ๋ค
๐งธ์กฐ์์ ๋ชจ๋ ๋ด์ฉ์ด ์์์๊ฒ ์๊ธฐ ๋๋ฌธ์ ๊ฑฑ์ ํ ํ์ ์๋ค - ๋ช
์์ ์บ์คํ
: super → child
์กฐ์ ํ์ ์ ์์ํ์ ์ผ๋ก ์ฐธ์กฐํ ๋์๋ ํ๋ณํ ์๋ต์ด ๋ถ๊ฐ๋ฅํ๋ค ์กฐ์์ ๋ฌด์์ ์์์ผ๋ก ๋ฐ๊ฟ ์ ์์ผ๋ ์ฃผ์ํ์ → instanceof ์ฐ์ฐ์๋ฅผ ์ด์ฉํ์ฌ ์ค์ ๋ฉ๋ชจ๋ฆฌ์ ์๋ ๊ฐ์ฒด๊ฐ ํน์ ํด๋์ค ํ์ ์ธ์ง boolean์ผ๋ก ๋ฆฌํดํ ํ true ์ผ๋์๋ง ์บ์คํ ํ ๊ฒ
์๋์ ์ฝ๋๋ super → child ๋ก์ ํ๋ณํํ ์ฝ๋์ด๋ค. ๋ช ์์ ์บ์คํ ์, instanceof ๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ๋ก ์บ์คํ ์ด ๊ฐ๋ฅํ์ง ์๋์ง ํ์ธ์ ํ์ง ์๋๋ค๋ฉด, ์๋ฐ๋ ์๋ ค์ฃผ์ง ์๊ธฐ ๋๋ฌธ์ ์ ์คํ๊ฒ ์จ์ผํ๋ค.
- ๋ฌต์์ ์บ์คํ
: child → super
Pokemon pikachu = new Pokemon (); if (pikachu instanceof FairyType) { FairyType ft = (FairyType) pikachu; } //์์ ์ฝ๋์ฒ๋ผ instance of ๋ฅผ ์ฐ์ง ์์์ ๊ฒฝ์ฐ์๋, Pokemon phantom = new Pokemon (); FairyType ft2 = (FairyType) phantom; phantom.angelicKiss(); //compile error ๋ฐ์์ด ์๋จ. ์์นซํ๋ฉด ์ค์ท ๋๋ค ํ๋ค๊ฐ ํ๋ก๊ทธ๋จ ๋ถ์ค์๋ ์๋ค..
2. ์ฐธ์กฐ ๋ณ์์ ๋ ๋ฒจ์ ๋ฐ๋ฅธ ๊ฐ์ฒด์ ๋งด๋ฒ ์ฐ๊ฒฐ
1. ์์ ๊ด๊ณ์์ ๊ฐ์ฒด์ ๋งด๋ฒ ๋ณ์๊ฐ ์ค๋ณต๋ ๋, ์ฐธ์กฐ๋ณ์์ ํ์ ์ ๋ฐ๋ผ ์ฐ๊ฒฐ์ด ๋ฌ๋ผ์ง
(2) ์์๊ด๊ณ์์ ๋ฉ์๋๊ฐ ์ค๋ณต๋ ๋ (Method Overriding) : ๋ฌด์กฐ๊ฑด ์์ ํด๋์ค์ ๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉฐ, ์ต๋ํ ๋ฉ๋ชจ๋ฆฌ์ ์์ฑ๋ ์ค์ฒด ๊ฐ์ฒด์ ์ต์ ํ ๋ ๋ฉ์๋๊ฐ ๋์ํ๋ค (์ฆ, overriding ๋ ๋ฉ์๋๊ฐ ์๋ค๋ฉด, ๊ทธ ๋ฉ์๋๋ฅผ ์ถ๋ ฅํ๋ ๊ฒ)์๋์ ์ฝ๋๋ ๋๋ค ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค. ์ด๋ println ์ toString() override ๋๋ฌธ์ด๋ค
System.out.println(Phantom.toString()); System.out.println(Phantom);
<3> Generics
Generics๋ ๋ค์ํ ํ์ ์ ๊ฐ์ฒด๋ฅผ ๋ค๋ฃจ๋ ๋ฉ์๋, ์ปฌ๋ ์ ํด๋์ค์์ ์ปดํ์ผ ์์ ํ์ ์ ์ฒดํฌํ๋ค ์ฆ, ๋ฏธ๋ฆฌ ํ๋ณํ์ ํ์ง ์์๋ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ ๊ฒ์ด๋ค.
ํํ
- ํด๋์ค ๋๋ ์ธํฐํ์ด์ค ์ ์ธ ์ <> ์ ํ์ ํ๋ผ๋ฏธํฐ๋ฅผ ํ์
className : Raw Type
className<T> : Generic Typepublic class ClassName<T> {} public interface InterfaceName<T> {}
2. ํ์ ํ๋ผ๋ฏธํฐ ์์์ ์ฐธ์กฐํ ํ์ ์ ๋ปํจ
๐คฆ๐ปโ๏ธ T : reference Type
E : Element
K : Key
V : Valuepublic class ArrayList<E> extends AbstractList<E> implements List<E>, RnadomAccess, Cloneable {...} public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Serializable{...}
3. ๊ฐ์ฒด ์์ฑ ๋ณ์์ชฝ๊ณผ ์์ฑ ์ชฝ์ ํ์ ์ ๋ฐ๋์ ๊ฐ์์ผ ํ๋ค
ClassName <String> generic1 = new ClassName<String>(); List<Integer> generic2 = new ArrayList<Integer>(); List<String> generic3 = new ArrayList<Character>(); //ํ๋ฆผ
์์
public class GenericClass<T> { private T something; public T getSomething(){return something;} public void setSome(T something){this.something = something;}}
public class Test{ private static void genericTest(){ //< > ์์ ์ํ๋ ํ์ ์ ๋ฃ๊ธฐ GenericClass <String> gStr = new GnericClass<>(); //String ์ด๋ผ๊ณ ์ ์ํ๋ฉด..? GenericClass <Integer> gInt = new GenericClass<> (); //Integer๋ ๊ฐ๋ฅ,,?! //์ํ๋ ํ์ ์ง์ด ๋ฃ๊ธฐ gStr.setsomething("String something์ด๋ผ๊ณ ์ ํด์ ธ์ ๋์..!");} gInt.setsomething("Integer something ์ด๋ผ๊ณ suggest ๋!"); //์ํ๋ ํ์ ๋์ง์ด ๋ด๊ธฐ String gStr2 = gStr.getSomething(); System.out.println(gStr2); //์ค ๋บ๋๋ string ๋ง ๊ฐ๋ฅํด์ก์ด ! } } //์ปดํ์ผ ํ์ ์ ํ์ ํ๋ผ๋ฏธํฐ๋ค์ด ๋์ ๋ ํ์ ์ผ๋ก ๋์ฒด๋จ!
์ฌ์ฉ
- Type Parameter ์ ์ ํ ํ์์ ๋ฐ๋ผ ๊ตฌ์ฒด์ ์ธ ํ์
์ ํ์ด ํ์ํ๋ค.
ex: ๊ณ์ฐ๊ธฐ ํ๋ก๊ทธ๋จ ๊ตฌํ ์ Number ์ดํ์ ํ์ (byte, short, interger..) ๋ก๋ง ์ ํํด์ผ ํ๋ค
type parameter ์ ์ธ ๋ค extends ์ ํจ๊ป ์์ ํ์ ์ ๋ช ์ํ์ฌ ์ ํํ๋ค.
class Number< T extends Number>{} //T๋ Number ๋ฅผ ์์ ๋ฐ์์ผ ํ๋ค
2. ์ธํฐํ์ด์ค๋ก ์ ํํ ๊ฒฝ์ฐ๋ extends ์ฌ์ฉ
3. ํด๋์ค์ ํจ๊ป ์ธํฐํ์ด์ค ์ ์ฝ ์กฐ๊ฑด์ ์ด์ฉํ ๊ฒฝ์ฐ & ๋ก ์ฐ๊ฒฐ
class TypeRestrcit1 <T extends Cloneable> (); class TypeRestrict2 <T extends Number & Cloneable & Comparable<String>>{}
728x90๋ฐ์ํ'๐ STUDY > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
QUEUE (0) 2022.05.18 ์บก์ํ (0) 2022.05.17 ์์ (0) 2022.05.17 ์ถ์ํ (0) 2022.05.17 ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ (1) 2022.05.17 ๋๊ธ
- ๋คํ์ฑ์ผ๋ก ๋ค๋ฅธ ํ์
์ ๋ฐ์ดํฐ๋ฅผ ํ๋์ ๋ฐฐ์ด๋ก๊ด๋ฆฌํ ์ ์๋ค