JAVA进阶day08泛型
寫這篇博文,我并沒有特別大的把握。姑且貼代碼做分析。遇到什么知識點分析什么知識點吧。
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());} }根據(jù)貼的這個代碼我知道所謂的泛型就是在定義類的時候先不給他確定類內(nèi)部的一些類型定義。在具體實現(xiàn)對象的時候再給根據(jù)需要給予合適的類型。
Person<String> p = new Person<String>需要注意一下這個地方實現(xiàn)的寫法。嗯這個例程就這么點知識。貼下一個
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的能力。這個特性聽特殊的。
繼續(xù)貼代碼
這個代碼感覺沒什么技術(shù)含量,不說了繼續(xù)。
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> { }我們繼續(xù)鐵代碼分析
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函數(shù)中就不能夠直接使用這個函數(shù)了,必須首先做generics這個類的實現(xiàn)。然后再去調(diào)用printInfo函數(shù)。
繼續(xù)貼圖:
介個么,就是弄出來個接口類來風騷一下。接口類中的都是全局變量跟抽象函數(shù)。別的沒啥了。繼續(xù)貼圖
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>來使用也就是說只能在方法實現(xiàn)的時候用。而且經(jīng)過編程測試,在方法實現(xiàn)中,<T>也是不能夠跟super做搭檔的。
我編寫java代碼較少。具體還需要后續(xù)繼續(xù)深化吧。
總結(jié)
以上是生活随笔為你收集整理的JAVA进阶day08泛型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python--随机森林模型
- 下一篇: 随机森林模型的原理