寻找郑州网站建设,网站搭建服务器需要什么,网站开发如何找甲方,佛山seo写这篇博文#xff0c;我并没有特别大的把握。姑且贴代码做分析。遇到什么知识点分析什么知识点吧。 class PersonT {private T age;public void setAge(T age) {this.age age;}public T getAge() {return this.age;}
}public class Generics {public static void ma…写这篇博文我并没有特别大的把握。姑且贴代码做分析。遇到什么知识点分析什么知识点吧。 class PersonT {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[]) {PersonString p new PersonString();p.setAge(3 years old);System.out.println(p.getAge());PersonInteger p2 new PersonInteger();p2.setAge(3);System.out.println(p2.getAge());}
}根据贴的这个代码我知道所谓的泛型就是在定义类的时候先不给他确定类内部的一些类型定义。在具体实现对象的时候再给根据需要给予合适的类型。
PersonString p new PersonString
需要注意一下这个地方实现的写法。嗯这个例程就这么点知识。贴下一个 class PersonT {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[]) {PersonString p new PersonString();p.setAge(3 years old);//System.out.println(p.getAge());printInfo(p);PersonInteger p2 new PersonInteger();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 PersonT {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[]) {PersonString p new PersonString();p.setAge(3 years old);//System.out.println(p.getAge());printInfo(p);PersonInteger p2 new PersonInteger();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(PersonT p) {System.out.println(p.getAge());}}
这个代码感觉没什么技术含量不说了继续。 class PersonT {private T age;public void setAge(T age) {this.age age;}public T getAge() {return this.age;}
}class StudentT extends PersonT {
}class Student2 extends PersonString {
}public class Generics {public static void main(String args[]) {PersonString p new PersonString();p.setAge(3 years old);//System.out.println(p.getAge());printInfo(p);PersonInteger p2 new PersonInteger();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);StudentInteger s new StudentInteger();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(PersonT p) {System.out.println(p.getAge());}}
这个代码的核心知识点在于泛型类的继承
class StudentT extends PersonT {
}class Student2 extends PersonString {
}
我们继续铁代码分析 interface PersonT {public void setAge(T age);public T getAge();
}class StudentT implements PersonT {T age;public void setAge(T age){this.age age;}public T getAge() {return this.age;}
}class Student2 implements PersonString {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[]) {StudentInteger s new StudentInteger();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(PersonT p) {System.out.println(p.getAge());}}
这个代码的重点在于 public static T void printInfo2(PersonT p) {System.out.println(p.getAge());}想要在方法中使用泛型需要在后面添加。写到这里猛然想到如果把这个去掉会怎样呢肯定不可以。如果去掉那么函数中就不能够直接使用这个函数了必须首先做这个类的实现。然后再去调用函数。 继续贴图 interface PersonT {public void setAge(T age);public T getAge();
}/* Integer, Float */
class StudentT extends Number implements PersonT {T age;public void setAge(T age){this.age age;}public T getAge() {return this.age;}
}class Student2 implements PersonString {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[]) {StudentInteger s new StudentInteger();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(PersonT p) {System.out.println(p.getAge());}}
介个么就是弄出来个接口类来风骚一下。接口类中的都是全局变量跟抽象函数。别的没啥了。继续贴图 interface PersonT {public void setAge(T age);public T getAge();
}class StudentT implements PersonT {T age;public void setAge(T age){this.age age;}public T getAge() {return this.age;}
}class Student2 implements PersonString {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[]) {StudentString s new StudentString();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(PersonT p) {System.out.println(p.getAge());}}
这部分讲解的是一个泛型的上限跟下限。只能跟通配符来使用不能跟着来使用也就是说只能在方法实现的时候用。而且经过编程测试在方法实现中也是不能够跟做搭档的。 我编写代码较少。具体还需要后续继续深化吧。