201871010119-帖佼佼《面向对象程序设计(java)》第十一周学习总结
博文正文开头格式:(2分)
项目 |
内容 |
这个作业属于哪个课程 |
https://www.cnblogs.com/nwnu-daizh/ |
这个作业的要求在哪里 |
https://www.cnblogs.com/nwnu-daizh/p/11435127.html |
作业学习目标 |
|
第一部分:总结第八章关于泛型程序设计理论知识(25分)
1.泛型程序设计:编写的代码可以被很多不同类型的对象所重用。
1)泛型(参数化类型):在定义类、接口和方法时,通过类型参数指示将要处理的对象类型。如ArrayList类。
2)泛型类:是具有一个或多个类型变量的类,即创建用类型作为参数的类。在<>内定义形式类型参数,该形参表示类型而不表示值。
泛型类可以有多个类型变量,例如:public class Pair<T, U>{ 。。。。。。 }
类型参数的优点:改善程序的可读性,增强类型使用安全性;
类定的类型变量用于指定方法的返回类型以及域、局部变量的类型。
2、泛型方法的声明:
1)泛型方法:除了泛型类外,还可以只单独定义一个方法作为泛型方法,用于指定方法参数或者返回值为泛型类型,在方法调用时确定具体需要。(泛型方法可以声明在泛型类中,也可以声明在普通类中)
2)泛型变量的限定:a.泛型变量的上界:如<T extends Number>(extends关键字所声明的上界既可以是一个类,也可以是一个接口,extends并不代表继承,它是类型范围限制。)
b.<T extends Bounding Type>表示T应该是绑定类型的子类型。
c .一个类型变量和通配符可以有多个限定,限定类型用“&”分割。eg:<T entends Compareable & Seriaizable>
3)泛型变量下界的说明:
通过使用super关键字可以固定泛型参数的类型为某种类型或者其超类;
当程序希望为一个方法的参数限定类型时,通常可以使用下限通配符
3.通配符类型:
1)通配符:“?”符号表明参数的类型可以是任何一种类型,而T表示一种未知类型。
2)通配符的一般用法:a.?:用于表示任何类型;
b.? extends type,表示带有上界;
c.? super type,表示带有下界。
3)无界通配符(*): Pair< ? >
4.泛型类的约束与局限性:
1)不能用基本类型(如int型,float型等)实例化类型参数
2)运行时类型查询只适用于原始类型
3)不能抛出也不能捕获泛型类实例
4)参数化类型的数组不合法
5)不能实例化类型变量
6)泛型类的静态上下文中类型变量无效
7)注意擦除后的冲突
5.泛型类型的继承规则:Java中的数组是协变的,但这一原理不适用于泛型类型。不允许这样做是因为,避免破坏要提供类型的安全泛型。
泛型类可扩展或实现其它的泛型类。
第二部分:实验部分
实验1:测试程序1(6分)
l 编辑、调试、运行教材311、312页代码,结合程序运行结果理解程序;
l 在泛型类定义及使用代码处添加注释;
掌握泛型类的定义及使用。
实验代码如下:
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); //words是通过类名调用,依赖于ArrayAlg类 System.out.println("min = " + mm.getFirst()); //利用封装技术分别显示min和max System.out.println("max = " + mm.getSecond()); } } class ArrayAlg //定义一个泛型类,与Pair<T>是依赖关系 { /** * Gets the minimum and maximum of an array of strings. * @param a an array of strings * @return a pair with the min and max values, 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]; //利用compareTo方法 if (max.compareTo(a[i]) < 0) max = a[i]; } return new Pair<>(min, max); //返回新的泛型类 } }
package pair1; /** * @version 1.00 2004-05-10 * @author Cay Horstmann */ //T作为泛型类的标志 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; } }
运行结果如下:
实验1:测试程序2(6分)
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; } //setFirst()方法声明 public void setSecond(T newValue) { second = newValue; } //setSecond()方法声明 }
package pair2; import java.time.*; /** * @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 }; //创建一个birthdays数组 Pair<LocalDate> mm = ArrayAlg.minmax(birthdays); //通过类名来调用 System.out.println("min = " + mm.getFirst()); //利用封装技术,分别显示min和max 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 values, or null if a is null or empty */ public static <T extends Comparable> Pair<T> minmax(T[] a) //将T限制为实现了Comparable接口(只含有一个compareTo方法的标准接口)的类。 { 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]; //compareTo方法的使用 if (max.compareTo(a[i]) < 0) max = a[i]; } return new Pair<>(min, max); //返回泛型类 。 } }
运行结果如下:
实验1:测试程序1(6分)
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; /** * @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类对象 Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15); Pair<Manager> buddies = new Pair<Manager>(ceo, cfo); printBuddies(buddies); ceo.setBonus(1000000); cfo.setBonus(500000); Manager[] managers = { ceo, cfo }; //创建一个manager[]数组 Pair<Employee> result = new Pair<Employee>(); //创建一个泛型类对象 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) //hasNulls()方法 { return p.getFirst() == null || p.getSecond() == null; } public static void swap(Pair<?> p) { swapHelper(p); } //swap()方法 public static <T> void swapHelper(Pair<T> p) { T t = p.getFirst(); p.setFirst(p.getSecond()); p.setSecond(t); } }
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 //继承父类Employee类 { private double bonus; //定义一个私有双浮点型变量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) //manager构造器 { super(name, salary, year, month, day); bonus = 0; } public double getSalary() //getSalary()方法声明 { double baseSalary = super.getSalary(); return baseSalary + bonus; } public void setBonus(double b) { bonus = b; } public double getBonus() { return bonus; } }
运行结果如下:
实验2:结对编程练习(32分)
(1)编写一个泛型接口GeneralStack,要求类中方法对任何引用类型数据都适用。GeneralStack接口中方法如下:
push(item); //如item为null,则不入栈直接返回null。
pop(); //出栈,如为栈为空,则返回null。
peek(); //获得栈顶元素,如为空,则返回null.
public boolean empty();//如为空返回true
public int size(); //返回栈中元素数量
(2)定义GeneralStack的子类ArrayListGeneralStack,要求:
ü 类内使用ArrayList对象存储堆栈数据,名为list;
ü 方法: public String toString()//代码为return list.toString();
ü 代码中不要出现类型不安全的强制转换。
(3)定义Car类,类的属性有:
private int id;
private String name;
方法:Eclipse自动生成setter/getter,toString方法。
(4)main方法要求
ü 输入选项,有quit, Integer, Double, Car 4个选项。如果输入quit,程序直接退出。否则,输入整数m与n。m代表入栈个数,n代表出栈个数。然后声明栈变量stack。
ü 输入Integer,打印Integer Test。建立可以存放Integer类型的ArrayListGeneralStack。入栈m次,出栈n次。打印栈的toString方法。最后将栈中剩余元素出栈并累加输出。
ü 输入Double ,打印Double Test。剩下的与输入Integer一样。
ü 输入Car,打印Car Test。其他操作与Integer、Double基本一样。只不过最后将栈中元素出栈,并将其name依次输出。
特别注意:如果栈为空,继续出栈,返回null
输入样例
Integer
5
2
1 2 3 4 5
Double
5
3
1.1 2.0 4.9 5.7 7.2
Car
3
2
1 Ford
2 Cherry
3 BYD
quit
输出样例
Integer Test
push:1
push:2
push:3
push:4
push:5
pop:5
pop:4
[1, 2, 3]
sum=6
interface GeneralStack
Double Test
push:1.1
push:2.0
push:4.9
push:5.7
push:7.2
pop:7.2
pop:5.7
pop:4.9
[1.1, 2.0]
sum=3.1
interface GeneralStack
Car Test
push:Car [id=1, name=Ford]
push:Car [id=2, name=Cherry]
push:Car [id=3, name=BYD]
pop:Car [id=3, name=BYD]
pop:Car [id=2, name=Cherry]
[Car [id=1, name=Ford]]
Ford
interface GeneralStack
实验代码如下:
import java.util.ArrayList; import java.util.Scanner; interface GeneralStack<T>{ //泛型类接口GeneralStack<T> public T push(T item); //如果item为null,则不入栈直接返回null。 public T pop(); //出栈,如为栈为空,则返回null。 public T peek(); //获得栈顶元素,如为空,则返回null. public boolean empty(); //如为空返回true public int size(); //返回栈中元素数量 } class ArrayListGeneralStack implements GeneralStack{ //创建一个实现GeneralStack接口的类 ArrayList a =new ArrayList(); @Override //重写toString方法 public String toString() { return a.toString(); } @Override //重写压栈方法 public Object push(Object item) { if (a.add(item)){ return item; }else { return false; } } @Override //重写出栈方法 public Object pop() { if (a.size()==0){ //判断栈为空时,返回null return null; } return a.remove(a.size()-1); } @Override //重写获取栈顶元素的函数 public Object peek() { return a.get(a.size()-1); } @Override public boolean empty() { //栈为空时,直接返回boolean值 if (a.size()==0){ return true; }else { return false; } } @Override //重写得到栈中元素个数的函数 public int size() { return a.size(); } } class Car{ //定义一个Car类 private int id; //两个私有属性 private String name; @Override public String toString() { return "Car [" + "id=" + id + ", name=" + name + ']'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Car(int id, String name) { this.id = id; this.name = name; } } public class Main { public static void main(String[] args) { Scanner in =new Scanner(System.in); while (true){ String b = in .nextLine(); //输入选项,有quit, Integer, Double, Car 4个选项。 if (b.equals("Double")){ //输入Double ,打印Double Test。 System.out.println("Double Test"); int count=in.nextInt(); int pop_time=in.nextInt(); ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();//建立可以存放Double类型的ArrayListGeneralStack。 for (int i=0;i<count;i++){ //入栈次数 System.out.println("push:"+arrayListGeneralStack.push(in.nextDouble())); } for (int i=0;i<pop_time;i++){ //出栈次数 System.out.println("pop:"+arrayListGeneralStack.pop()); } System.out.println(arrayListGeneralStack.toString()); //打印栈的toString方法 double sum=0; int size=arrayListGeneralStack.size(); for (int i=0;i<size;i++){ sum+=(double)arrayListGeneralStack.pop(); //最后将栈中剩余元素出栈并累加输出。 } System.out.println("sum="+sum); System.out.println("interface GeneralStack"); }else if (b.equals("Integer")){ //输入Integer,打印Integer Test。 System.out.println("Integer Test"); int count=in.nextInt(); int pop_time=in.nextInt(); ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack();//建立可以存放Integer类型的ArrayListGeneralStack。 for (int i=0;i<count;i++){ //入栈次数 System.out.println("push:"+arrayListGeneralStack.push(in.nextInt())); } for (int i=0;i<pop_time;i++){ //出栈次数 System.out.println("pop:"+arrayListGeneralStack.pop()); } System.out.println(arrayListGeneralStack.toString()); //打印栈的toString方法。 int sum=0; int size=arrayListGeneralStack.size(); for (int i=0;i<size;i++){ sum+=(int)arrayListGeneralStack.pop(); //最后将栈中剩余元素出栈并累加输出。 } System.out.println("sum="+sum); System.out.println("interface GeneralStack"); }else if (b.equals("Car")){ //输入Car,打印Car Test。 System.out.println("Car Test"); int count=in.nextInt(); int pop_time=in.nextInt(); ArrayListGeneralStack arrayListGeneralStack = new ArrayListGeneralStack(); //创建可以存放Car类型的ArrayListGeneralStack。 for (int i=0;i<count;i++){ //入栈次数 int id=in.nextInt(); String name=in.next(); Car car = new Car(id,name); System.out.println("push:"+arrayListGeneralStack.push(car)); } for (int i=0;i<pop_time;i++){ //出栈次数 System.out.println("pop:"+arrayListGeneralStack.pop()); } System.out.println(arrayListGeneralStack.toString()); //定义toString方法 if (arrayListGeneralStack.size()>0){ //栈不为空 int size=arrayListGeneralStack.size(); for (int i=0;i<size;i++){ Car car=(Car) arrayListGeneralStack.pop();//将栈中元素出栈,并将其name依次输出。 System.out.println(car.getName()); } } System.out.println("interface GeneralStack"); }else if (b.equals("quit")){ //如果输入quit,程序直接退出。 break; } } } }
运行结果如下:
实验总结:(15分)
这一周的内容比较少,主要学习了有关泛型类的知识。在理论课的学习,听老师讲泛型类不算特别难,实验课上运行程序时,也不是很难理解程序,我觉得理论知识的掌握还是可以。上一周的编程题,经过学长的视频讲解,对程序有了一定程度的理解,但是也只是能读懂程序,在需要自己写的时候,嗯,就有点尴尬,感觉写不出来,还是有很大问题。通过看其他人的编程,自己写了一个固定数据的加减乘除运算,还考虑不周到,只能计算出给定值的运算。在之后的学习中,只能继续努力了,多理解,多练习。