์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- docker compose
- ์คํํ๋ ๋ฏธ์ค
- ์๋ฒ ํฐ์ง๋ ๋์ปค ์ฌ์คํ
- ์๋ฐ
- ์ ํจ์ค ์ค์ผ์ค๋ฌ
- redis ํ ์คํธ์ฝ๋
- ์ ํจ์ค ๋น๋ ์ค๋ฅ
- s3 ์ด๋ฏธ์ง ์ ์ฅ
- ํ๋ก๊ทธ๋๋จธ์ค ํฉ์นํ์์๊ธ
- ํ์ดํผ๋ฐ์ด์
- Kafka
- prod docker-compose
- private subnet ec2 ๋ก์ปฌ ์ ์
- AWS Certified Solutions Architect - Associate
- docker
- aws ์ฟ ํฐ
- aws saa ํฉ๊ฒฉ
- s3 ์ด๋ฏธ์ง ๋ค์ด๋ก๋
- s3 log ์ ์ฅ
- JPA
- docker-compose kafka
- ํ๋ก๊ทธ๋๋จธ์ค
- redis ์กฐํ
- ๋ค์ค ์ปจํ ์ด๋
- jvm ๋ฐ๋ฐ๋ฅ๊น์ง ํํค์น๊ธฐ
- docker ps -a
- nGrinder
- Codedeploy ์ค๋ฅ
- ํ๋ก๊ทธ๋๋จธ์ค ์ปฌ๋ฌ๋ง๋ถ
- Entity
- Today
- Total
๐๐ข๐๐ โ๐๐๐ ๐๐๐ก๐๐ ๐๐๐๐โง
[Java] Generic Method ์ ๋ค๋ฆญ ๋ฉ์๋ ๋ณธ๋ฌธ
[Java] Generic Method ์ ๋ค๋ฆญ ๋ฉ์๋
๐คRyusun๐ค 2022. 10. 12. 15:05โญ์ ๋ค๋ฆญ ๋ฉ์๋(generic method)
- ์ ๋ค๋ฆญ ๋ฉ์๋๋ ๋ฉ์๋์ ์ ์ธ๋ถ์ ํ์ ๋ณ์๋ฅผ ์ฌ์ฉํ ๋ฉ์๋๋ฅผ ์๋ฏธ
- ๋ฐ์ดํฐ ํ์ ์ ์์๊ด๊ณ์ ์๊ด์์ด ๋ค์ํ๊ฒ ์ฃผ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค.
- ํํ
- public class ํด๋์ค๋ช <T> { ... }
- public interface ์ธํฐํ์ด์ค๋ช <T> { ... }
- ํน์ง
- ํ๋ผ๋ฏธํฐ ํ์ ์ด๋ ๋ฆฌํด ํ์ ์ ๋ํ ์ ์๋ฅผ ์ธ๋ถ๋ก ๋ฏธ๋ฃธ
- ํ์ ์ ๋ํ ์์ ์ฑ๊ณผ ์ ์ฐ์ฑ ํ๋ณด
- ๋ง์ฝ ์ต์์ ํด๋์ค Object๋ก ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ๋๋ค๋ฉด ๊ฐ์ฒด๋ณ์์ ์ค์ ์ธ์คํด์ค ํ๋๋ ๋ฉ์๋์ ์ ๊ทผํ ๋ ํ๋ณํ ํ์ํ๋ค.
- ๋ํ Object ๊ฐ์ฒด๋ณ์์ ์ค์ ์ธ์คํด์ค ํ๋๋ ๋ฉ์๋์ ์ ๊ทผํ๊ธฐ ์ํด ํ๋ณํ์ ์๋ชปํ ๊ฒฝ์ฐ ์ปดํ์ผ๋ฌ๋ ์ด๋ฅผ ์ก์ง ๋ชปํ๋ค.
- ์ ๋๋ฆญ ์ฅ์
- ๋ฐ์ดํฐ ํ ๋ณํ ์ ๊ฑฐ
- ์ปดํ์ผ์ ๋ฐ์ดํฐ ํ์ ์ฒดํฌ ๊ฐ๋ฅ
์์
import java.util.Objects;
public class GiftBox<T> {
private T gift;
public GiftBox() {
}
public GiftBox(T gift) {
this.gift = gift;
}
public T getGift() {
return gift;
}
public void setGift(T gift) {
this.gift = gift;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GiftBox<?> that = (GiftBox<?>) o;
return gift.equals(that.gift);
}
@Override
public int hashCode() {
return Objects.hash(gift);
}
@Override
public String toString() {
return "RandomGiftBox{" +
"gift=" + gift +
'}';
}
}
import me.day13.generic.basics.gift.Note;
import me.day13.generic.basics.gift.Pen;
import me.day13.generic.basics.gift.SmartPhone;
import me.day13.generic.basics.gift.Tablet;
public class GenericExample {
public static void main(String[] args) {
GiftBox<Note> noteBox = new GiftBox<>();
noteBox.setGift(new Note("1001", "๋ชจ๋๊ธ๋ก๋ฆฌ", true));
System.out.println("noteBox = " + noteBox);
// ํ ๋ณํ ํ ์ฌ์ฉํด์ผ ํจ
noteBox.getGift().setHasLine(false); // ํ๋ณํ ๋ถํ์.
// ((Pen)noteBox.getGift()).setInkColor("๋นจ๊ฐ"); // ์ปดํ์ผ๋ฌ ์ค๋ฅ.
System.out.println("noteBox = " + noteBox);
System.out.println();
GiftBox<Pen> penBox = new GiftBox<>();
penBox.setGift(new Pen("2001", "๋ชจ๋๊ธ๋ก๋ฆฌ", "๊ฒ์ "));
System.out.println("penBox = " + penBox);
penBox.getGift().setInkColor("ํ๋"); // ํ๋ณํ ๋ถํ์.
System.out.println("penBox = " + penBox);
System.out.println();
GiftBox<Tablet> tableBox = new GiftBox<>();
tableBox.setGift(new Tablet("3001", "APPLE", "IPAD-AIR3", true));
System.out.println("tableBox = " + tableBox);
tableBox.getGift().setHasPencil(false);
System.out.println("tableBox = " + tableBox);
System.out.println();
GiftBox<SmartPhone> smartPhoneBox = new GiftBox<>();
smartPhoneBox.setGift(new SmartPhone("4001", "SAMSUNG", "Z-FOLD4", 3, 20));
System.out.println("smartPhoneBox = " + smartPhoneBox);
smartPhoneBox.getGift().setNumOfCameras(5);
smartPhoneBox.getGift().setNumOfSensors(30);
System.out.println("smartPhoneBox = " + smartPhoneBox);
System.out.println();
}
}
- ๋ฉํฐ ํ์
ํ๋ผ๋ฏธํฐ
- ์ ๋๋ฆญ ํ์ ์ ๋ ๊ฐ ์ด์ ๋ฉํฐ ํ์ ํ๋ผ๋ฏธํฐ ์ฌ์ฉ ๊ฐ๋ฅ
- ํํ
์ ๋๋ฆญ ๋ฉ์๋์ ๋ํ ์์ธํ ์ค๋ช : https://devlog-wjdrbs96.tistory.com/201
์์
public interface Utils {
static <T> Box<T> boxing(T item) {
// <T>๋ ์ ๋๋ฆญ ๋ฉ์๋. ์ ๋๋ฆญ ๋ฉ์๋๋ ํ์ ์ด ๋ณํ๋ ๋ฉ์๋. ๋ง์ฝ T์ Note๊ฐ ๋ค์ด์ค๋ฉด Note๊ฐ boxing๋์ด์ Noteํ์ ์ Box๊ฐ ๋ฐํ๋๋๊ฒ์ด๋ค.
<T> = ์ด ๋ฉ์๋๊ฐ ์ ๋๋ฆญ ๋ฉ์๋ ๋ผ๊ณ ์๋ฏธ
<T> = ๋ฐํ๋๋ Box ํ์ ์ ์๋ฏธ
T = ๋งค๊ฐ๋ณ์์ ํ์ ์ ์๋ฏธ
๋ฐ๋ผ์ ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด boxing์ด ๋๊ณ ์ด ๋ฐํbox๋ฅผ ํตํด์
Box<T> box = new Box<>();
box.setItem(item);
return box;
}
ํด๋์ค์ ์ ๋๋ฆญ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํ์
์ ์ ํด์ค์ ์๋๋ฐ ์ธํฐํ์ด์ค๋ ์๋ก ๊ฐ์ฒด๋ฅผ ์์ฑ์ ๋ชปํ๊ธฐ๋๋ฌธ์ ์ฌ๊ธฐ์ ํ์
์ ์ ํด์ฃผ๋๊ฒ์ด๋ค.
static <T> void decoration(Box<T> box, Decorations decoration) {
System.out.println(box + " is decorated with "+ decoration);
}
static <T> T unboxing(Box<T> box) {
T item = box.getItem();
return item;
}
}
- ์ ํ๋ ํ์
ํ๋ผ๋ฏธํฐ
- bounded type parameter
- <ํ์
ํ๋ผ๋ฏธํฐ extends ์์ํ์
>
- ์์ ๊ด๊ณ๋์์ ์์ ํ์ ์ดํ์ ์๋ ํ์ ์ด ํ๋ผ๋ฏธํฐ๋ก ์ฌ ์ ์์
- ์ํ ํ์ ์ ํ
- <ํ์
ํ๋ผ๋ฏธํฐ super ํ์ํ์
>
- ์์ ๊ด๊ณ๋์์ ํ์ ํ์ ์ด์์ ์๋ ํ์ ์ด ํ๋ผ๋ฏธํฐ๋ก ์ฌ ์ ์์
- ํํ ํ์ ์ ํ
import java.util.Arrays;
import java.util.Objects;
public class Gifts<E extends Item> {
private static final int DEFULAT_SIZE = 10;
private int size;
private E[] gifts;
private int count;
public Gifts() {
gifts = (E[]) new Item[DEFULAT_SIZE];
this.size = DEFULAT_SIZE;
}
public Gifts(int size) {
gifts = (E[]) new Item[size];
this.size = size;
}
public E get(int i) {
return gifts[i];
}
public void add(E element) { // ํน์ ์ธ๋ฑ์ค์ ์์ ์ถ๊ฐํ๊ธฐ
if (count < size) {
gifts[count] = element;
count++;
} else {
E[] origin = Arrays.copyOf(gifts, count);
// Arrays.copy(๋ฐฐ์ด, ๊ฐ์) = ๋ฐฐ์ด์ ๊ฐ์๋งํผ ๋ณต์ฌํ๋ ํจ์
size *= 2;
gifts = Arrays.copyOf(origin, size);
add(element);
}
}
public void remove(E element) { // ํน์ ์ธ๋ฑ์ค๊ฐ ์ญ์ ํ๊ธฐ
int elementIndex = -1;
for (int i = 0; i < count; i++) {
if (gifts[i] != null) {
if (gifts[i].equals(element)) {
elementIndex = i;
}
}
}
if (elementIndex == -1) {// ์ด๊ธฐ๊ฐ์ด ์๋ณํ์ผ๋ฉด ์ฐพ๋ ์ธ๋ฑ์ค๊ฐ ์๋ค๋ ์๋ฏธ
System.out.println(element + " can't be found.");
} else {
gifts[elementIndex] = null;
for (int i = elementIndex+1; i < count; i++) {
gifts[i-1] = gifts[i];
}
count--;
System.out.println(element + " removed successfully.");
}
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public E[] getGifts() {
return gifts;
}
public void setGifts(E[] gifts) {
this.gifts = gifts;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Gifts<?> that = (Gifts<?>) o;
return size == that.size && count == that.count && Arrays.equals(gifts, that.gifts);
}
@Override
public int hashCode() {
int result = Objects.hash(size, count);
result = 31 * result + Arrays.hashCode(gifts);
return result;
}
@Override
public String toString() {
return "RandomGiftBox{" +
"size=" + size +
", gifts=" + Arrays.toString(gifts) +
", count=" + count +
'}';
}
}
import me.day13.generic.extend.gift.*;
public class GenericExtendsExample {
public static void main(String[] args) {
Gifts<Note> randomNotes = new Gifts<>();
randomNotes.add(new Note("1001", "๋ชจ๋๊ธ๋ก๋ฆฌ", true));
randomNotes.add(new Note("2001", "๋ฐ๋ฅธ์", false));
randomNotes.add(new Note("3001", "๋ฐ๋ฅธ์", true));
randomNotes.add(new Note("4001", "๋ชจ๋๊ธ๋ก๋ฆฌ", false));
for (int i = 0; i < randomNotes.getCount(); i++) {
randomNotes.get(i).setHasLine(true);
System.out.println("randomNotes.getGifts().get(i) = " + randomNotes.get(i));
}
System.out.println();
randomNotes.remove(new Note("1001", "๋ชจ๋๊ธ๋ก๋ฆฌ", true));
randomNotes.remove(new Note("2001", "๋ฐ๋ฅธ์", true));
randomNotes.remove(new Note("3001", "๋ฐ๋ฅธ์", true));
randomNotes.remove(new Note("4001", "๋ชจ๋๊ธ๋ก๋ฆฌ", true));
randomNotes.remove(new Note("4001", "๋ชจ๋๊ธ๋ก๋ฆฌ", true));
System.out.println();
System.out.println();
//////////////////////////////////////////////////////////////////////////////////////////
Gifts<Pen> randomPens = new Gifts<>();
randomPens.add(new Pen("1001", "๋ชจ๋๊ธ๋ก๋ฆฌ", "๋นจ๊ฐ"));
randomPens.add(new Pen("2001", "๋ฐ๋ฅธ์", "๊ฒ์ "));
randomPens.add(new Pen("3001", "๋ฐ๋ฅธ์", "๊ฒ์ "));
randomPens.add(new Pen("4001", "๋ชจ๋๊ธ๋ก๋ฆฌ", "ํ๋"));
for (int i = 0; i < randomPens.getCount(); i++) {
randomPens.get(i).setInkColor("๊ฒ์ ");
System.out.println("randomPens.getGifts().get(i) = " + randomPens.get(i));
}
System.out.println();
randomPens.remove(new Pen("1001", "๋ชจ๋๊ธ๋ก๋ฆฌ", "๊ฒ์ "));
randomPens.remove(new Pen("2001", "๋ฐ๋ฅธ์", "๊ฒ์ "));
randomPens.remove(new Pen("3001", "๋ฐ๋ฅธ์", "๊ฒ์ "));
randomPens.remove(new Pen("4001", "๋ชจ๋๊ธ๋ก๋ฆฌ", "๊ฒ์ "));
randomPens.remove(new Pen("4001", "๋ชจ๋๊ธ๋ก๋ฆฌ", "๊ฒ์ "));
System.out.println();
System.out.println();
//////////////////////////////////////////////////////////////////////////////////////////
Gifts<Tablet> randomTables = new Gifts<>();
randomTables.add(new Tablet("1001", "SAMSUNG", "GALAXY-TAB S8", true));
randomTables.add(new Tablet("2001", "APPLE", "IPAD-AIR3", false));
randomTables.add(new Tablet("3001", "APPLE", "IPAD-PRO3", false));
randomTables.add(new Tablet("4001", "SAMSUNG", "GALAXY-TAB S8+", true));
for (int i = 0; i < randomTables.getCount(); i++) {
randomTables.get(i).setHasPencil(true);
System.out.println("randomTables.getGifts().get(i) = " + randomTables.get(i));
}
System.out.println();
randomTables.remove(new Tablet("1001", "SAMSUNG", "GALAXY-TAB S8", true));
randomTables.remove(new Tablet("2001", "APPLE", "IPAD-AIR3", true));
randomTables.remove(new Tablet("3001", "APPLE", "IPAD-PRO3", true));
randomTables.remove(new Tablet("4001", "SAMSUNG", "GALAXY-TAB S8+", true));
randomTables.remove(new Tablet("4001", "SAMSUNG", "GALAXY-TAB S8+", true));
System.out.println();
System.out.println();
//////////////////////////////////////////////////////////////////////////////////////////
Gifts<SmartPhone> randomSmartPhone = new Gifts<>();
randomSmartPhone.add(new SmartPhone("1001", "SAMSUNG", 3, 20));
randomSmartPhone.add(new SmartPhone("2001", "LG", 2, 10));
randomSmartPhone.add(new SmartPhone("3001", "LG", 3, 10));
randomSmartPhone.add(new SmartPhone("4001", "SAMSUNG", 2, 15));
for (int i = 0; i < randomSmartPhone.getCount(); i++) {
randomSmartPhone.get(i).setNumOfCameras(3);
randomSmartPhone.get(i).setNumOfSensors(20);
System.out.println("randomSmartPhone.getGifts().get(i) = " + randomSmartPhone.get(i));
}
System.out.println();
randomSmartPhone.remove(new SmartPhone("1001", "SAMSUNG", 3, 20));
randomSmartPhone.remove(new SmartPhone("2001", "LG", 3, 20));
randomSmartPhone.remove(new SmartPhone("3001", "LG", 3, 20));
randomSmartPhone.remove(new SmartPhone("4001", "SAMSUNG", 3, 20));
randomSmartPhone.remove(new SmartPhone("4001", "SAMSUNG", 3, 20));
System.out.println();
System.out.println();
}
}
E ์๋ฆฌ์ ์ฌ์ฉํ ํ์ ์ ์ฐ๋ฉด ๋๋ค.
โญ์์ผ๋์นด๋
- ์ฝ๋์์ ? ๋ก ์ฌ์ฉ
- ์ ๋๋ฆญ ํ์ ์ ๋ฉ์๋์ ๋งค๊ฐ๊ฐ์ด๋ ๋ฆฌํด ํ์ ์ผ๋ก ์ฌ์ฉํ ๋ ๊ตฌ์ฒด์ ์ธ ํ์ ๋์ ์ ์์ผ๋ ์นด๋๋ก ๋์ฒด ๊ฐ๋ฅํ๋ค.
- ํด๋์ค/์ธํฐํ์ด์ค๋ช
<?>
- Unbounded Wildcards (์ ํ์์)
- ํ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋์นํ๋ ๊ตฌ์ฒด์ ์ธ ํ์ ์ผ๋ก ๋ชจ๋ ํด๋์ค/์ธํฐํ์ด์ค ์ฌ ์ ์์
- ํด๋์ค/์ธํฐํ์ด์ค๋ช
<? extends ์์ํ์
>
- Upper Bounded Wildcards (์์ ํด๋์ค ์ ํ)
- ํ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋์นํ๋ ๊ตฌ์ฒด์ ์ธ ํ์ ์ผ๋ก ์์ ํ์ ์ด๋ ์์ ํ์ ์ดํ์ ํ์ ๋ง ์ฌ ์ ์์
- ํด๋์ค/์ธํฐํ์ด์ค๋ช
<? super ํ์ํ์
>
- Lower Bounded Wildcards (ํ์ ํด๋์ค ์ ํ)
- ํ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋์นํ๋ ๊ตฌ์ฒด์ ์ธ ํ์ ์ผ๋ก ํ์ ํ์ ์ด๋ ํ์ ํ์ ์ด์์ ํ์ ๋ง ์ฌ ์ ์์
- <?> // ํ์ ๋ณ์์ ๋ชจ๋ ํ์ ์ ์ฌ์ฉํ ์ ์์.
- <? extends T> // T ํ์ ๊ณผ T ํ์ ์ ์์๋ฐ๋ ์์ ํด๋์ค ํ์ ๋ง์ ์ฌ์ฉํ ์ ์์.
- <? super T> // T ํ์ ๊ณผ T ํ์ ์ด ์์๋ฐ์ ์กฐ์ ํด๋์ค ํ์ ๋ง์ ์ฌ์ฉํ ์ ์์.
์์
import java.util.Arrays;
import java.util.Objects;
public class Course<T> {
public enum RegisterStudentType { ANIMAL, PERSON, STUDENT, HIGH_STUDENT, UNIV_STUDENT, WORKER }
private String courName;
private RegisterStudentType registerStudentType;
private T[] registerStudents;
private int count = 0;
private int size = 0;
public Course() {
}
public Course(String courName, RegisterStudentType registerStudentType, int size) {
this.courName = courName;
this.registerStudentType = registerStudentType;
this.registerStudents = (T[]) new Object[size];
this.size = size;
}
public String getCourName() {
return courName;
}
public void setCourName(String courName) {
this.courName = courName;
}
public RegisterStudentType getRegisterStudentType() {
return registerStudentType;
}
public void setRegisterStudentType(RegisterStudentType registerStudentType) {
this.registerStudentType = registerStudentType;
}
public T[] getRegisterStudents() {
return registerStudents;
}
public void setRegisterStudents(T[] registerStudents) {
this.registerStudents = registerStudents;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void add(T t) {
if (count < size) {
registerStudents[count] = (T) t;
count++;
System.out.println(t + " is added successfully.");
} else {
T[] copied = Arrays.copyOf(registerStudents, size);
size *= 2;
registerStudents = Arrays.copyOf(copied, size);
add(t);
}
}
public T remove(T t) {
if (t == null) {
System.out.println(t +" can't be removed successfully.");
return null;
}
int index = -1;
for (int i = 0; i < count; i++) {
if (t instanceof Person) {
if (registerStudents[i] != null) {
if (registerStudents[i].equals(t)) {
index = i;
}
}
}
}
T removeStudent = registerStudents[index];
registerStudents[index] = null;
for (int i = index+1; i < count; i++) {
registerStudents[i-1] = registerStudents[i];
}
System.out.println(removeStudent + " is removed successfully.");
return removeStudent;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Course<?> course = (Course<?>) o;
return courName.equals(course.courName) && registerStudentType == course.registerStudentType && Arrays.equals(registerStudents, course.registerStudents);
}
@Override
public int hashCode() {
int result = Objects.hash(courName, registerStudentType);
result = 31 * result + Arrays.hashCode(registerStudents);
return result;
}
@Override
public String toString() {
return "Course{" +
"courName='" + courName + '\'' +
", registerStudentType=" + registerStudentType +
", registerStudents=" + Arrays.toString(registerStudents) +
'}';
}
}
public interface CourseUtils {
static void registerPersonCourse(Course<? extends Person> course) {
System.out.println(course + " is registered successfully.\n\n");
}
static void registerAnimalCourse(Course<? extends Animal> course) {
System.out.println(course + " is registered successfully.\n\n");
}
}
public class GenericWildCardExample {
public static void main(String[] args) {
Course<Person> personCourse = new Course<>("์ ํ
ํฌ ์์ํ๊ธฐ", Course.RegisterStudentType.PERSON, 5);
Course<Student> studentCourse = new Course<>("์คํฌ๋์น ํ๋ก๊ทธ๋๋ฐ", Course.RegisterStudentType.STUDENT, 5);
Course<Worker> workerCourse = new Course<>("์ค๋ฌด ์๋ํ๋ฅผ ์ํ ํ๋ก๊ทธ๋๋ฐ", Course.RegisterStudentType.WORKER, 5);
Course<HighStudent> highStudentCourse = new Course<>("๋ํ ์
์๋ฅผ ์ํ SW ๊ฒฝ์ง๋ํ ๋๋น", Course.RegisterStudentType.HIGH_STUDENT, 5);
Course<UnivStudent> univStudentCourse = new Course<>("OOP ํ๋ก๊ทธ๋๋ฐ", Course.RegisterStudentType.UNIV_STUDENT, 5);
Course<Animal> animalCourse = new Course<>("๋ฐ๋ ค๋๋ฌผ ์ฌํ์ฑ ๊ธฐ๋ฅด๊ธฐ", Course.RegisterStudentType.ANIMAL, 5);
Person person = new Person();
personCourse.add(person);
// studentCourse.add(person); // ์ปดํ์ผ ์ค๋ฅ.
// workerCourse.add(person); // ์ปดํ์ผ ์ค๋ฅ.
// highStudentCourse.add(person); // ์ปดํ์ผ ์ค๋ฅ.
// univStudentCourse.add(person); // ์ปดํ์ผ ์ค๋ฅ.
// animalCourse.add(person); // ์ปดํ์ผ ์ค๋ฅ.
System.out.println();
Student student = new Student();
personCourse.add(student);
studentCourse.add(student);
// workerCourse.add(student); // ์ปดํ์ผ ์ค๋ฅ.
// highStudentCourse.add(student); // ์ปดํ์ผ ์ค๋ฅ.
// univStudentCourse.add(student); // ์ปดํ์ผ ์ค๋ฅ.
// animalCourse.add(student); // ์ปดํ์ผ ์ค๋ฅ.
System.out.println();
Worker worker = new Worker();
personCourse.add(worker);
// studentCourse.add(worker); // ์ปดํ์ผ ์ค๋ฅ.
workerCourse.add(worker);
// highStudentCourse.add(worker); // ์ปดํ์ผ ์ค๋ฅ.
// univStudentCourse.add(worker); // ์ปดํ์ผ ์ค๋ฅ.
// animalCourse.add(worker); // ์ปดํ์ผ ์ค๋ฅ.
System.out.println();
HighStudent highStudent = new HighStudent();
personCourse.add(highStudent);
studentCourse.add(highStudent);
// workerCourse.add(highStudent); // ์ปดํ์ผ ์ค๋ฅ.
highStudentCourse.add(highStudent);
// univStudentCourse.add(highStudent); // ์ปดํ์ผ ์ค๋ฅ.
// animalCourse.add(highStudent); // ์ปดํ์ผ ์ค๋ฅ.
System.out.println();
UnivStudent univStudent = new UnivStudent();
personCourse.add(univStudent);
studentCourse.add(univStudent);
// workerCourse.add(univStudent); // ์ปดํ์ผ ์ค๋ฅ.
// highStudentCourse.add(univStudent); // ์ปดํ์ผ ์ค๋ฅ.
univStudentCourse.add(univStudent);
// animalCourse.add(univStudent); // ์ปดํ์ผ ์ค๋ฅ.
System.out.println();
Animal animal = new Animal();
// personCourse.add(animal); // ์ปดํ์ผ ์ค๋ฅ.
// studentCourse.add(animal); // ์ปดํ์ผ ์ค๋ฅ.
// workerCourse.add(animal); // ์ปดํ์ผ ์ค๋ฅ.
// highStudentCourse.add(animal); // ์ปดํ์ผ ์ค๋ฅ.
// univStudentCourse.add(animal); // ์ปดํ์ผ ์ค๋ฅ.
animalCourse.add(animal);
System.out.println("\n\n========================================================= " +
"register person course results =========================================================");
CourseUtils.registerPersonCourse(personCourse);
CourseUtils.registerPersonCourse(studentCourse);
CourseUtils.registerPersonCourse(workerCourse);
CourseUtils.registerPersonCourse(highStudentCourse);
CourseUtils.registerPersonCourse(univStudentCourse);
// CourseUtils.registerPersonCourse(animalCourse); // ์ปดํ์ผ ์ค๋ฅ.
System.out.println("\n\n========================================================= " +
"register animal course results =========================================================");
// CourseUtils.registerAnimalCourse(personCourse); // ์ปดํ์ผ ์ค๋ฅ.
// CourseUtils.registerAnimalCourse(studentCourse); // ์ปดํ์ผ ์ค๋ฅ.
// CourseUtils.registerAnimalCourse(workerCourse); // ์ปดํ์ผ ์ค๋ฅ.
// CourseUtils.registerAnimalCourse(highStudentCourse); // ์ปดํ์ผ ์ค๋ฅ.
// CourseUtils.registerAnimalCourse(univStudentCourse); // ์ปดํ์ผ ์ค๋ฅ.
CourseUtils.registerAnimalCourse(animalCourse);
}
}
- ์ ๋๋ฆญ ํ์
์์
- ์์ ํ ํ์ ํ๋ผ๋ฏธํฐ ์ถ๊ฐ ๊ฐ๋ฅ
์์
import java.util.Objects;
public class Product<T, M> {
private T kind;
private M model;
public Product() {
}
public Product(T kind, M model) {
this.kind = kind;
this.model = 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;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product<?, ?> product = (Product<?, ?>) o;
return kind.equals(product.kind) && model.equals(product.model);
}
@Override
public int hashCode() {
return Objects.hash(kind, model);
}
@Override
public String toString() {
return "Product{" +
"kind=" + kind +
", model=" + model +
'}';
}
}
import java.util.Objects;
public class ChildProduct<T, M, C> extends Product<T, M> {
private C company;
public ChildProduct() {
}
public ChildProduct(C company) {
this.company = company;
}
public ChildProduct(T kind, M model, C company) {
super(kind, model);
this.company = company;
}
public C getCompany() {
return company;
}
public void setCompany(C company) {
this.company = company;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
ChildProduct<?, ?, ?> that = (ChildProduct<?, ?, ?>) o;
return company.equals(that.company);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), company);
}
@Override
public String toString() {
return "ChildProduct{" + super.toString() + ", " +
"company=" + company +
'}';
}
}
- ์ ๋๋ฆญ ํ์
๊ตฌํ
- ๊ตฌํ ํ ํ์ ์ ํ ๊ฐ๋ฅ
public interface Storage<T> {
void add(T item);
T get(int index);
void remove(T item);
}
import java.util.Arrays;
import java.util.Objects;
public class StorageImpl<T extends Clothes> implements Storage<T> {
private static final int DEFULAT_SIZE = 10;
private int size;
private T[] list;
private int count;
public StorageImpl() {
list = (T[]) new Clothes[DEFULAT_SIZE];
this.size = DEFULAT_SIZE;
}
public StorageImpl(int size) {
list = (T[]) new Clothes[size];
this.size = size;
}
public T get(int i) {
return list[i];
}
public void add(T element) {
if (count < size) {
list[count] = element;
count++;
} else {
T[] origin = Arrays.copyOf(list, count);
size *= 2;
list = Arrays.copyOf(origin, size);
add(element);
}
}
public void remove(T element) {
int elementIndex = -1;
for (int i = 0; i < count; i++) {
if (list[i] != null) {
if (list[i].equals(element)) {
elementIndex = i;
}
}
}
if (elementIndex == -1) {
System.out.println(element + " can't be found.");
} else {
list[elementIndex] = null;
for (int i = elementIndex+1; i < count; i++) {
list[i-1] = list[i];
}
count--;
System.out.println(element + " removed successfully.");
}
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public T[] getList() {
return list;
}
public void setList(T[] list) {
this.list = list;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StorageImpl<?> storage = (StorageImpl<?>) o;
return size == storage.size && count == storage.count && Arrays.equals(list, storage.list);
}
@Override
public int hashCode() {
int result = Objects.hash(size, count);
result = 31 * result + Arrays.hashCode(list);
return result;
}
@Override
public String toString() {
return "StorageImpl{" +
"size=" + size +
", list=" + Arrays.toString(list) +
", count=" + count +
'}';
}
}
import java.util.Objects;
class Clothes {
private String type;
private String season;
private String color;
public Clothes() {
}
public Clothes(String type, String season, String color) {
this.type = type;
this.season = season;
this.color = color;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Clothes clothes = (Clothes) o;
return type.equals(clothes.type) && season.equals(clothes.season) && color.equals(clothes.color);
}
@Override
public int hashCode() {
return Objects.hash(type, season, color);
}
@Override
public String toString() {
return "Clothes{" +
"type='" + type + '\'' +
", season='" + season + '\'' +
", color='" + color + '\'' +
'}';
}
}
public class ImplementExample {
public static void main(String[] args) {
StorageImpl<Clothes> storage = new StorageImpl<>();
storage.add(new Clothes("์
์ธ ", "๊ฐ์", "๋ธ๋ฃจ"));
storage.add(new Clothes("๋ฐ๋ฐ์ง", "์ฌ๋ฆ", "๊ฒ์ "));
storage.add(new Clothes("๋ํธ", "๊ฒจ์ธ", "๋ฒ ์ด์ง"));
System.out.println();
for (int i = 0; i < storage.getCount(); i++) {
System.out.println("storage.get(i) = " + storage.get(i));
}
System.out.println();
storage.remove(new Clothes("์
์ธ ", "๊ฐ์", "๋ธ๋ฃจ"));
storage.remove(new Clothes("๋ฐ๋ฐ์ง", "์ฌ๋ฆ", "๊ฒ์ "));
storage.remove(new Clothes("๋ํธ", "๊ฒจ์ธ", "๋ฒ ์ด์ง"));
}
}
'๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป > ๐๐๐ฏ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] JSP, Directive (0) | 2022.10.27 |
---|---|
[Java] PriorityQueue (0) | 2022.10.19 |
[Java] ์บก์ํ (0) | 2022.10.07 |
[Java] ๊ฐ์ฒด, ์์ฑ์, ํด๋์ค (0) | 2022.10.07 |
[Java] ๋ฐฐ์ด (6) | 2022.09.30 |