객체지향 프로그래밍 복습 (8) Generic Class
[JAVA] 객체지향 프로그래밍 복습 (8) Generic Class
자바
자바(Java)는 C언어에 객체 지향적 기능을 추가하여 만든 C++과는 달리, 처음부터 객체 지향 언어로 개발된 프로그래밍 언어입니다. 자바는 자바 가상 머신(JVM, Java Virtual Machine)을 사용하여, 운영체제와는 독립적으로 동작할 수 있습니다. 따라서 자바는 어느 운영체제에서나 같은 형태로 실행될 수 있습니다. 바로 이러한 점이 수많은 개발자로 하여금 자바를 사용하게 하는 원동력이 되고 있습니다. 현재 자바는 전 세계에서 가장 많이 사용하는 프로그래밍 언어 중 하나입니다.
Generic Class
제네릭은 클래스, 메소드에서 사용할 데이터 타입을 나중에 확정하는 기법입니다. 나중에라는 말은 클래스나 메소드를 선언할 때가 아닌 사용할 때, 즉 인스턴스를 생성할 때나 메소드를 호출할 때 정한다는 의미입니다. 제네릭의 사용 방법과 특징은 메소드의 매개변수와 굉장히 유사한데, 메소드의 매개변수가 ‘값’과 관련되어 있다면 제네릭은 데이터의 ‘타입’과 관련이 있습니다. 자바의 제네릭은 C++의 template과 유사한 기능입니다.
Box 클래스
제네릭을 복습하기 위해 물건을 넣는 박스 클래스를 구현합니다. 데이터 타입을 모르는 아이템 추가, 아이템 리턴, 아이템 리스트 리턴, 아이템 출력. 아이템 사이즈 출력이 있습니다. 또, 넣을 물건을 정하기 위해 Toy 와 Fruit 클래스를 구현합니다.
Box.java
import java.util.ArrayList;
public class Box<T> {
private ArrayList<T> itemList = new ArrayList<T>();
public void add(T item) {
itemList.add(item);
}
public T get(int i) {
int size = itemList.size();
if(i <= size && i >= 0) {
return itemList.get(i-1);
} else {
System.out.println("The Value of " + i + " is invalid.");
return null;
}
}
public ArrayList<T> getItemList() {
return itemList;
}
public int size() {
return itemList.size();
}
public String toString() {
return itemList.toString();
}
}
Toy.java
//객체지향프로그래밍및실습 과제
//20201881 장환곤
//OOP.LAB.09
public class Toy {
private String name;
public Toy(String name) {
this.name = name;
System.out.println("New item is added!: Toy-" + name);
}
public String toString() {
return "Toy-" + this.name;
}
}
Fruit. java
//객체지향프로그래밍및실습 과제
//20201881 장환곤
//OOP.LAB.09
public class Fruit {
private String name;
public Fruit(String name) {
this.name = name;
System.out.println("New item is added!: Fruit-" + name);
}
public String toString() {
return "Fruit-" + this.name;
}
}
test.java
public class test {
public static void main(String[] args) {
System.out.println("== Creating 3 Toys ==");
Box<Toy> toyBox = new Box<>();
System.out.println("Toy Box is created.");
Box<Fruit> fruitBox = new Box<>();
System.out.println("Fruit Box is created.");
System.out.println("\n== Creating 3 Toys ==");
Toy toy1 = new Toy("Ball");
Toy toy2 = new Toy("Doll");
Toy toy3 = new Toy("Puzzle");
toyBox.add(toy1);
toyBox.add(toy2);
toyBox.add(toy3);
System.out.println("\n== Printing the whole content of Toy Box ==");
System.out.println(toyBox.toString());
System.out.println("\n== Printing the number of toys in Toy Box ==");
System.out.println("The number of toys in Toy Box: " + toyBox.size());
System.out.println("\n== Printing 2nd toy of Toy Box ==");
System.out.println("2nd Toy of Toy Box: " + toyBox.get(2));
System.out.println("\n== Creating 4 Fruits ==");
Fruit fruit1 = new Fruit("Apple");
Fruit fruit2 = new Fruit("Banana");
Fruit fruit3 = new Fruit("Cherry");
Fruit fruit4 = new Fruit("DewBerry");
fruitBox.add(fruit1);
fruitBox.add(fruit2);
fruitBox.add(fruit3);
fruitBox.add(fruit4);
System.out.println("\n== Printing the whole content of Fruit Box ==");
System.out.println(fruitBox.toString());
System.out.println("\n== Printing the number of fruits in Fruit Box ==");
System.out.println("The number of fruits in Fruit Box: " + fruitBox.size());
System.out.println("\n== Printing 3rd fruit of Fruit Box ==");
System.out.println("3rd Fruit of Fruit Box: " + fruitBox.get(3));
}
}