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

福州做网站软件以网站做跳板入侵

福州做网站软件,以网站做跳板入侵,网站流量怎么做,重庆建设工程造价信息网官网查询泛型JDK1.5设计了泛型的概念。泛型即为“类型参数”#xff0c;这个类型参数在声明它的类、接口或方法中#xff0c;代表未知的通用的类型。例如#xff1a;java.lang.Comparable接口和java.util.Comparator接口#xff0c;是用于对象比较大小的规范接口#xff0c;这两个…泛型JDK1.5设计了泛型的概念。泛型即为“类型参数”这个类型参数在声明它的类、接口或方法中代表未知的通用的类型。例如java.lang.Comparable接口和java.util.Comparator接口是用于对象比较大小的规范接口这两个接口只是限定了当一个对象大于另一个对象时返回正整数小于返回负整数等于返回0。但是并不确定是什么类型的对象比较大小之前的时候只能用Object类型表示使用时既麻烦又不安全因此JDK1.5就给它们增加了泛型。public interface ComparableT{int compareTo(T o) ; }public interface ComparatorT{int compare(T o1, T o2) ; }其中T就是类型参数即泛型。泛型的好处那么我们在使用如上面这样的接口时如果没有泛型或不指定泛型很麻烦而且有安全隐患。如果有了泛型并使用泛型那么既能保证安全又能简化代码。JavaBean圆类型class Circle{private double radius;public Circle(double radius) {super();this.radius radius;}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius radius;}Overridepublic String toString() {return Circle [radius radius ];}}比较器import java.util.Comparator;public class CircleComparator implements Comparator{Overridepublic int compare(Object o1, Object o2) {//强制类型转换Circle c1 (Circle) o1;Circle c2 (Circle) o2;return Double.compare(c1.getRadius(), c2.getRadius());}}测试类public class TestGeneric {public static void main(String[] args) {CircleComparator com new CircleComparator();System.out.println(com.compare(new Circle(1), new Circle(2)));System.out.println(com.compare(圆1, 圆2));//运行时异常ClassCastException} }使用泛型比较器class CircleComparator implements ComparatorCircle{Overridepublic int compare(Circle o1, Circle o2) {//不再需要强制类型转换代码更简洁return Double.compare(o1.getRadius(), o2.getRadius());}}测试类import java.util.Comparator;public class TestGeneric {public static void main(String[] args) {CircleComparator com new CircleComparator();System.out.println(com.compare(new Circle(1), new Circle(2)));// System.out.println(com.compare(圆1, 圆2));//编译错误因为圆1, 圆2不是Circle类型编译器提前报错而不是冒着风险在运行时再报错} }其中T是类型变量Type VariablesComparatorT这种就称为参数化类型Parameterized TypesComparatorCircle中的Circle是参数化类型的类型参数Type Arguments of Parameterized Types。类比方法的参数我们可以把T称为类型形参将Circle称为类型实参有助于我们理解泛型。 参数类型泛型类与泛型接口当我们在声明类或接口时类或接口中定义某个成员时该成员有些类型是不确定的而这个类型需要在使用这个类或接口时才可以确定那么我们可以使用泛型。 声明泛型类与泛型接口语法格式【修饰符】 class 类名类型变量列表{} 【修饰符】 interface 接口名类型变量列表{}类型变量列表可以是一个或多个类型变量一般都是使用单个的大写字母表示。例如T、K,V等。类型变量列表中的类型变量不能用于静态成员上。示例代码例如我们要声明一个学生类该学生包含姓名、成绩而此时学生的成绩类型不确定为什么呢因为语文老师希望成绩是“优秀”、“良好”、“及格”、“不及格”数学老师希望成绩是89.5, 65.0英语老师希望成绩是A,B,C,D,E。那么我们在设计这个学生类时就可以使用泛型。public class StudentT{private String name;private T score;public Student() {super();}public Student(String name, T score) {super();this.name name;this.score score;}public String getName() {return name;}public void setName(String name) {this.name name;}public T getScore() {return score;}public void setScore(T score) {this.score score;}Overridepublic String toString() {return 姓名 name , 成绩 score;} }使用泛型类与泛型接口在使用这种参数化的类与接口时我们需要指定泛型变量的实际类型参数1实际类型参数必须是引用数据类型不能是基本数据类型2在创建类的对象时指定类型变量对应的实际类型参数public class TestGeneric{public static void main(String[] args) {//语文老师使用时StudentString stu1 new StudentString(张三, 良好);//数学老师使用时//Studentdouble stu2 new Studentdouble(张三, 90.5);//错误必须是引用数据类型StudentDouble stu2 new StudentDouble(张三, 90.5);//英语老师使用时StudentCharacter stu3 new StudentCharacter(张三, C);//错误的指定//StudentObject stu new StudentString();//错误的} }JDK1.7支持简写形式StudentString stu1 new Student(张三, 良好);指定泛型实参时必须左右两边一致不存在多态现象3在继承泛型类或实现泛型接口时指定类型变量对应的实际类型参数class ChineseStudent extends StudentString{public ChineseStudent() {super();}public ChineseStudent(String name, String score) {super(name, score);}}public class TestGeneric{public static void main(String[] args) {//语文老师使用时ChineseStudent stu new ChineseStudent(张三, 良好);} } class Circle implements ComparableCircle{private double radius;public Circle(double radius) {super();this.radius radius;}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius radius;}Overridepublic String toString() {return Circle [radius radius ];}Overridepublic int compareTo(Circle c){return Double.compare(radius,c.radius);}}类型变量的上限当在声明类型变量时如果不希望这个类型变量代表任意引用数据类型而是某个系列的引用数据类型那么可以设定类型变量的上限。语法格式类型变量 extends 上限如果有多个上限类型变量 extends 上限1 上限2如果多个上限中有类有接口那么只能有一个类而且必须写在最左边。接口的话可以多个。如果在声明类型变量时没有指定任何上限默认上限是java.lang.Object。例如我们要声明一个两个数求和的工具类要求两个加数必须是Number数字类型并且实现Comparable接口。class SumToolsT extends Number ComparableT{private T a;private T b;public SumTools(T a, T b) {super();this.a a;this.b b;}SuppressWarnings(unchecked)public T getSum(){if(a instanceof BigInteger){return (T) ((BigInteger) a).add((BigInteger)b);}else if(a instanceof BigDecimal){return (T) ((BigDecimal) a).add((BigDecimal)b);}else if(a instanceof Short){return (T)(Integer.valueOf((Short)a(Short)b));}else if(a instanceof Integer){return (T)(Integer.valueOf((Integer)a(Integer)b));}else if(a instanceof Long){return (T)(Long.valueOf((Long)a(Long)b));}else if(a instanceof Float){return (T)(Float.valueOf((Float)a(Float)b));}else if(a instanceof Double){return (T)(Double.valueOf((Double)a(Double)b));}throw new UnsupportedOperationException(不支持该操作);} }测试类 public static void main(String[] args) {SumToolsInteger s new SumToolsInteger(1,2);Integer sum s.getSum();System.out.println(sum);// SumToolsString s new SumToolsString(1,2);//错误因为String类型不是extends Number}泛型擦除当使用参数化类型的类或接口时如果没有指定泛型那么会怎么样呢会发生泛型擦除自动按照最左边的第一个上限处理。如果没有指定上限上限即为Object。 public static void main(String[] args) {SumTools s new SumTools(1,2);Number sum s.getSum();System.out.println(sum);}import java.util.Comparator;public class CircleComparator implements Comparator{Overridepublic int compare(Object o1, Object o2) {//强制类型转换Circle c1 (Circle) o1;Circle c2 (Circle) o2;return Double.compare(c1.getRadius(), c2.getRadius());}}public class TestExer1 {public static void main(String[] args) {CoordinateString c1 new Coordinate(北纬38.6, 东经36.8);System.out.println(c1);// CoordinateDouble c2 new Coordinate(38.6, 38);//自动装箱与拆箱只能与对应的类型 38是int自动装为IntegerCoordinateDouble c2 new Coordinate(38.6, 36.8);System.out.println(c2);} } class CoordinateT{private T x;private T y;public Coordinate(T x, T y) {super();this.x x;this.y y;}public Coordinate() {super();}public T getX() {return x;}public void setX(T x) {this.x x;}public T getY() {return y;}public void setY(T y) {this.y y;}Overridepublic String toString() {return Coordinate [x x , y y ];}}泛型方法前面介绍了在定义类、接口时可以声明类型变量在该类的方法和属性定义、接口的方法定义中这些类型变量可被当成普通类型来用。但是在另外一些情况下1如果我们定义类、接口时没有使用类型变量但是某个方法定义时想要自己定义类型变量2另外我们之前说类和接口上的类型形参是不能用于静态方法中那么当某个静态方法想要定义类型变量。那么JDK1.5之后还提供了泛型方法的支持。语法格式【修饰符】 类型变量列表 返回值类型 方法名(【形参列表】)【throws 异常列表】{//... }类型变量列表可以是一个或多个类型变量一般都是使用单个的大写字母表示。例如T、K,V等。类型变量同样也可以指定上限示例代码我们编写一个数组工具类包含可以给任意对象数组进行从小到大排序要求数组元素类型必须实现Comparable接口public class MyArrays{public static T extends ComparableT void sort(T[] arr){for (int i 1; i arr.length; i) {for (int j 0; j arr.length-i; j) {if(arr[j].compareTo(arr[j1])0){T temp arr[j];arr[j] arr[j1];arr[j1] temp;}}}} }测试类public class TestGeneric{public static void main(String[] args) {int[] arr {3,2,5,1,4}; // MyArrays.sort(arr);//错误的因为int[]不是对象数组String[] strings {hello,java,chai};MyArrays.sort(strings);System.out.println(Arrays.toString(strings));Circle[] circles {new Circle(2.0),new Circle(1.2),new Circle(3.0)};MyArrays.sort(circles);System.out.println(Arrays.toString(circles));} }类型通配符当我们声明一个方法时某个形参的类型是一个参数化的泛型类或泛型接口类型但是在声明方法时又不确定该泛型实际类型我们可以考虑使用类型通配符。?任意类型例如我们要声明一个学生管理类这个管理类要包含一个方法可以遍历学生数组。学生管理类class StudentService {public static void print(Student?[] arr) {for (int i 0; i arr.length; i) {System.out.println(arr[i]);}} }测试类public class TestGeneric {public static void main(String[] args) {// 语文老师使用时StudentString stu1 new StudentString(张三, 良好);// 数学老师使用时// Studentdouble stu2 new Studentdouble(张三, 90.5);//错误必须是引用数据类型StudentDouble stu2 new StudentDouble(张三, 90.5);// 英语老师使用时StudentCharacter stu3 new StudentCharacter(张三, C);Student?[] arr new Student[3];arr[0] stu1;arr[1] stu2;arr[2] stu3;StudentService.print(arr);} }? extends 上限例如我们要声明一个学生管理类这个管理类要包含一个方法找出学生数组中成绩最高的学生对象。要求学生的成绩的类型必须可比较大小实现Comparable接口。学生管理类class StudentService {SuppressWarnings({ rawtypes, unchecked })public static Student? extends Comparable max(Student? extends Comparable[] arr){Student? extends Comparable max arr[0];for (int i 0; i arr.length; i) {if(arr[i].getScore().compareTo(max.getScore())0){max arr[i];}}return max;} }测试类public class TestGeneric {SuppressWarnings({ rawtypes, unchecked })public static void main(String[] args) {Student? extends Double[] arr new Student[3];arr[0] new StudentDouble(张三, 90.5);arr[1] new StudentDouble(李四, 80.5);arr[2] new StudentDouble(王五, 94.5);Student? extends Comparable max StudentService.max(arr);System.out.println(max);} }? super 下限现在要声明一个数组工具类包含可以给任意对象数组进行从小到大排序只要你指定定制比较器对象而且这个定制比较器对象可以是当前数组元素类型自己或其父类的定制比较器对象数组工具类class MyArrays{public static T void sort(T[] arr, Comparator? super T c){for (int i 1; i arr.length; i) {for (int j 0; j arr.length-i; j) {if(c.compare(arr[j], arr[j1])0){T temp arr[j];arr[j] arr[j1];arr[j1] temp;}}}} }例如有如下JavaBeanclass Person{private String name;private int age;public Person(String name, int age) {super();this.name name;this.age age;}public Person() {super();}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}Overridepublic String toString() {return name name , age age;} } class Student extends Person{private int score;public Student(String name, int age, int score) {super(name, age);this.score score;}public Student() {super();}public int getScore() {return score;}public void setScore(int score) {this.score score;}Overridepublic String toString() {return super.toString() ,score score;}}测试类public class TestGeneric {public static void main(String[] args) {Student[] all new Student[3];all[0] new Student(张三, 23, 89);all[1] new Student(李四, 22, 99);all[2] new Student(王五, 25, 67);MyArrays.sort(all, new ComparatorPerson() {Overridepublic int compare(Person o1, Person o2) {return o1.getAge() - o2.getAge();}});System.out.println(Arrays.toString(all));MyArrays.sort(all, new ComparatorStudent() {Overridepublic int compare(Student o1, Student o2) {return o1.getScore() - o2.getScore();}});System.out.println(Arrays.toString(all));} }使用类型通配符来指定类型参数的问题?不可变因为?类型不确定编译时任意类型都是错? extends 上限不可变因为? extends 上限的?可能是上限或上限的子类即类型不确定编译按任意类型处理都是错。? super 下限可以将值修改为下限或下限子类的对象因为? super 下限?代表是下限或下限的父类那么设置为下限或下限子类的对象是安全的。public class TestGeneric {public static void main(String[] args) {Student? stu1 new Student();stu1.setScore(null);//除了null无法设置为其他值Student? extends Number stu2 new Student();stu2.setScore(null);//除了null无法设置为其他值Student? super Number stu3 new Student();stu3.setScore(56);//可以设置Number或其子类的对象} } class StudentT{private String name;private T score;public Student() {super();}public Student(String name, T score) {super();this.name name;this.score score;}public String getName() {return name;}public void setName(String name) {this.name name;}public T getScore() {return score;}public void setScore(T score) {this.score score;}Overridepublic String toString() {return 姓名 name , 成绩 score;} }
http://www.w-s-a.com/news/840702/

