Object类
说明
类 Object 是类层次结构的根类,所有的类都继承于Object类,所有对象(包括数组)都实现这个类的方法。
方法摘要:
- equals(Object obj)
指示其他某个对象是否与此对象“相等”,返回一个boolean值。 - hashCode()
返回该对象的哈希码值 - toString()
返回该对象的字符串表示。
代码示例
创建Student类
public class Student {
private String name;
private int stuNum;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
@Override
public boolean equals(Object obj) {//重写了equals方法
if(obj!=null&&obj instanceof Student){
return ((Student)obj).stuNum==this.stuNum;
}
return false;
}
@Override
public String toString() {//重写了toStrin方法
return this.name;
}
}
创建Test类
public class Test {
public static void main(String[] args) {
Student zhangsan = new Student();
zhangsan.setName("张三");
zhangsan.setStuNum(1);
Student lisi = new Student();
lisi.setName("李四");
lisi.setStuNum(2);
System.out.println(zhangsan.equals(lisi));//比较两个不同的对象,返回false
System.out.println(zhangsan);//返回该对象的字符串表示
Student wangwu = new Student();
wangwu.setName("王五");
wangwu.setStuNum(1);
System.out.println(zhangsan.equals(wangwu));//重写了equals方法
System.out.println(wangwu);//重写了toStrin方法
}
}
运行结果:
JAVA的异常处理机制
异常类型
1. Error为系统异常,不是程序所能控制的。如JVM出现异常。
2. 编译Exception,需要在编写代码的时候处理。通常用try/catch语句处理。
3. RuntimeException,运行异常,能通过编译,但运行结果会异常。
捕获异常
使用try/catch块
public void 方法名(){
try{
//代码块(此处会产生异常)
}
catch(异常类型 e){
//对异常进行处理的代码块
}
代码块 //继续执行后边的代码
}
使用多重catch块
public void 方法名(){
try{
//代码块(此处会产生异常)
}
catch(异常类型1 e){//对异常进行处理的代码块}
catch(异常类型2 e){//对异常进行处理的代码块}
catch(异常类型3 e){//对异常进行处理的代码块}
代码块 //继续执行后边的代码
}
注意:一段代可能引发多种类型的异常,当异常发生时,会按照顺序查看每个catch语句,并执行第一个与异常类型匹配的catch语句,执行该catch语句后,其后的catch语句将会被跳过。
使用使用try/catch/finally块
public void 方法名(){
try{
//代码块(此处会产生异常)
}
catch(异常类型 e){
//对异常进行处理的代码块
}
finall{
代码块 //一定会执行的代码
}
}
声明异常
throws关键字
使用throws关键字声明方法可能要抛出的各种异常,throws只能出现在方法头。
如:
public void 方法名() throws 异常类型
代码例子
创建Student类
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Student {//throws关键字的用法
public void read() throws FileNotFoundException,IOException{
FileInputStream is = new FileInputStream("c:\\UniqloVedio.rmvb");
is.read();
}
}
创建测试类
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
Student zhangsan = new Student();
try {
zhangsan.read();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
抛出异常
throw关键字
throw关键字用于自定义异常,然后手动抛出。throw关键字只能出现在方法体内。
如:
public void 方法名(){
throw new 自定义异常名
}
代码例子
创建Stude类
import lz20150722.d3.ForbiddenException;
public class Student {
public void see() throws ForbiddenException{
throw new ForbiddenException("禁止看羞羞视频");//throw关键字
}
}
创建自定义异常类
public class ForbiddenException extends Exception{
public ForbiddenException(String message){
//super("禁止看羞羞视频!");第一种写法(无参)
super(message);//第二种写法(有参)
}
}
创建Test类
public class Test {
public static void main(String[] args) {
Student zhangsan = new Student();
try {
zhangsan.see();
} catch (ForbiddenException e) {
e.printStackTrace();
}
}
}
运行结果: