当前位置: 首页 > news >正文

对政府网站建设发展趋势的认识电商网站建设定制

对政府网站建设发展趋势的认识,电商网站建设定制,花生壳如何建设网站,蚌埠注册公司目录 ​编辑 1.泛型 1.1Object类引出泛型概念 2.泛型语法 2.1泛型编写代码 3.泛型的机制 3.1擦除机制 4.泛型的上界 4.1泛型上界的语法 4.2泛型上界的使用 5.泛型方法 5.1泛型方法语法 5.2泛型方法的使用 1.泛型 一般的类和方法中,只能使用具体的代码…

目录

​编辑

1.泛型

1.1Object类引出泛型概念

2.泛型语法

2.1泛型编写代码

3.泛型的机制

3.1擦除机制

4.泛型的上界

4.1泛型上界的语法

4.2泛型上界的使用

5.泛型方法

5.1泛型方法语法

5.2泛型方法的使用

1.泛型

一般的类和方法中,只能使用具体的代码来实现同一种类型数据的操作。比如一个数组里面存储的是同一种类型,这种存储方式太过于死板。因此JDK1.5引入了新的语法:泛型,通俗的来讲泛型就是多种数据类型(泛滥),从代码上来说就是实现了不同类型之间的存储,因此当我们想要存储各种各样的数据时,我们会使用到泛型。


1.1Object类引出泛型概念

在泛型之前,我们在Object类中学到了,所有类的父类都是Object类,因此我们能把一个数组设置为Object类型呢,这样就能达到数组里面存放各种各样的元素。所以我们可以这样去写代码:

class MyArray {//Object类型数组Object[] arr = new Object[3];public void show() {//分别初始化三种不同类型的数据arr[0] = 1;arr[1] = 1.2;arr[2] = "abc";//遍历arr数组for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);}}
}
public class Test {public static void main(String[] args) {MyArray myArray = new MyArray();myArray.show();}
}

运行后输出:

以上代码是可以很好的运行,因为我们直接初始化了Object类中的元素。当我们使用get、set方法来实现时就会发现不同处。

