201871010135 张玉晶《面向对象程序设计(java)》第十一周学习总结
|
项目 |
内容 |
|
《面向对象程序设计(java)》 |
https://www.cnblogs.com/nwnu-daizh/ |
|
这个作业的要求在哪里 |
https://www.cnblogs.com/nwnu-daizh/p/11815810.html |
|
作业学习目标 |
|
1:总结第八章关于泛型程序设计理论知识
1. 泛型类的定义
a. 一个泛型类就是具有一个或多个类型变量的类,即创建用类型作为参数的类。
b. 泛型:也称参数化类型,就是定义类,接口和方法时,通过类型参数指示将要处理的对象类型。
2. 定义简单的泛型类:
a. public class Pair<T> Pair类引入了一个类型变量T,用<>括起来,并放在类名的后面。
b. 泛型类可以有多个类型变量 public class Pair<T,U>
c. 类的类型变量用于指定方法的返回类型以及域,局部变量的类型。
3. 泛型方法的声明:
a. 除了泛型类外,还可以只单独定义一个方法作为泛型方法,用于指定方法参数或者返回值为泛型类型,留待方法调用时确定。
b. 泛型方法可以声明在泛型类中,也可以在普通类中
class ArrayAlg
{
public static <T> getMiddle (T...a) {...}
}
4. 泛型接口的定义:
a. 定义: public interface IPool <T> {...}
b. 实现: publoc class GenericPool <T> implements IPool <T> {...}
5. 泛型变量的限定
a. 定义泛型变量的上界 : public class NumberGeneric <T extends Number>
• 上述声明规定了NumberGeneric类所能处理的泛型变量类型需和Number有继承关系;
• extends 关键字所声明的上界既可以是一个类,也可以是一个接口;
• T 应该是绑定类型的子类型;
• 一个类型变量或通配符可以有多个限定,限定类型用" &" 分割。 <T extends Comparable & Serializable>
b. 定义泛型变量的下界 : public static <T > void sort (T[] a, Comparator <? super T> c) {...}
• 通过super关键字可以固定泛型参数 T 的类型为某种类型的 超类;
• 当希望为一个方法的参数限定类型时,通常可以使用下限通配符
6. 通配符的类型
通配符 " ? " 表示参数类型可以是任何一种类型。
用法: a. 单独的 " ?" ,用于表示任何类型;
b. ? ertends type, 表示带有上界;
c. ? super type,表示带有下界。
7. 泛型程序设计小结:
a. 定义一个泛型类时,在"<>" 定义形式类型参数 class Test < K , V> , K,V 表示类型
b. 实例化泛型对象的时候, 一定要在类名后面指定类型参数的值(类型), 一共要有两次书写, Test <String , String> t = new Test <String, String>();
c. 泛型中 <T extends Object> , extends并不代表继承,它是类型范围的限制;
d. 泛型类不是协变的。
2、实验内容和步骤
实验1: 导入第8章示例程序,测试程序并进行代码注释。
测试程序1:
package Pair;
public class Pair<T> //泛型类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 Pair;
public class PairTest1
{
public static void main(String[] args)
{
String[] words = { "Mary", "had", "a", "little", "lamb" };
Pair<String> mm = ArrayAlg.minmax(words); //使用泛型类Pair<String>定义泛型变量
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 values, or null if a is null or empty
*/
public static Pair<String> minmax(String[] a) //定义一个泛型方法
{
if (a == null || a.length == ) return null;
String min = a[];
String max = a[];
for (int i = ; i < a.length; i++)
{
if (min.compareTo(a[i]) > ) min = a[i];
if (max.compareTo(a[i]) < ) max = a[i];
}
return new Pair<>(min, max);//返回Pair<String>类对象
}
}
运行结果如下:

测试程序2:
package pair2; import java.time.*; import Pair.Pair; /**
* @version 1.02 2015-06-21
* @author Cay Horstmann
*/
public class PairTest2
{
public static void main(String[] args)
{
LocalDate[] birthdays =
{
LocalDate.of(, , ), // G. Hopper
LocalDate.of(, , ), // A. Lovelace
LocalDate.of(, , ), // J. von Neumann
LocalDate.of(, , ), // K. Zuse
};
Pair<LocalDate> mm = ArrayAlg.minmax(birthdays); //使用泛型类Pair<String>定义泛型变量
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 values, or null if a is null or empty
*/
public static <T extends Comparable> Pair<T> minmax(T[] a) //对泛型参数T设置了一个上界
{
if (a == null || a.length == ) return null;
T min = a[];
T max = a[];
for (int i = ; i < a.length; i++)
{
if (min.compareTo(a[i]) > ) min = a[i];
if (max.compareTo(a[i]) < ) max = a[i];
}
return new Pair<>(min, max); //返回Pair<>类对象
}
}
运行结果如下:

测试程序3:
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 / ;
salary += raise;
}
}
package pair3; public class Manager extends Employee
{
private double bonus;
public Manager(String name, double salary, int year, int month, int day)
{
super(name, salary, year, month, day);
bonus = ;
} public double getSalary()
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
} public void setBonus(double b)
{
bonus = b;
} public double getBonus()
{
return bonus;
}
}
package pair3;
import Pair.Pair;
public class PairTest3
{
public static void main(String[] args)
{
//创建Manager类对象
Manager ceo = new Manager("Gus Greedy", , , , );
Manager cfo = new Manager("Sid Sneaky", , , , );
Pair<Manager> buddies = new Pair<Manager>(ceo, cfo); // 创建泛型类对象
printBuddies(buddies); ceo.setBonus();
cfo.setBonus();
Manager[] managers = { ceo, cfo }; 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) //表示任何泛型Pair类型,它的参数类型是Employee子类。 ?表示通配符类型
{
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) //<? super Manager> 限制为Manager的所有超类型
{
if (a.length == ) return;
Manager min = a[];
Manager max = a[];
for (int i = ; 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)//使用无限定通配符用来测试一个Pair是否包含一个null引用
{
return p.getFirst() == null || p.getSecond() == null;
} public static void swap(Pair<?> p) { swapHelper(p); } // swap调用swapHelper方法 public static <T> void swapHelper(Pair<T> p) //swapHelper是一个泛型方法
{
T t = p.getFirst();
p.setFirst(p.getSecond());
p.setSecond(t);
}
}
运行结果如下:

实验2:结对编程练习,将程序提交到PTA(2019面向对象程序设计基础知识测试题(2))
(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 |
package tuxing;
import java.util.ArrayList;
import java.util.Scanner; interface 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
{ ArrayList list = new ArrayList();
public String toString()
{
return list.toString();
}
@Override
public Object push(Object item) {
if (list.add(item)){
return item;
}else {
return false;
}
} @Override
public Object pop() {
if (list.size()==){
return null;}
return list.remove(list.size()-);
} @Override
public Object peek() {
if(list.size()!=)
return list.get(list.size()-);
return null;
} @Override
public boolean empty() {
if(list.size()==)
return true;
return false;
} @Override
public int size() {
// TODO Auto-generated method stub
return list.size();
} } class Car
{
private int id;
private String name; @Override
public String toString() {
return "Car [" + "id=" + id +", name=" + getName() +']';
} 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.setName(name);
}
}
public class Main { public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(true){
String c = in.nextLine();
if(c.equals("Integer"))
{
System.out.println("Integer Test");
int m=in.nextInt();
int n=in.nextInt();
ArrayListGeneralStack array = new ArrayListGeneralStack();
for(int i=;i<m;i++)
{
System.out.println("push:"+array.push(in.nextInt()));
}
for(int i=;i<n;i++)
{
System.out.println("pop:"+array.pop());
}
System.out.println(array.toString());
int sum=;
int size=array.size();
for(int i=;i<size;i++)
{
sum+=(int)array.pop();
}
System.out.println("sum="+sum);
System.out.println("interface GeneralStack");
}
else if(c.equals("Double"))
{
System.out.println("Double Test");
int m = in.nextInt();
int n = in.nextInt();
ArrayListGeneralStack array = new ArrayListGeneralStack();
for (int i=;i<m;i++)
{
System.out.println("push:"+array.push(in.nextDouble()));
}
for(int i=;i<n;i++)
{
System.out.println("pop:"+array.pop());
}
System.out.println(array.toString());
double sum=;
int size=array.size();
for(int i =;i<size;i++)
{
sum+=(double)array.pop();
}
System.out.println("sum="+sum);
System.out.println("interface GeneralStack");
}
else if(c.equals("Car"))
{
System.out.println("Cat Test");
int m=in.nextInt();
int n=in.nextInt();
ArrayListGeneralStack array = new ArrayListGeneralStack();
for(int i=;i<m;i++)
{
int id = in.nextInt();
String name = in.next();
Car car = new Car(id, name);
System.out.println("push"+array.push(car));
}
for(int i =;i<n;i++)
{
System.out.println("pop"+array.pop());
}
System.out.println(array.toString());
int size=array.size();
for(int i=;i<size;i++)
{
Car car=(Car) array.pop();
System.out.println(car.getName());
} System.out.println("interface GeneralStack");
}
else if (c.equals("quit")){
System.exit();;
}}
}
}
结果:

