日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA进阶day08泛型

發(fā)布時間:2023/12/9 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA进阶day08泛型 小編覺得挺不錯的,現(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());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ù)貼代碼

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());}}

這個代碼感覺沒什么技術(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ù)貼圖:

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());}}

介個么,就是弄出來個接口類來風騷一下。接口類中的都是全局變量跟抽象函數(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。