写这篇博文,我并没有特别大的把握。姑且贴代码做分析。遇到什么知识点分析什么知识点吧。
class Person<T> {private T age;public void setAge(T age) {this.age = age;}public T getAge() {return this.age;}
}public class Generics {public static void main(String args[]) {Person<String> p = new Person<String>();p.setAge("3 years old");System.out.println(p.getAge());Person<Integer> p2 = new Person<Integer>();p2.setAge(3);System.out.println(p2.getAge());}
}
根据贴的这个代码我知道所谓的泛型就是在定义类的时候先不给他确定类内部的一些类型定义。在具体实现对象的时候再给根据需要给予合适的类型。
Person<String> p = new Person<String>
需要注意一下这个地方实现的写法。嗯这个例程就这么点知识。贴下一个
class Person<T> {private T age;public void setAge(T age) {this.age = age;}public T getAge() {return this.age;}
}public class Generics {public static void main(String args[]) {Person<String> p = new Person<String>();p.setAge("3 years old");//System.out.println(p.getAge());printInfo(p);Person<Integer> p2 = new Person<Integer>();p2.setAge(3);//System.out.println(p2.getAge());printInfo(p2);Person<?> p3;p3 = p;//p3.setAge("4 years");p3.getAge();}public static void printInfo(Person<?> p) {System.out.println(p.getAge());}}
这个代码,跟第一次贴的包含的知识点在于
Person<?> p3;p3 = p;//p3.setAge("4 years");p3.getAge()
我们p赋值给定义的一个用通配符设置的p3后。我们的p3竟然没有了设置age的能力。这个特性听特殊的。
继续贴代码
class Person<T> {private T age;public void setAge(T age) {this.age = age;}public T getAge() {return this.age;}
}public class Generics {public static void main(String args[]) {Person<String> p = new Person<String>();p.setAge("3 years old");//System.out.println(p.getAge());printInfo(p);Person<Integer> p2 = new Person<Integer>();p2.setAge(3);//System.out.println(p2.getAge());printInfo(p2);Person<?> p3;p3 = p;//p3.setAge("4 years");p3.getAge();printInfo2(p);printInfo2(p2);printInfo2(p3);}public static void printInfo(Person<?> p) {System.out.println(p.getAge());}public static <T> void printInfo2(Person<T> p) {System.out.println(p.getAge());}}
这个代码感觉没什么技术含量,不说了继续。
class Person<T> {private T age;public void setAge(T age) {this.age = age;}public T getAge() {return this.age;}
}class Student<T> extends Person<T> {
}class Student2 extends Person<String> {
}public class Generics {public static void main(String args[]) {Person<String> p = new Person<String>();p.setAge("3 years old");//System.out.println(p.getAge());printInfo(p);Person<Integer> p2 = new Person<Integer>();p2.setAge(3);//System.out.println(p2.getAge());printInfo(p2);Person<?> p3;p3 = p;//p3.setAge("4 years");p3.getAge();printInfo2(p);printInfo2(p2);printInfo2(p3);Student<Integer> s = new Student<Integer>();s.setAge(10);printInfo(s);Student2 s2 = new Student2();s2.setAge("11 years");printInfo(s2);}public static void printInfo(Person<?> p) {System.out.println(p.getAge());}public static <T> void printInfo2(Person<T> p) {System.out.println(p.getAge());}}
这个代码的核心知识点在于泛型类的继承
class Student<T> extends Person<T> {
}class Student2 extends Person<String> {
}
我们继续铁代码分析
interface Person<T> {public void setAge(T age);public T getAge();
}class Student<T> implements Person<T> {T age;public void setAge(T age){this.age = age;}public T getAge() {return this.age;}
}class Student2 implements Person<String> {String age;public void setAge(String age){this.age = age;}public String getAge() {return this.age;}
}public class Generics {public static void main(String args[]) {Student<Integer> s = new Student<Integer>();s.setAge(10);printInfo(s);Student2 s2 = new Student2();s2.setAge("11 years");printInfo(s2);}public static void printInfo(Person<?> p) {System.out.println(p.getAge());}public static <T> void printInfo2(Person<T> p) {System.out.println(p.getAge());}}
这个代码的重点在于
public static <T> void printInfo2(Person<T> p) {System.out.println(p.getAge());}
想要在方法中使用泛型需要在static后面添加<T>。写到这里猛然想到如果把这个static去掉会怎样呢?肯定不可以。如果去掉static那么main函数中就不能够直接使用这个函数了,必须首先做generics这个类的实现。然后再去调用printInfo函数。
继续贴图:
interface Person<T> {public void setAge(T age);public T getAge();
}/* Integer, Float */
class Student<T extends Number> implements Person<T> {T age;public void setAge(T age){this.age = age;}public T getAge() {return this.age;}
}class Student2 implements Person<String> {String age;public void setAge(String age){this.age = age;}public String getAge() {return this.age;}
}public class Generics {public static void main(String args[]) {Student<Integer> s = new Student<Integer>();s.setAge(10);printInfo(s);Student2 s2 = new Student2();s2.setAge("11 years");printInfo(s2);}public static void printInfo(Person<?> p) {System.out.println(p.getAge());}public static <T> void printInfo2(Person<T> p) {System.out.println(p.getAge());}}
介个么,就是弄出来个接口类来风骚一下。接口类中的都是全局变量跟抽象函数。别的没啥了。继续贴图
interface Person<T> {public void setAge(T age);public T getAge();
}class Student<T> implements Person<T> {T age;public void setAge(T age){this.age = age;}public T getAge() {return this.age;}
}class Student2 implements Person<String> {String age;public void setAge(String age){this.age = age;}public String getAge() {return this.age;}
}public class Generics {public static void main(String args[]) {Student<String> s = new Student<String>();s.setAge("10");printInfo(s);Student2 s2 = new Student2();s2.setAge("11 years");printInfo(s2);}public static void printInfo(Person<? super String> p) {System.out.println(p.getAge());}public static <T> void printInfo2(Person<T> p) {System.out.println(p.getAge());}}
这部分讲解的是一个泛型的上限跟下限。super只能跟通配符来使用不能跟着<T>来使用也就是说只能在方法实现的时候用。而且经过编程测试,在方法实现中,<T>也是不能够跟super做搭档的。
我编写java代码较少。具体还需要后续继续深化吧。