반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

generic 본문

JAVA

generic

닉의네임 2020. 4. 2. 12:21
반응형

Human.java

package com.test.doo.entities;


import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Human<T, M> {
    protected T name;
    protected M age;
}

Boy.java

package com.test.doo.entities;

public class Boy<T, M> extends Human{
}

Girl.java

package com.test.doo.entities;

public class Girl<T, M> extends Human {
}

Main.Java

package com.test.doo.entities;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        Girl<String, Integer> girl = new Girl();
        girl.setName("sh");
        girl.setAge(45);

        Boy<String, Integer> boy = new Boy();
        boy.setName("dh");
        boy.setAge(38);

        Cat cat = new Cat();
        cat.setSpecies("kor");
        cat.setAge(2);

        List<Human> humans = new ArrayList<>();
        Utiles.<Human>getMember(humans, girl);
        Utiles.<Human>getMember(humans, boy);
//        Utiles.getMember(humans, cat);

        humans.stream().forEach(x -> {
                    System.out.println(x.getName());

                }
        );
    }


}

 

Utiles.java

package com.test.doo.entities;

import java.util.List;

public class Utiles {

    public static  <T extends Human> List<? extends Human> getMember(List<T> list , T t){
        list.add(t);
        return list;
    }
}

 

반응형

'JAVA' 카테고리의 다른 글

[java] 10진수 를 2진수, 8진수, 16진수로 변환하기  (0) 2023.05.15
[java] Spark java tutorial  (0) 2022.06.05
Error querying database  (0) 2022.04.25
Table 'shop.hibernate_sequence' doesn't exist  (0) 2022.04.16
Comments