目录
一、学习内容
- 异常
- 泛型
- IO
- Java8 (粗略过了一遍)
具体要点:
- try-catch-finally 和 throws(默认)
- 注意泛型的语法 ;泛型类、泛型接口、泛型方法( 泛型创建对象时,必须说明其类型,不指定默认为Object)
- InputStream 和 OutputStream
二、项目展示
2.1 项目-1
知识点:异常处理(try-catch、throws、throw)
2.1.1 源码
package com.weiwei.exception_;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TryCatchExercise {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(System.in);
int num = 0;
String inputStr = "";
while(true){
System.out.println("请输入一个整数");
inputStr = scanner.next();
try {
num = Integer.parseInt(inputStr);
break;
} catch (NumberFormatException e) {
System.out.println("你输入的不是一个整数");
}
}
System.out.println("num = " + num);
// f2();
int age = 200;
if(!(age >= 18 && age <= 120)){
throw new AgeException("年龄错误");
}
}
public static void f2() throws FileNotFoundException{
FileInputStream fis = new FileInputStream("d://aa.txt");
}
}
class AgeException extends RuntimeException{
public AgeException(String message) {
super(message);
}
}
2.1.2 结果展示
2.2 项目-2
知识点:泛型、集合的操作
2.2.1 源码
User.java
package com.weiwei.customgeneric.homework_;
public class User {
private int id;
private int age;
private String name;
public User(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "\nUser{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
DAO.java
package com.weiwei.customgeneric.homework_;
import java.util.*;
public class DAO <T>{
private Map<String,T> map = new HashMap<>();
public T get(String id){
return map.get(id);
}
public void update(String id, T entity){
map.put(id, entity);
}
public List<T> list(){
List<T> list = new ArrayList<>();
Set<String> keySet = map.keySet();
for(String key : keySet){
list.add(map.get(key));
}
return list;
}
public void delete(String id){
map.remove(id);
}
public void save(String id,T entity){
map.put(id,entity);
}
}
Homework01.java
package com.weiwei.customgeneric.homework_;
import org.junit.Test;
import java.util.List;
public class Homework01 {
public static void main(String[] args) {
}
@Test
public void testList(){
DAO<User> userDAO = new DAO<>();
userDAO.save("001", new User(1,10,"jack"));
userDAO.save("002", new User(2,12,"jac"));
userDAO.save("003", new User(3,13,"ja"));
List<User> list = userDAO.list();
System.out.println("list= " + list);
userDAO.update("003", new User(3,28,"milan"));
userDAO.delete("001");
list = userDAO.list();
System.out.println("===========change=============");
System.out.println("list= " + list);
System.out.println(userDAO.get("003"));
}
}
2.2.2 结果展示