关键词:
一、理论知识部分
泛型:也称参数化类型,就是在定义类、接口和方法时,通过类型参数指示将要处理的对象类型。(如ArrayList类)
泛型程序设计(Generic programming):编写代码可以被很多不同类型的对象所重用。
一个泛型类(generic class)就是具有一个或多个类型变量的类,即创建用类型作为参数的类。如一个泛型类定义格式如下: class Generics<K,V>其中的K和V是类中的可变类型参数。
泛型类可以有多个类型变量。例如: public class Pair<t, u=""> <T,U> …
类定义中的类型变量用于指定方法的返回类型以及域、局部变量的类型。
泛型方法:除了泛型类外,还可以只单独定义一个方法作为泛型方法,用于指定方法参数或者返回值为泛型类型,留待方法调用时确定。泛型方法可以声明在泛型类中,也可以声明在 普通类中。
extends关键字所声明的上界既可以是一个类,也可以是一个接口
<T extends Bounding Type>表示T应该是绑定类型的子类型。一个类型变量或通配符可以有多个限定,限定类型用“&”分割。
泛型变量下界:通过使用super关键字可以固定泛型参数的类型为某种类型或者其超类。当程序希望为一个方法的参数限定类型时,通常可以使用下限通配符
通配符:“?”符号表明参数的类型可以是任何一种类型,它和参数T的含义是有区别的。T表示一种未知类型,而“?”表示任何一种类型。这种通配符一般有以下三种用法:单独的?:用于表示任何类型。 ? extends type:表示带有上界。 ? super type:表示带有下界。
二、实验部分
1、实验目的与要求
(1) 理解泛型概念;
(2) 掌握泛型类的定义与使用;
(3) 掌握泛型方法的声明与使用;
(4) 掌握泛型接口的定义与实现;
(5)了解泛型程序设计,理解其用途。
2、实验内容和步骤
实验1: 导入第8章示例程序,测试程序并进行代码注释。
测试程序1:
l 编辑、调试、运行教材311、312页 代码,结合程序运行结果理解程序;
l 在泛型类定义及使用代码处添加注释;
l 掌握泛型类的定义及使用
package pair1; /** * @version 1.00 2004-05-10 * @author Cay Horstmann */ public class Pair<T> private T first; private T second; public Pair() first = null; second = null; public Pair(T first, T second) this.first = first; this.second = second; public T getFirst() return first; public T getSecond() return second; public void setFirst(T newValue) first = newValue; public void setSecond(T newValue) second = newValue;
package pair1; /** * @version 1.01 2012-01-26 * @author Cay Horstmann */ public class PairTest1 public static void main(String[] args) String[] words = "Mary", "had", "a", "little", "lamb" ;//泛型对象是字符串对象 Pair<String> mm = ArrayAlg.minmax(words); System.out.println("min = " + mm.getFirst()); System.out.println("max = " + mm.getSecond()); class ArrayAlg /** * Gets the minimum and maximum of an array of strings. * @param a an array of strings * @return a pair with the min and max value, or null if a is null or empty */ public static Pair<String> minmax(String[] a)//用具体的类型替换类型变量可以实例化泛型类型 if (a == null || a.length == 0) return null; String min = a[0]; String max = a[0]; for (int i = 1; i < a.length; i++) if (min.compareTo(a[i]) > 0) min = a[i]; if (max.compareTo(a[i]) < 0) max = a[i]; return new Pair<>(min, max);
测试程序2:
l 编辑、调试运行教材315页 PairTest2,结合程序运行结果理解程序;
l 在泛型程序设计代码处添加相关注释;
l 掌握泛型方法、泛型变量限定的定义及用途。
package pair2; /** * @version 1.00 2004-05-10 * @author Cay Horstmann */ public class Pair<T> private T first; private T second; public Pair() first = null; second = null; public Pair(T first, T second) this.first = first; this.second = second; public T getFirst() return first; public T getSecond() return second; public void setFirst(T newValue) first = newValue; public void setSecond(T newValue) second = newValue;
package pair2; import java.time.LocalDate; /** * @version 1.02 2015-06-21 * @author Cay Horstmann */ public class PairTest2 public static void main(String[] args) LocalDate[] birthdays = LocalDate.of(1906, 12, 9), // G. Hopper LocalDate.of(1815, 12, 10), // A. Lovelace LocalDate.of(1903, 12, 3), // J. von Neumann LocalDate.of(1910, 6, 22), // K. Zuse ; Pair<LocalDate> mm = ArrayAlg.minmax(birthdays); System.out.println("min = " + mm.getFirst()); System.out.println("max = " + mm.getSecond()); class ArrayAlg /** Gets the minimum and maximum of an array of objects of type T. @param a an array of objects of type T @return a pair with the min and max value, or null if a is null or empty */ public static <T extends Comparable> Pair<T> minmax(T[] a) //通过对类型变量T设置限定,将T限制为实现了Comparable接口的类 if (a == null || a.length == 0) return null; T min = a[0]; T max = a[0]; for (int i = 1; i < a.length; i++) if (min.compareTo(a[i]) > 0) min = a[i]; if (max.compareTo(a[i]) < 0) max = a[i]; return new Pair<>(min, max);
测试程序3:
l 用调试运行教材335页 PairTest3,结合程序运行结果理解程序;
l 了解通配符类型的定义及用途。
package pair3; /** * @version 1.00 2004-05-10 * @author Cay Horstmann */ public class Pair<T> private T first; private T second; public Pair() first = null; second = null; public Pair(T first, T second) this.first = first; this.second = second; public T getFirst() return first; public T getSecond() return second; public void setFirst(T newValue) first = newValue; public void setSecond(T newValue) second = newValue;
package pair3; import java.time.*; public class Employee private String name; private double salary; private LocalDate hireDay; public Employee(String name, double salary, int year, int month, int day) this.name = name; this.salary = salary; hireDay = LocalDate.of(year, month, day); public String getName() return name; public double getSalary() return salary; public LocalDate getHireDay() return hireDay; public void raiseSalary(double byPercent) double raise = salary * byPercent / 100; salary += raise;
package pair3; public class Manager extends Employee private double bonus; /** @param name the employee‘s name @param salary the salary @param year the hire year @param month the hire month @param day the hire day */ public Manager(String name, double salary, int year, int month, int day) super(name, salary, year, month, day); bonus = 0; public double getSalary() double baseSalary = super.getSalary(); return baseSalary + bonus; public void setBonus(double b) bonus = b; public double getBonus() return bonus;
package pair3; /** * @version 1.01 2012-01-26 * @author Cay Horstmann */ public class PairTest3 public static void main(String[] args) Manager ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15); Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15); Pair<Manager> buddies = new Pair<>(ceo, cfo); printBuddies(buddies); ceo.setBonus(1000000); cfo.setBonus(500000); Manager[] managers = ceo, cfo ; Pair<Employee> result = new Pair<>(); minmaxBonus(managers, result); System.out.println("first: " + result.getFirst().getName() + ", second: " + result.getSecond().getName()); maxminBonus(managers, result); System.out.println("first: " + result.getFirst().getName() + ", second: " + result.getSecond().getName()); //打印雇员对的方法 public static void printBuddies(Pair<? extends Employee> p)//使用通配符类型,表示带有上界。 Employee first = p.getFirst(); Employee second = p.getSecond(); System.out.println(first.getName() + " and " + second.getName() + " are buddies."); public static void minmaxBonus(Manager[] a, Pair<? super Manager> result)//表示带有下界。 if (a.length == 0) return; Manager min = a[0]; Manager max = a[0]; for (int i = 1; i < a.length; i++) if (min.getBonus() > a[i].getBonus()) min = a[i]; if (max.getBonus() < a[i].getBonus()) max = a[i]; result.setFirst(min); result.setSecond(max); public static void maxminBonus(Manager[] a, Pair<? super Manager> result) minmaxBonus(a, result); PairAlg.swapHelper(result); // OK--swapHelper captures wildcard type // Can‘t write public static <T super manager> ... class PairAlg public static boolean hasNulls(Pair<?> p)//无限定的通配符 return p.getFirst() == null || p.getSecond() == null; public static void swap(Pair<?> p) swapHelper(p); public static <T> void swapHelper(Pair<T> p) T t = p.getFirst(); p.setFirst(p.getSecond()); p.setSecond(t);
实验2:编程练习:
编程练习1:实验九编程题总结
l 实验九编程练习1总结(从程序总体结构说明、模块说明,目前程序设计存在的困难与问题三个方面阐述)。
总体结构:主类Main 子类Person
模块说明:Main:查找文件,对文件进行读取。
Person:对文件进行具体的处理
package ID; public class Person implements Comparable<Person> private String name; private String ID; private int age; private String sex; private String birthplace; public String getname() return name; public void setname(String name) this.name = name; public String getID() return ID; public void setID(String ID) this.ID = ID; public int getage() return age; public void setage(int age) this.age = age; public String getsex() return sex; public void setsex(String sex) this.sex = sex; public String getbirthplace() return birthplace; public void setbirthplace(String birthplace) this.birthplace = birthplace; public int compareTo(Person o) return this.name.compareTo(o.getname()); public String toString() return name + " " + sex + " " + age + " " + ID + " " + birthplace + " ";
package ID; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Main private static ArrayList<Person> Personlist; public static void main(String[] args) Personlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("身份证号.txt"); try FileInputStream fis = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String temp = null; while ((temp = in.readLine()) != null) Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String ID = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String place = linescanner.nextLine(); Person Person = new Person(); Person.setname(name); Person.setID(ID); Person.setsex(sex); int a = Integer.parseInt(age); Person.setage(a); Person.setbirthplace(place); Personlist.add(Person); catch (FileNotFoundException e) System.out.println("查找不到信息"); e.printStackTrace(); catch (IOException e) System.out.println("信息读取有误"); e.printStackTrace(); boolean isTrue = true; while (isTrue) System.out.println("1:按姓名字典序输出人员信息"); System.out.println("2:查询最大年龄与最小年龄人员信息"); System.out.println("3:按省份找同乡"); System.out.println("4:输入你的年龄,查询年龄与你最近人的信息"); System.out.println("5:退出"); int nextInt = scanner.nextInt(); switch (nextInt) case 1: Collections.sort(Personlist); System.out.println(Personlist.toString()); break; case 2: int max = 0, min = 100; int j, k1 = 0, k2 = 0; for (int i = 1; i < Personlist.size(); i++) j = Personlist.get(i).getage(); if (j > max) max = j; k1 = i; if (j < min) min = j; k2 = i; System.out.println("年龄最大:" + Personlist.get(k1)); System.out.println("年龄最小:" + Personlist.get(k2)); break; case 3: System.out.println("省份?"); String find = scanner.next(); String place = find.substring(0, 3); String place2 = find.substring(0, 3); for (int i = 0; i < Personlist.size(); i++) if (Personlist.get(i).getbirthplace().substring(1, 4).equals(place)) System.out.println("同乡 " + Personlist.get(i)); break; case 4: System.out.println("年龄:"); int yourage = scanner.nextInt(); int near = agenear(yourage); int d_value = yourage - Personlist.get(near).getage(); System.out.println("" + Personlist.get(near)); break; case 5: isTrue = false; System.out.println("欢迎使用!"); break; default: System.out.println("输入有误"); public static int agenear(int age) int j = 0, min = 53, d_value = 0, k = 0; for (int i = 0; i < Personlist.size(); i++) d_value = Personlist.get(i).getage() - age; if (d_value < 0) d_value = -d_value; if (d_value < min) min = d_value; k = i; return k;
问题:查找不到文件,代码编程不是太会
l 实验九编程练习2总结(从程序总体结构说明、模块说明,目前程序设计存在的困难与问题三个方面阐述)。
总体结构:主类:Main 子类:math
模块说明:Main:随机生成十道100内的计算题,并判断答案正误
math:对具体计算进行处理
public class math private int a; private int b; public static int add(int a, int b) return a + b; public static int reduce(int a, int b) return a - b; public static int multiplication(int a, int b) return a * b; public static int division(int a, int b) if (b != 0) return a / b; else return 0;
import java.io.*; import java.io.PrintWriter; import java.util.Scanner; public class Main public static void main(String[] args) Scanner in = new Scanner(System.in); int sum = 0; for (int i = 1; i <= 10; i++) int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int m = (int) Math.round(Math.random() * 4); switch (m) case 1: System.out.println(i + ": " + a + "/" + b + "="); while (b == 0) b = (int) Math.round(Math.random() * 100); int c1 = in.nextInt(); if (c1 == a / b) System.out.println("恭喜答案正确"); sum += 10; else System.out.println("抱歉,答案错误"); break; case 2: System.out.println(i + ": " + a + "*" + b + "="); int c2 = in.nextInt(); if (c2 == a * b) System.out.println("恭喜答案正确"); sum += 10; else System.out.println("抱歉,答案错误"); break; case 3: System.out.println(i + ": " + a + "+" + b + "="); int c3 = in.nextInt(); if (c3 == a + b) System.out.println("恭喜答案正确"); sum += 10; else System.out.println("抱歉,答案错误"); break; case 4: System.out.println(i + ": " + a + "-" + b + "="); int c4 = in.nextInt(); if (c4 == a - b) System.out.println("恭喜答案正确"); sum += 10; else System.out.println("抱歉,答案错误"); break; System.out.println("成绩" + sum);
问题:不符合小学四则运算要求,有的计算结果出现了负数
编程练习2:采用泛型程序设计技术改进实验九编程练习2,使之可处理实数四则运算,其他要求不变。
import java.io.*; import java.io.PrintWriter; import java.util.Scanner; public class Main public static void main(String[] args) Scanner in = new Scanner(System.in); PrintWriter output = null; try output = new PrintWriter("text.txt"); catch (FileNotFoundException e) // TODO Auto-generated catch block e.printStackTrace(); int sum = 0; for (int i = 1; i <= 10; i++) int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int m = (int) Math.round(Math.random() * 4); switch (m) case 1: while (b == 0) b = (int) Math.round(Math.random() * 100); while (a % b != 0) a = (int) Math.round(Math.random() * 100); System.out.println(i + ": " + a + "/" + b + "="); int c1 = in.nextInt(); output.println(a + "/" + b + "=" + c1); if (c1 == a / b) System.out.println("恭喜答案正确"); sum += 10; else System.out.println("抱歉,答案错误"); break; case 2: System.out.println(i + ": " + a + "*" + b + "="); int c2 = in.nextInt(); output.println(a + "*" + b + "=" + c2); if (c2 == a * b) System.out.println("恭喜答案正确"); sum += 10; else System.out.println("抱歉,答案错误"); break; case 3: System.out.println(i + ": " + a + "+" + b + "="); int c3 = in.nextInt(); output.println(a + "+" + b + "=" + c3); if (c3 == a + b) System.out.println("恭喜答案正确"); sum += 10; else System.out.println("抱歉,答案错误"); break; case 4: while (a < b) a = (int) Math.round(Math.random() * 100); System.out.println(i + ": " + a + "-" + b + "="); int c4 = in.nextInt(); output.println(a + "-" + b + "=" + c4); if (c4 == a - b) System.out.println("恭喜答案正确"); sum += 10; else System.out.println("抱歉,答案错误"); break; System.out.println("成绩" + sum); output.println("成绩" + sum); output.close();
public class math<T> private T a; private T b; public int add(int a, int b) return a + b; public int reduce(int a, int b) return a - b; public int multiplication(int a, int b) return a * b; public int division(int a, int b) if (b != 0 && a % b == 0) return a / b; else return 0;
三:实验总结:
通过这次实验,我了解了泛型类,泛型方法、通配符、泛型变量上界、泛型变量下界。通过对之前实验的总结,对之前的知识进行进一步的回顾,通过改进代码更好的了解了泛型程序设计。编程仍需多加练习,对之前的知识需要多加复习巩固。
徐思201771010132《面向对象程序设计(java)》第二周学习总结
一、理论知识部分1、标识符:标识符由字母、下划线、美元符号和数字组成,且第一个符号不能为数字。标识符可用作类名、变量名、方法名、数组名、文件名等。2、关键字就是Java语言中已经被赋予特定意义的一些单词。常见... 查看详情
徐思201771010132《面向对象程序设计(java)》第七周学习总结(代码片段)
一、理论知识部分访问修饰符:private:只有该类可以访问 protected:该类及其子类的成员可以访问,同一个包中的类也可访问 public:该类或非该类均可访问 默认:相同包中的类可以访问 ... 查看详情
徐思201771010132《面向对象程序设计(java)》第九周学习总结(代码片段)
一、理论知识部分异常:在程序的执行过程中所发生的异常事件,它中断指令的正常执行。Java的异常处理机制可以控制程序从错误产生的位置转移到能够进行错误处理的位置。程序中出现的常见的错误和问题有:用户输入错误;设... 查看详情
徐思201771010132《面向对象程序设计(java)》第六周学习总结(代码片段)
一、理论知识部分继承:用已有类来构建新类的一种机制。当定义了一个新类继承了一个类时,这个新类就继承了这个类的方法和域,同时在新类中添加新的方法和域以适应新的情况。继承的特点:具有层次结构;子类继承了父... 查看详情
徐思201771010132《面向对象程序设计(java)》课程学习总结(代码片段)
1、实验目的与要求(1)综合掌握java基本程序结构;(2)综合掌握java面向对象程序设计特点;(3)综合掌握javaGUI程序设计结构;(4)综合掌握java多线程编程模型;(5)综合编程练习。2、实验内容和步骤任务1:填写课程课后调查问卷,网... 查看详情
杨玲徐思《面向对象程序设计(java)》第十一周学习总结(代码片段)
《面向对象程序设计(java)》第十一周学习总结第一部分:理论知识学习部分1、一般将数据结构分为两大类:线性数据结构和非线性数据结构。线性数据结构:线性表、栈、队列、串、数组和文件。非线性数据结构:树和图。2... 查看详情
08-面向对象----j
一面向对象的程序设计的由来请参考:http://www.cnblogs.com/linhaifeng/articles/6428835.html二什么是面向对象的程序设计及为什么要有它面向过程的程序设计的核心是过程,过程即解决问题的步骤,面向过程的设计就好比精心设计好一条... 查看详情
2020面向对象设计与构造第三单元博客总结(代码片段)
面向对象设计与构造第三单元总结一、JML规格化设计JML,全称TheJavaModelingLanguage,是用于对Java程序进行规格化描述的注释性质语言。笔者在本文总结了常见的JML语法描述。1.注释结构在注释行或注释块中,以@开头的行被认作JML注... 查看详情
面向对象
老王和隔壁的美女猜数字,一共有四次机会,猜到了就有特殊奖励publicclasstest{publicstaticvoidmain(String[]args){inti=(int)(Math.random()*10);Scannerinput=newScanner(System.in);for(intj=0;j<4;j++){System.out.println("老王第"+(j+1)+"次 查看详情
201771010126王燕《面向对象程序设计(java)》第二周学习总结
201771010126王燕《面向对象程序设计(java)》第二周学习总结一.理论知识学习部分3.1j简单的java应用程序标识符由字母、下划线、美元符号和数字组成,且第一个符号不能为数字。标识符可用作:类名、变量名、方法名、数组名... 查看详情
201823072019-2020-1《数据结构与面向对象程序设计》实验1报告
课程:《程序设计与数据结构》班级:1823姓名:王美皓学号:20182322实验教师:王美皓实验日期:2019年9月9日必修/选修:必修1.实验内容基于命令行和IDE(IntelljIDEA简易教程](http://www.cnblogs.com/rocedu/p/4421202.html)进行简单的Java程... 查看详情
杨其菊201771010134《面向对象程序设计(java)》第三周学习总结(代码片段)
《面向对象程序设计(Java)》第三周学习总结第一部分:理论知识 这周课程没有新进度,由于感觉对基础语法的不熟悉,复习了一遍前三章的细碎知识,学到一些之前不知道的原理: 1.计算机高级语言按程序的... 查看详情
c++面向对象的主要体现是啥?
...得对我有用的话,我会再追加分。)C++面向对象是表现在程序设计的过程上,它是突破了C的结构化设计而出现的完全以实际问题为入手点的。C++的面向对象3个特性:1.封装。2.继承。3.多态。尤其重要的是,它采用类的设计,杜... 查看详情
201771010110孔维滢《面向对象程序设计(java)》第十二周学习总结
理论知识部分1.Java的抽象窗口工具箱(AbstractWindowToolkit,AWT)包含在java.awt包中,它提供了许多用来设计GUI的组件类和容器类。2.Swing用户界面库是非基于对等体的GUI工具箱。Swing类库被放在javax.swing包里。3.大部分AWT组件都有其Swing... 查看详情
马凯军201771010116《面向对象与程序设计java》第十二周学习总结(代码片段)
一、理论与知识学习部分Java的抽象窗口工具箱(AbstractWindowToolkit,AWT)包含在java.awt包中,它提供了许多用来设计GUI的组件类和容器类。大部分AWT组件都有其Swing的等价组件,Swing组件的名字一般是在AWT组件名前面添加一个字母“J... 查看详情
201771010134杨其菊《面向对象程序设计java》第十二周学习总结(代码片段)
...bsp; 内容概要: AWT与Swing简介;框架的创建;图形程序设计; 显示图像;1.AWT组件:2.Swing组件层次关系3.AWT与Swing的关系:大部分AWT组件都有其Swing的等价组件; Swing组件的名字一般是在AWT组件名前面添加一个字母“J 查看详情
面向对象程序设计介绍以及面向对象的基本特征
面向对象的程序设计(ObjectOrientedProgramming,OOP)方法是目前比较流行的程序设计方法,和面向过程的程序设计比,它更符合人类的自然思维方式。在面向过程程序设计中,程序=数据+算法,数据和对数据的操作是分离的,如... 查看详情
《面向对象程序设计概述》牛咏梅
面向对象程序设计概述牛咏梅(南阳理工学院河南南阳473000)摘要:分析传统程序设计方法与面向对象程序设计方法之间的差别,重点分析了面向对象程序设计方法的特点,介绍了面向对象程序设计方法的步骤及其优点。关键词:面向对... 查看详情