8.16 学习成果

目录

一、学习内容

二、项目展示


一、学习内容

  • 异常
  • 泛型
  • 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 结果展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值