实验总结: 在本节中我知道了泛型类的定义,泛型方法的声明,泛型接口的定义,类型变量的限定,包括上界,下界,还有通配符类型。收获还是可以的,就是编程的时候遇到了一些困难,然后通过上网搜查和问同学,有的问题解决了,但有的问题还是懵懵懂懂。就是在PTA上交不上去,出现了一些未经检查或不安全的操作。就这个问题弄了好长的时间。
201871010135 张玉晶《面向对象程序设计(java)》第十一周学习总结的更多相关文章
- 201871010132——张潇潇《面向对象程序设计JAVA》第二周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 201771010134杨其菊《面向对象程序设计java》第九周学习总结
第九周学习总结 第一部分:理论知识 异常.断言和调试.日志 1.捕获 ...
- 201871010132-张潇潇《面向对象程序设计(java)》第一周学习总结
面向对象程序设计(Java) 博文正文开头 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cn ...
- 扎西平措 201571030332《面向对象程序设计 Java 》第一周学习总结
<面向对象程序设计(java)>第一周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 ...
- 杨其菊201771010134《面向对象程序设计Java》第二周学习总结
第三章 Java基本程序设计结构 第一部分:(理论知识部分) 本章主要学习:基本内容:数据类型:变量:运算符:类型转换,字符串,输入输出,控制流程,大数值以及数组. 1.基本概念: 1)标识符:由字母 ...
- 201871010124 王生涛《面向对象程序设计JAVA》第一周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://edu.cnblogs.com/campus/xbsf/ ...
- 201871010115——马北《面向对象程序设计JAVA》第二周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 201777010217-金云馨《面向对象程序设计(Java)》第二周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 201771010123汪慧和《面向对象程序设计Java》第二周学习总结
一.理论知识部分 1.标识符由字母.下划线.美元符号和数字组成, 且第一个符号不能为数字.标识符可用作: 类名.变量名.方法名.数组名.文件名等.第二部分:理论知识学习部分 2.关键字就是Java语言 ...
- 201871010135 张玉晶《面向对象程序设计(java)》第七周学习总结
201871010135 张玉晶<面向对象程序设计(java)>第七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
随机推荐
- python 连接oracle -- sqlalchemy及cx_Oracle的使用详解
python连接oracle -- sqlalchemy import cx_Oracle as orcl import pandas as pd from sqlalchemy import cre ...
- Python 协程 (Coroutine)
协程 (Coroutine) 什么是协程 协程(微线程)是比线程更轻量化的存在,像一个进程可以拥有多个线程一样,一个线程也可以拥有多个协程 最重要的是,协程不是被操作系统内核所管理,而完全是由程序所控 ...
- C++面向对象程序设计学习笔记(6)
多态性 编译时的多态性与运行时的多态性 在面向对象方法中,所谓多态性就是不同对象收到相同信息时,产生不同的行为.在c++程序设计中,即"一个接口,多种方法" 在C++中,多态性的实 ...
- json数据转换成结构体
package main import ( "encoding/json" "fmt" ) type IT1 struct { Company string ` ...
- 记录错误or日记(更新中)
前言: 从2018.8-17开始记录 本篇随笔记录做题时的小错误(大多数),考试总结(懒得总结了),做过的每个题的错误 2019.12.7 傻逼学校,给我三个小时假期给你们做题挣工资 2019.11. ...
- 物联网架构成长之路(37)-基于C#开发串口工具
0. 前言 作为物联网平台开发,最基础的工具链还是要有的.前几篇博客,介绍了用C#开发一个MQTT的客户端,用于模拟设备连接平台,并发送数据到平台.但是对于一些硬件来说,可能会用到串口uart来发送数 ...
- LeetCode 752:打开转盘锁 Open the Lock
题目: 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋转:例如把 ' ...
- javascript 写一个 map方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HTML+CSS基础 权重的计算 权重计算规则
权重的计算 将选择器上面的选择器进行叠加,叠加后的总和就是该选择器的权重. 权重计算规则
- css不常用的4个选择器-个人向
①:element1.element2(给同时满足有element1和element2 2个类名的元素添加样式) <!DOCTYPE html> <html> <head ...