当前位置: 移动技术网 > IT编程>开发语言>Java > java泛型demo

java泛型demo

2020年01月01日  | 移动技术网IT编程  | 我要评论

青岛租房信息,健康之路20131028,洋模当街换衣遭围观

1.泛型类

public class dog<t> {
    private t age;

    public dog(t age) {
        this.age = age;
    }

    public t getage() {
        return age;
    }

    public static void main(string[] args) {
        //java7之后,尖括号中是不需要填写参数的
        dog<string> dog=new dog<>("28");
        system.out.println(dog.getage());
    }
}

普通的类

public class dog {
    private object age;

    public dog(object age) {
        this.age = age;
    }

    public object getage() {
        return age;
    }

    public static void main(string[] args) {
        dog dog=new dog("28");
        system.out.println(dog.getage());
    }
}

这样的代码是完全可以执行了,那为什么还需要泛型类?

1.安全性

public class dog {
    private object age;

    public dog(object age) {
        this.age = age;
    }

    public object getage() {
        return age;
    }

    public static void main(string[] args) {
        dog dog=new dog("28");
        integer age=(integer) dog.getage();
        system.out.println(age);
    }
}

上面的代码编译是完全可以通过的,但是执行的时候就会出现classcastexception异常
2.可读性好,省去了反复的强制类型转换。

 

对于泛型类,java编译器会将泛型代码转换成普通的非泛型代码,

所以对于虚拟机来说,是没有泛型类的概念的。
为什么这么设计呢?应为泛型是jdk6以后才有的,为了向下兼容。

泛型方法:

public class testmethod {
    public static <t> boolean ishas(t[] arr, t elemt){
        for(t t:arr){
            if(t.equals(elemt)){
                return true;
            }
        }
        return false;
    }
    public <s> boolean isstring(s s){
       if(s instanceof string){
           return true;
       }
       return false;
    }

    public static void main(string[] args) {
        integer[] arr={1,5,6,8};
        system.out.println(ishas(arr,8));
        testmethod testmethod=new testmethod();
        system.out.println(testmethod.isstring(5));
    }
}

一个方法是不是泛型和他的类是不是泛型没有任何关系。
泛型方法需要在方法的返回值前先声明,在从后面的代码中使用。

泛型接口:
参照comparable接口。

public class testcomparable implements mycomparable<testcomparable>{

    private integer n;

    public testcomparable(int n) {
        this.n = n;
    }

    @override
    public boolean isequals(testcomparable testcomparable) {
        return this.n.intvalue()==testcomparable.getn().intvalue()?true:false;
    }

    public integer getn() {
        return n;
    }

    public static void main(string[] args) {
        testcomparable testcomparable1=new testcomparable(2);
        testcomparable testcomparable2=new testcomparable(2);
        system.out.println(testcomparable1.isequals(testcomparable2));
    }
}
interface mycomparable<t> {
    boolean isequals(t t);
}

类型参数继承某个类

/**
 * 测试继承class
 */
public class testinheritclass<t extends  father>{
    private t t;

    public testinheritclass(t t) {
        this.t = t;
    }

    void output(){
        system.out.println(t.getname());
    }

    public static void main(string[] args) {
        child child=new child("李逵");
        testinheritclass<child> t=new testinheritclass<>(child);
        t.output();
    }
}
class father{
    private string name;

    public string getname() {
        return name;
    }

    public father(string name) {
        this.name = name;
    }
}
class child extends father{
    public child(string name) {
        super(name);
    }

}

测试继承接口

/**
 * 测试继承接口
 */
public class testinheritinterface<t extends ifruits> {
    private t t;

    public testinheritinterface(t t) {
        this.t = t;
    }
    void output(){
        t.shape();
    }

    public static void main(string[] args) {
        apple apple=new apple();
        testinheritinterface<apple> t=new testinheritinterface<>(apple);
        t.output();
    }
}
interface ifruits{
    //形状
    void shape();
}
class apple implements ifruits{
    @override
    public void shape() {
        system.out.println("苹果是圆形的。");
    }
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网