相关文章:

  • 做直播网站要哪些技术内容营销理论
  • 价格划算的网站开发怎么找有赞做网站
  • 做网站店铺图片用什么软件网络营销方案格式
  • 做外贸要自己建网站吗有效的网络营销方式
  • 精通网站开发书籍做网站获取手机号码
  • 论坛做视频网站有哪些济南新站seo外包
  • 哪类型网站容易做冷水滩做微网站
  • 搭建企业网站流程保定徐水网站建设
  • 建设单位到江川区住房和城乡建设局网站伦敦 wordpress 设计
  • 响应式网站的服务麦德龙网站建设目标
  • 做国外单的网站叫什么海南省海口市网站建设
  • 杭州响应式网站案例wordpress5.2.2
  • 网站建设运营维护合同wordpress资源搜索插件
  • 国外网站流量查询东莞网站建设教程
  • 餐饮类网站建设达到的作用东莞工程建设交易中心网
  • 网站设计 知识产权湖北网站建设xiduyun
  • 猫咪网站模版下载中国风 古典 红色 网站源代码
  • 个人网站备案模板制作网站首页
  • 潍坊正规建设网站网站建设设计作业
  • 推荐一下网站谢谢辽宁住房城乡建设部官方网站
  • 网站文件大小英选 网站开发
  • 济南建网站哪家好wordpress编辑器排行
  • 在福州做搬家网站多少钱画册设计网站有哪些
  • 如何让别人浏览我做的网站哪些方法可以建设网站
  • 网站建设与管理网络推广的优点
  • 美食网站的设计与制作做网站的电销话术
  • 中国档案网站建设现状研究陕西建设厅执业资格注册中心网站
  • 网站建设的内容管理怎么用ps切片在dw里做网站
  • 建设婚恋网站用什么搭建涿州网站开发
  • 做知识内容的网站与app哈尔滨哪里有做网站的