정보보안공부
Java2_day05 본문
*** n01.generic_impl - Worker
-> public class Worker extends Person //Worker클래스에 Person클래스 상속
{ public Worker(String name) { super(name); } }
*** n01.generic_impl - Person
->public class Person { private String name;
public Person(String name) { this.name = name; }
public String getName() {return name;} public String toString() {return name;}
*** n01.generic_impl - Storage
-> public interface Storage<T> { //추상메소드 선언
public void add(T item, int index); public T get(int index);
public void remove(int index); public void printAll();
*** n01.generic_impl - StorageImpl
-> publi class StorageImpl<T> implements Storage<T> {
private T[] array;
-> public StorageImpl(int capacity) { this.array = (T[]) (new Object[capacity]); }
-> public void add(T item, int index) { array[index] = item }
-> public T get(int index) { return array[index]; }
-> public void remove(int index) { array[index] = null;
for(int i = index;i<array.length-1;i++)
array[i] = array[i+1];
-> public void printAll() { System.out.println("========printAll========");
for(T temp : array)
if(temp == null) System.out.println("****");
else System.out.println(temp);
*** n01.generic_impl - Generic_impl
-> Storage<Person> p = new StorageImpl<>(10);
Storage<Worker> w = new StorageImpl<>(10);
-> p.add(new Person("hong"), 0);
p.add(new Person("kim"), 1);
p.add(new Person("lee"), 2);
p.add(new Person("Choi"), 3);
p.add(new Person("Kwon"), 4);
StorageImpl에 정의된 add로 array[index] = item 적용
-> Person p1 = p.get(1); get(1)의값은 kim을 p1변수의 Person클래스형으로 대입
p1.getName() Person클래스에 정의된 getName()을 사용해 return값 name은kim
-> p.remove(2); 2번인덱스값 lee삭제해서 앞으로 밀착시킨다. remove메소드에 의해
-> p.printAll(); printAll()메소드를 실행해 전부 출력한다.
-> w.add(new Worker("kang"), 0);
w.add(new Worker("jung"), 1);
w.add(new Worker("ho"), 2);
w.add(new Worker("hi"), 3);
w.add(new Worker("hello"), 4);
StorageImpl에 정의된 add로 array[index] = item 적용
-> Worker w1 = w.get(0); get(0)의 값은 kang을 wq변수의 Worker클래스형으로 대입
w1.getName() Person클래스에 정의된 getName()을 사용해 return값 name은kang
*** n02.boundedtype - Util
-> public class Util { public static <T extends Number> int compare(T t1, T t2)
{double v1 = t1.doubleValue(); //t1을 double 타입으로
double v2 = t2.doubleValue(); //t2를 double 타입으로
*** n02.boundedtype -
-> Number의 하위클래스 타입으로만 generic type을 제한
Number의 하위클래스 : byte, short, integer, long, float, double
*** n02.boundedtype - BoundedEx
-> public class BoundedEx { public static void main(String[] args) {
int result1 = Util.compare(10,20); //-1, left < right
-> int result2 = Util.compare(4.5, 3); //1, left > right
-> int result3 = Util.compare(100,100); //0, left = right
*** n03.generic_wildcard - Student
-> public class Student extends Person {
public Student(String name) { super(name); }
*** n03.generic_wildcard - Person
-> public class Person { private String name;
public Person(String name) { this.name = name; }
public String getName() { return name; }
public String toString() { return name; }
*** n03.generic_wildcard - HighStudent
-> public class HighStudent exxtends Student {
public HighStudent(String name) { super(name); }
*** n03.generic_wildcard - Worker
-> public class Worker extends Person{
public Worker (String name) { super(name); }
*** n03.generic_wildcard - Course
-> public class Course<T> { private String name; private T[] students;
public Course(String name, int capacity) { this.name = name;
this.students = (T[]) (new Object[capacity]); }
-> public String getName() { return name; }
-> public T[] getStudents() { return students; }
-> public void add(T t) { for (int i =0 < i<students.length;i++) {
if(students[i] == null) { students[i] = t; break; } } }
*** n03.generic_wildcard - WildCardExample
-> public class WildCardExample {
public static void courseA(Course<?> course) {
System.out.println(course.getName() + " 수강생 : "
+ Arrays.toString(course.getStudents()));
-> public static void courseS(Course<? extends Student> course) {
System.out.println(course.getName() + " 수강생 : "
+ Arrays.toString(course.getStudents()));
-> public static void courseW(Course<? extends Worker> course) {
System.out.println(course.getName() + " 수강생 : "
+ Arrays.toString(course.getStudents()));
-> Course<Person> pcourse = new Course<Person>("일반인과정", 5);
pcourse.add(new Person("일반인"));
pcourse.add(new Worker("직장인"));
pcourse.add(new Student("학생"));
pcourse.add(new HighStudent("고등학생"));
-> Course<Worker> wcourse = new Course<Worker>("직장인과정", 5);
w.course.add(new Worker("직장인");
w.course.add(new Person("직장인"); //에러,generic type보다 상위클래스생성저장x
-> Course<Student> scourse = new Course<Student>("학생과정", 5);
scourse.add(new Student("학생"));
scourse.add(new HighStudent("고등학생"));
-> Couarse<HighStudent> hcourse = new Course<HighStudent>("고등학생과정",5);
hcourse.add(new HighStudent("고등학생"));
-> courseA(pcourse);
courseA(wcourse);
courseA(scourse);
courseA(hcourse);
-> courseS(pcourse); //에러 : courseS는 Student클래스 pcourse는 Person클래스
courseS(wcourse); //에러 : courseS는 Student클래스 wcourse는 Worker클래스
courseS(scourse);//Person은 Student상위클래스 Worker는 Student클래스와 연관
courseS(hcourse);
-> courseW(pcourse);
courseW(wcourse);
courseW(scourse); //에러 : courseW는 Worker클래스 scourse는 Student클래스
courseW(hcourse); //에러 : courseW는 Worker클래스 hcourse는 HighStudent
-> courseS는 extends Student로 Student와 그 하위클래스 HighStudent를 포함한다.
extends대신 super였다면,
courseS는 Student와 그 상위클래스 Person을 포함한다.
'Language > Java2' 카테고리의 다른 글
Java2_day07 (0) | 2017.03.18 |
---|---|
Java2_day06 (0) | 2017.03.17 |
Java2_day04 (0) | 2017.03.16 |
Java2_day03 (0) | 2017.03.14 |
Java2_day02 (0) | 2017.03.11 |