Java基础知识图解
1 为什么要有泛型
2 在集合中使用泛型
3 自定义泛型结构
自定义泛型结构
自定义泛型结构:泛型类、泛型接口
自定义泛型结构:泛型类
自定义泛型结构:泛型方法
4 泛型在继承上的体现
5 通配符的使用
通配符的使用:注意点
通配符的使用:有限制的通配符
练习题:为什么编译如下的操作会报错?
6 泛型应用举例
泛型应用举例:泛型嵌套
泛型应用举例:实际案例
interface Info{ // 只有此接口的子类才是表示人的信息
}
class Contact implements Info{ // 表示联系方式
private String address ; // 联系地址
private String telephone ; // 联系方式
private String zipcode ; // 邮政编码
public Contact(String address,String telephone,String zipcode){
this.address = address;
this.telephone = telephone;
this.zipcode = zipcode;
}
public void setAddress(String address){
this.address = address ;
}
public void setTelephone(String telephone){
this.telephone = telephone ;
}
public void setZipcode(String zipcode){
this.zipcode = zipcode;
}
public String getAddress(){
return this.address ;
}
public String getTelephone(){
return this.telephone ;
}
public String getZipcode(){
return this.zipcode;
}
@Override
public String toString() {
return "Contact [address=" + address + ", telephone=" + telephone
+ ", zipcode=" + zipcode + "]";
}
}
class Introduction implements Info{
private String name ; // 姓名
private String sex ; // 性别
private int age ; // 年龄
public Introduction(String name,String sex,int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public void setName(String name){
this.name = name ;
}
public void setSex(String sex){
this.sex = sex ;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getSex(){
return this.sex ;
}
public int getAge(){
return this.age ;
}
@Override
public String toString() {
return "Introduction [name=" + name + ", sex=" + sex + ", age=" + age
+ "]";
}
}
class Person<T extends Info>{
private T info ;
public Person(T info){ // 通过构造器设置信息属性内容
this.info = info;
}
public void setInfo(T info){
this.info = info ;
}
public T getInfo(){
return info ;
}
@Override
public String toString() {
return "Person [info=" + info + "]";
}
}
public class GenericPerson{
public static void main(String args[]){
Person<Contact> per = null ; // 声明Person对象
per = new Person<Contact>(new Contact("北京市","01088888888","102206")) ;
System.out.println(per);
Person<Introduction> per2 = null ; // 声明Person对象
per2 = new Person<Introduction>(new Introduction("李雷","男",24));
System.out.println(per2) ;
}
}
实验1:集合中使用泛型
定义一个 Employee 类,
该类包含:private 成员变量 name,age,birthday,其中 birthday 为 MyDate 类的 对象;
并为每一个属性定义 getter, setter 方法;
并重写 toString 方法输出 name, age, birthday
MyDate 类包含:
private 成员变量 month,day,year;并为每一个属性定义 getter, setter 方法;
创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(TreeSet 需使用泛型来 定义),
分别按以下两种方式对集合中的元素进行排序,并遍历输出:
1). 使 Employee 继承 Comparable 接口,并按 name 排序
2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序
实验2:自定义泛型类的使用
定义个泛型类 DAO,在其中定义一个 Map 成员变量,Map 的键 为 String 类型,值为 T 类型。
分别创建以下方法:
public void save(String id,T entity): 保存 T 类型的对象到 Map 成员 变量中
public T get(String id):从 map 中获取 id 对应的对象
public void update(String id,T entity):替换 map 中 key 为 id 的内容, 改为 entity 对象
public List list():返回 map 中存放的所有 T 对象
public void delete(String id):删除指定 id 对象
定义一个 User 类:
该类包含:private 成员变量(int 类型) id,age;(String 类型)name。
定义一个测试类:
创建 DAO 类的对象, 分别调用其 save、get、update、list、delete 方法来操作 User 对象,
使用 Junit 单元测试类进行测试。
体会泛型中通配符的使用
章节练习题及面试题
开发一个泛型Apple类,要求有一个重量属性weight在测试类中实例化不同的泛型对象,要求对象a1的这一属性是String类型,对象a2的这一属性是Integer型,a3的这一属性是Double型。分别为a1,a2,a3的重量属性赋值为:”500克”,500,500.0,在测试类中通过对象调用访问器得到属性值并输出。另外思考,为什么a2和a3的属性需要是Integer和Double而不是int和double?
封装一个新闻类News,包含新闻标题,新闻作者,新闻内容,新闻类型三个属性,提供必要的访问器和修改器方法,重写toString方法,要求打印对象时输出格式为“标题;类型;作者”,要求只要新闻标题相同就判断为同一条新闻。在测试类中创建一个只能容纳该类对象的ArrayList集合,添加三条新闻。遍历集合,打印新闻标题,将新闻标题截取字符串到10个汉字的长度。
按要求完成下列任务
- 使用HashMap类实例化一个Map类型的对象m1,键(String类型)和值(int型)分别用于存储员工的姓名和工资,存入数据如下: 张三——800元;李四——1500元;王五——3000元;
- 将张三的工资更改为2600元
- 为所有员工工资加薪100元;
- 遍历集合中所有的员工
- 遍历集合中所有的工资