class MyArray {//Object类型数组public Object[] arr = new Object[3];//提供get方法public Object getPos(int pos) {return this.arr[pos];}//提供set方法public void setArr(int pos,Object value) {this.arr[pos] = value;}
}public class Test {public static void main(String[] args) {MyArray myArray = new MyArray();//分别设置了三种不同类型的元素myArray.setArr(0,1);myArray.setArr(1,1.2);myArray.setArr(2,"abc");//分别输出了三种不同类型的元素System.out.println(myArray.getPos(0));System.out.println(myArray.getPos(1));//输出字符类型时,报错System.out.println(myArray.getPos(3));}
}

运行后输出:

我们发现,当我往数组中添加了一个字符串时就会出现异常。所以,Object在存储不同类型的时候

还是会出现错误。因此,我们可以想到既然不让我存不同类型的数据,那么我就存同一种类型的数据就好了,这时我们就可以用到泛型。它可以将不同类型数据存储在不同的对象中,但在不同的对象中每一个元素的类型是相同的。


2.泛型语法

(1)语法1

//泛型类语法格式
class 泛型类名称<类型形参列表> {//内容
}
//泛型类中形参可为多个
class ClassName<T,S,B,U> {//内容
}

上述代码中,我们可以看到泛型类与普通类多了一个<>其余并无太大差异,注意<>内可写多个参数。 

(2)语法2 

//泛型类继承类或泛型类
class 泛型类名称<类型形参列表> extends 类名 {}
//泛型类继承泛型类
class ClassName1<T,S,B,U> extends ClassName2<T> {}

 上述代码,表示了泛型类可以继承一个普通类,也可以继承一个泛型类


2.1泛型编写代码

因此,我们可以这样去编写一段泛型代码:

class MyArray<T> {//创建一个泛型数组T[] arr= (T[])new Object[3];//get方法public T getPos (int pos) {return this.arr[pos];}//set方法public void setArr (int pos,T value) {this.arr[pos] = value;}
}
public class Test {public static void main(String[] args) {//myArray1对象设置int类型数据MyArray<Integer> myArray1 = new MyArray<>();myArray1.setArr(0,1);myArray1.setArr(1,2);myArray1.setArr(2,3);//myArray2对象设置String类型数据MyArray<String> myArray2 = new MyArray<>();myArray2.setArr(0,"a");myArray2.setArr(1,"b");myArray2.setArr(2,"c");//通过get方法输出各个下标元素System.out.print(myArray1.getPos(0)+" ");System.out.print(myArray1.getPos(1)+" ");System.out.print(myArray1.getPos(2)+" ");System.out.print(myArray2.getPos(0)+" ");System.out.print(myArray2.getPos(1)+" ");System.out.print(myArray2.getPos(2));}
}

运行后输出:

以上代码,就是泛型的一个体现,我们要想设置什么类型的数据就在<>里面设置什么类型的包装类即可。上述代码中相信get和set方法对于大家来说不是很难理解,但很多小伙伴第一件见这种代码,可能有些问题不太清楚,因此我来做出解释:

  1. 类名后面的<T>代表着占位符,表示着当前类为一个泛型类。<>里面的内容可以任意填写,你可以输入E、K、N等等。注意应当填写见名思意内容如T代表着type,N代表着number。
  2. T[] arr = new T[3];是不可行的,因为泛型不能直接new一个数组,但是我们可以强制类型转换如T[] arr = (T[]) new Object[];。
  3. 实例化泛型类时应当在<>只能是引用类型不得是基本类型因此通常我们填写包装类,并且该对象中值的类型要一致。
  4. 实例化泛型类对象时,前面<>内内容不得省略,后面<>内容可以省略。如:Array<String> array = new Array<> ();。

3.泛型的机制

泛型是一种运行时的机制,它会在编译时给我们指出一些错误,也会在获取元素时帮助我们进行强制类型转换。

(1)编译时指出错误

    public static void main(String[] args) {MyArray<int> myArray = new MyArray<>();}

报错:

上述报错,代表着泛型类中<>内容类型不能被定义为int。在上文中我们知道了,泛型类<>里面得是一个引用类型,因此int不能作为<>内参数。再比如以下代码:

    public static void main(String[] args) {MyArray<Integer> myArray = new MyArray<>();myArray.setArr(0,"abc");}

报错:

上述代码,我们在给set方法传参的时候传了一个String类型的数据,并不符合myArray这个对象的属性。因此造成报错现象。


(2)帮助进行强制类型转换

class MyArray<T> {//创建一个泛型数组public Object[] arr = new Object[3];//get方法public T getPos (int pos) {return (T)arr[pos];}//set方法public void setArr (int pos,T value) {arr[pos] = value;}
}
public class Test {public static void main(String[] args) {//Integer泛型类MyArray<Integer> myArray = new MyArray<>();//自动帮我们进行类型转换了myArray.setArr(0,23);//String泛型类MyArray<String> myArray1 = new MyArray<>();//自动帮我们进行类型转换了myArray1.setArr(0,"abc");}
}

以上代码中,set方法形参列表第二个参数value为泛型T类型,但是在main方法中。我在给setArr方法传参的时候,直接传了一个整型和一个字符串。编译器并没有报错,那是因为泛型自动帮助我们进行了强制类型转换,也就是把T类型分别转成了整型和字符串型。


3.1擦除机制

通过上述讲解我们知道了,泛型会在我们编译时显示错误会帮助我们强制类型转换。表明了泛型是一种编译时的机制。那我们的泛型在运行后会是什么样呢?其实我们的泛型在编译后会被擦除为Object类型。

class MyArray<T> {//创建一个泛型数组T[] arr= (T[])new Object[3];//get方法public T getPos (int pos) {return this.arr[pos];}//set方法public void setArr (int pos,T value) {this.arr[pos] = value;}
}
public class Test {public static void main(String[] args) {//实例一个泛型为Integer类的对象MyArray<Integer> myArray = new MyArray<>();myArray.setArr(0,1);myArray.setArr(1,2);//实例一个泛型为String类的对象MyArray<String> myArray1 = new MyArray<>();myArray1.setArr(0,"abc");myArray1.setArr(1,"def");}
}

调试后: 

我们可以发现明明我们在创建数组的时候是这样的 T[] arr= (T[])new Object[3];,但编译器后台自动给我们编程了Object类型。因此,我们可以知道运行后编译器会擦除泛型类型给我们转换为Object类型。

所以,我们可以这样创建一个泛型数组:

class MyArray<T> {//创建一个泛型数组public Object[] arr = new Object[3];//get方法public T getPos (int pos) {return (T)arr[pos];}//set方法public void setArr (int pos,T value) {arr[pos] = value;}
}

上述代码对数组进行一个初始化才是地道的初始化,而原来的T[] arr = (T[]) new T[3];并不是很地道,但也能达到效果。


4.泛型的上界

有一程序要求通过泛型找出一个数组的最大值,因此有以下代码:

class MaxArray<T> {public void findMax(T[] arr) {T max = arr[0];for (int i = 0; i < arr.length; i++) {if (max > arr[i]) {max = arr[i];}}System.out.println(max);}
}
public class Test {public static void main(String[] args) {MaxArray<Integer> maxArray =new MaxArray<>();Integer[] integers = {1,3,4,5,10,8,9,5,20};maxArray.findMax(integers);}
}

运行后报错:

上述代码报错原因是if里面的判断,当基本类型之间进行判断时可以使用算术符,当基本类型与引用类型之间进行判断时我们就得使用Comparable方法来判断。但是我们发现上述泛型并没有使用Comparable接口,因此我们可以使泛型继承这个接口就可以实现该操作,那么这样一个操作就代表着泛型上界这样一个概念。


4.1泛型上界的语法

class 泛型类<参数列表 extems 类型边界> {//内容}

以上代码就是泛型类中参数类型继承一个类型边界的创建,实例:

class Array<T extends Number> {//内容
}

只接受 Number 的子类型作为 T 的类型实参,因此只有关于Number的子类型我们能使用 ,比如Integer。String类型就不能。如:

class Num<T extends Number> {//以下三个都行Num<Integer> num1 = new Num<>();Num<Byte> num2 = new Num<>();Num<Double> num3 = new Num<>();//会报错Num<String> num4 = new Num<>();
}

报错:

jdk-8帮助手册中描述了以下类为Number子类。


4.2泛型上界的使用

还是4.1中那段代码,我们既然不能使用<来比较一个基本类型和一个引用类型,那我们就使T类型继承Comparable接口。

class MaxArray<T extends Comparable<T>> {public void findMax(T[] arr) {T max = arr[0];for (int i = 0; i < arr.length; i++) {//使用了Comparable接口中的compareTo方法if (max.compareTo(arr[i]) < 0) {max = arr[i];}}System.out.println(max);}
}
public class Test {public static void main(String[] args) {MaxArray<Integer> maxArray =new MaxArray<>();Integer[] integers = {1,3,4,5,10,8,9,5,20};maxArray.findMax(integers);}
}

运行后输出:

以上代码中泛型类中T类型继承了Comparable接口,因此if判断里面可以使用Comparable接口中的compareTo方法。此方法返回的值小于0代表前者比后者小,返回值等于0代表前者与后者相等,返回值大于0代表前者比后者大。


5.泛型方法

在上面我们学习到了泛型类的使用,那么泛型也是有方法的。我们可以把一个普通方法变成泛型方法去使用,那么泛型方法具体有什么用呢?下面我就来讲解:


5.1泛型方法语法

泛型方法的语法格式为:方法限定修饰符<类型形参列表> 返回值类型 方法名称(参数列表){ //内容 }。

实例:

public <T> T maxNumber(T[] arr) {//内容}

5.2泛型方法的使用

求数组中的最大数:

class Array {public <T extends Comparable<T>> T maxNum(T[] num){T max = num[0];for (int i = 0; i < num.length; i++) {if (max.compareTo(num[i]) < 0) {max = num[i];}}return max;}
}
public class Test {public static void main(String[] args) {Array array = new Array();Integer[] arr = {1,23,4,5,6,7};Integer max=array.<Integer>maxNum(arr);System.out.println(max);}
}

运行后输出:

以上代码展示了泛型方法的使用,我们可以看到泛型方法的语法比较抽象,这就是泛型方法的难点之处。


本期博客到这里就结束了,感谢各位的阅读。

下期预告:ArrayList与顺序表


文章转载自:
http://dinncowreckage.bkqw.cn
http://dinncogravenstein.bkqw.cn
http://dinncoambrosian.bkqw.cn
http://dinncoeparch.bkqw.cn
http://dinncosmart.bkqw.cn
http://dinncophotoglyphy.bkqw.cn
http://dinncogasman.bkqw.cn
http://dinncoexterritoriality.bkqw.cn
http://dinncolibran.bkqw.cn
http://dinncoso.bkqw.cn
http://dinncodeterminate.bkqw.cn
http://dinncobejabbers.bkqw.cn
http://dinncomarline.bkqw.cn
http://dinncoschwarzwald.bkqw.cn
http://dinncocephalous.bkqw.cn
http://dinncoundisturbedly.bkqw.cn
http://dinncoexpansionism.bkqw.cn
http://dinncoskyborne.bkqw.cn
http://dinncoscotice.bkqw.cn
http://dinnconeotropical.bkqw.cn
http://dinncomelanogenesis.bkqw.cn
http://dinncodeerstalker.bkqw.cn
http://dinncosemiologist.bkqw.cn
http://dinncoatoneable.bkqw.cn
http://dinncobosseyed.bkqw.cn
http://dinncotycooness.bkqw.cn
http://dinncofeirie.bkqw.cn
http://dinncoresurvey.bkqw.cn
http://dinncosidearm.bkqw.cn
http://dinncohydroxyphenyl.bkqw.cn
http://dinncosuperiority.bkqw.cn
http://dinncoicosidodecahedron.bkqw.cn
http://dinncobrushability.bkqw.cn
http://dinncoepigenous.bkqw.cn
http://dinncodactinomycin.bkqw.cn
http://dinncocrossgrained.bkqw.cn
http://dinncocapitao.bkqw.cn
http://dinncounblessed.bkqw.cn
http://dinncoappraisal.bkqw.cn
http://dinncointake.bkqw.cn
http://dinncoresistent.bkqw.cn
http://dinncolavalava.bkqw.cn
http://dinncoray.bkqw.cn
http://dinncosuccedent.bkqw.cn
http://dinncoferrovanadium.bkqw.cn
http://dinncopraecocial.bkqw.cn
http://dinncomaya.bkqw.cn
http://dinncoholland.bkqw.cn
http://dinncoamatory.bkqw.cn
http://dinncogrub.bkqw.cn
http://dinncotheirself.bkqw.cn
http://dinncohydrophytic.bkqw.cn
http://dinncodangerousness.bkqw.cn
http://dinncoflesher.bkqw.cn
http://dinncochapelmaster.bkqw.cn
http://dinncophotoproduct.bkqw.cn
http://dinncopleiotropic.bkqw.cn
http://dinncojocko.bkqw.cn
http://dinncoconation.bkqw.cn
http://dinncoinchworm.bkqw.cn
http://dinncoow.bkqw.cn
http://dinncovinegrower.bkqw.cn
http://dinncocallipers.bkqw.cn
http://dinncoabiochemistry.bkqw.cn
http://dinncocatbrier.bkqw.cn
http://dinncoresupine.bkqw.cn
http://dinncocuboid.bkqw.cn
http://dinncojacklighter.bkqw.cn
http://dinncoquixotry.bkqw.cn
http://dinncolavation.bkqw.cn
http://dinncocarrucate.bkqw.cn
http://dinncobeggarhood.bkqw.cn
http://dinncoimpeachment.bkqw.cn
http://dinncowestfalen.bkqw.cn
http://dinncotinkly.bkqw.cn
http://dinncopommard.bkqw.cn
http://dinncoolefin.bkqw.cn
http://dinncopize.bkqw.cn
http://dinncoskivvy.bkqw.cn
http://dinncoclianthus.bkqw.cn
http://dinncograndchild.bkqw.cn
http://dinncooxford.bkqw.cn
http://dinncodecimator.bkqw.cn
http://dinncoacidophil.bkqw.cn
http://dinnconighttime.bkqw.cn
http://dinncozombi.bkqw.cn
http://dinncocryoscopy.bkqw.cn
http://dinncoliberticidal.bkqw.cn
http://dinncowasteless.bkqw.cn
http://dinncopolacre.bkqw.cn
http://dinncomsae.bkqw.cn
http://dinncosurvival.bkqw.cn
http://dinncogreyfish.bkqw.cn
http://dinncogrannie.bkqw.cn
http://dinncojuryman.bkqw.cn
http://dinncocholangitis.bkqw.cn
http://dinncoantispeculation.bkqw.cn
http://dinncowaterishlogged.bkqw.cn
http://dinncoinhibiting.bkqw.cn
http://dinncocanonization.bkqw.cn
http://www.dinnco.com/news/161532.html

相关文章:

  • 做网站设计的长宽一般是多少友情链接交换的意义是什么
  • 青岛做网站方案营业推广策略
  • 东莞腾宇科技网站建设推广软件平台
  • 网站如何在工信部备案推广文案怎么写
  • 做数学题好的网站广州搜发网络科技有限公司
  • 南京驰铭做网站公司国内比百度好的搜索引擎
  • 建设银行登录用户名是什么惠州seo优化
  • 网站文章的作用网站seo推广排名
  • 公司网站横幅是做的吗福建百度推广
  • 海淀网站建设公司网站怎么优化关键词排名
  • 学3dmax做效果图的网站seo优化上海牛巨微
  • 中山微信网站谷歌seo外包
  • 基层政权和社区建设司网站山东进一步优化
  • 北京做网站找哪家好如何进行网站宣传推广
  • 建设网站的价格表百度发布信息怎么弄
  • 网站建站模式广东免费网络推广软件
  • 马鞍山网站建设价格免费的网页制作软件
  • 个人网站可以做论坛中国法律服务网app最新下载
  • wordpress miwoftpseo技术教程博客
  • 图片素材网站哪个最好网站关键词优化价格
  • 做可以上传文件的网站长春网站优化体验
  • 上海信息公司做网站黄山seo
  • 成都app定制公司搜索引擎优化的例子
  • 阿里巴巴网站建设目标seo词库排行
  • iis 做网站青岛seo青岛黑八网络最强
  • wordpress加个文本框seo到底是做什么的
  • 智慧校园官网南京百度关键字优化价格
  • 广州做护肤品的网站网络广告营销案例
  • 福田企业网站优化最好的方法软文广告文案
  • 无锡专业做网站建设直链平台