zoukankan      html  css  js  c++  java
  • 自定义异常(案例)

    1.写一个Student学生类,包含 name, id属性

    public class Student implements Comparable<Student> {
        
        private int id;
        private String name;
        
        
        public Student() {
            super();
        }
        
        public Student(int id, String name) throws Exception {
            super();
            // this.id = id;
            setId(id);
            // this.name = name;
            setName(name);
        }
        
        
        public int getId() {
            return id;
        }
        public void setId(int id) throws IdException {
            if(id>0 ) {
                this.id = id;
            }else {
            //    System.out.println("学号不合理,请重新输入......");
                throw new IdException("学号不合理!!!");
                
            }
            System.out.println("看这行代码是否执行");
            
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Student [id=" + id + ", name=" + name + "]";
        }
    
        
        
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + id;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Student other = (Student) obj;
            if (id != other.id)
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
    
        @Override     //实现了Comparable接口,则必然空实现Comparable接口中的comparableTo()方法
        
        public int compareTo(Student o) {
            // 按照学号比较大小
            // return getId()-o.getId();
            
            // 按照姓名比较大小
            return getName().compareTo(o.getName());
            
        }
        
        
        
    
    }

    2.自定义一个IdException异常类

    package com.monkey1030;
    
    public class IdException extends Exception {
        
        // 自定义无参构造方法
        public IdException() {
            
        }
        
        // 自定义有参构造方法
        public IdException(String str) {
            super(str);
        }
            
    
    }

    3.要求: 不合逻辑的id属性值构造对象,否则程序产生IdException异常

    package com.monkey1030;
    
    public class Test {
    
        public static void main(String[] args) {
            Student s = null;
            
            try {
                  s = new Student(-1001,"卡纳瓦罗");     // 此时id  传入的是 -1001,不合理值
            } catch (Exception e) {
                e.printStackTrace();
            }
                System.out.println(s);
        }
    
    }

      当  new Student(-1001,"卡纳瓦罗"); // 此时id 传入的是 -1001,不合理值

    结果:

    com.monkey1030.IdException: 学号不合理!!!
        at com.monkey1030.Student.setId(Student.java:30)
        at com.monkey1030.Student.<init>(Student.java:16)
        at com.monkey1030.Test.main(Test.java:9)
    null

      当  new Student(1001,"卡纳瓦罗"); // 此时id 传入的是 1001,合理值

    结果:

    看这行代码是否执行
    Student [id=1001, name=卡纳瓦罗]

    了解一下 compareTo()方法的使用:


  • 相关阅读:
    HDU 3572 Task Schedule(拆点+最大流dinic)
    POJ 1236 Network of Schools(Tarjan缩点)
    HDU 3605 Escape(状压+最大流)
    HDU 1166 敌兵布阵(分块)
    Leetcode 223 Rectangle Area
    Leetcode 219 Contains Duplicate II STL
    Leetcode 36 Valid Sudoku
    Leetcode 88 Merge Sorted Array STL
    Leetcode 160 Intersection of Two Linked Lists 单向链表
    Leetcode 111 Minimum Depth of Binary Tree 二叉树
  • 原文地址:https://www.cnblogs.com/penguin1024/p/11804990.html
Copyright © 2011-2022 走看看