Java基础知识复习-------代码块、final、abstract抽象类、代理类、接口、内部类、异常处理

 

 

 

目录

代码块

final

abstract抽象类

代理类实现

接口

内部类

异常处理


代码块

final

static final 用来修饰 属性 方法 

属性:全局常量;

自己定义方法的时候,一般不会定义final的。。想重写就重写了。

代码不行;注释可以。。注释中x本身没有变。。只是返回的 一个比x大一的数而已

代码可以  对象o没变。o里面的属性可以变。

可以考虑为存的i的地址没变。。。

abstract抽象类

代理类实现

package top.oneluckyguy.second;

public class Singleton {

	public Singleton() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		Server server = new Server();
		ProxyServer proxyServer= new ProxyServer(server);
		proxyServer.browse();
	}

}
interface NetWork{
	public void browse(); 
}
class Server implements NetWork{
	public void browse() {
		System.out.println("真实");
	}
}
class ProxyServer implements NetWork{
	private NetWork work;
	public ProxyServer(NetWork work) {
		this.work = work;
	}
	public void check() {
		System.out.println("检查工作");
	}
	public void browse() {
		check(); 
		work.browse();
	}
}

接口

接口中定义的静态方法只能自己用

 

object 是所有未指明父类的父类。。包括抽象类。

abstract不能与静态方法同时用的

是因为类内调用静态方法。但是abstract

内部类

异常处理

异常的体系结构:

各类异常:

数组角标越界

数组角标越界

类型转换异常

把字符串转成数值异常

输入不匹配异常

算数异常

上面是运行时异常下面是编译时异常

编译时异常

try catch finally语句

常见的异常处理的函数

在try的大括号里定义的num 在大括号外就不能用了。

垃圾回收机制

finally

读取本地文件并输出try catch 捕获异常。

package top.oneluckyguy.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class HelloWorld {
    public static void main(String[] args) {
        test();
    }
    public static void test(){
        FileInputStream fis = null;
        try{
            File file = new File("D:\\IDEA\\out\\production\\first\\top\\oneluckyguy\\java\\hello.txt");
            fis = new FileInputStream(file);
            int data = fis.read();
            while(data != -1){
                System.out.println((char)data);
                data = fis.read();
            }
       }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();

        }finally {
            try{
                if(fis != null){
                    fis.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

对于运行时异常不考虑try catch

在编译时异常用try catch

在开发中,由于运行时异常比较常见所以我们通常就不针对运行时异常写try catch语句了队医bian

throws抛出异常

package top.oneluckyguy.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class HelloWorld {
    public static void main(String[] args) {
        try{
            method2();
        }catch (IOException e){
            e.printStackTrace();
        }
        method3();
    }
    public static void method3(){
        try{
            method2();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public static void method2() throws IOException{
        method1();
    }
    public static void method1()throws FileNotFoundException,IOException {
        File file = new File("hello.txt");
        FileInputStream fis = new FileInputStream(file);
        int data = fis.read();
        while(data != -1){
            System.out.print((char)data);
            data = fis.read();
        }
        fis.close();
    }
}

子类可以比父类的异常更加细。

package top.oneluckyguy.java;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class HelloWorld {
    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.display(new SubClass());
    }
    public void display(SuperClass s){
        try{
            s.method();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

class SuperClass{
    public void method() throws IOException{

    }
}
class SubClass extends SuperClass{
    public void method() throws FileNotFoundException{

    }
}

手动抛出异常throw区别于throw

package top.oneluckyguy.java;

public class HelloWorld{
    public static void main(String[] args) {
        Student student = new Student();
        student.regist(-101);
        System.out.println(student);
    }
}
class Student{
    int id;
    public void regist(int id){
        if(id > 0){
            this.id = id;
        }else{
            throw new RuntimeException("输入的数据非法"); //如果抛出运行时异常,在编译的时候不会报错,运行的时候有错误才会报
        }
    }
}
package top.oneluckyguy.java;

public class HelloWorld{
    public static void main(String[] args) {
        Student student = new Student();
        try{
            student.regist(-101);        //调用的时候捕获异常
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(student);
    }
}
class Student {
    int id;
    public void regist(int id)throws Exception{      //在方法上加上抛出异常
        if(id > 0){
            this.id = id;
        }else{
            throw new RuntimeException("输入的数据非法"); //如果抛出的是总异常,在方法上加上throws抛出,然后再调用的时候try_catch捕获
        }
    }
}

自定义异常类

自定义异常类

package top.oneluckyguy.java;

/**
 * @author Liu Qingfeng
 * @create 2020-12-12----15:08
 */
public class MyException extends RuntimeException{
    static final long serialVersionUID = -7034897190745766939L;
    public MyException(){

    }
    public MyException(String msg){
        super(msg);
    }
}

调用自定义异常类:

package top.oneluckyguy.java;

public class HelloWorld{
    public static void main(String[] args) {
        Student student = new Student();
        try{
            student.regist(-101);        //调用的时候捕获异常
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(student);
    }
}
class Student {
    int id;
    public void regist(int id){      //因为在MyException继承自运行时异常了这里就不用throws Exception
        if(id > 0){
            this.id = id;
        }else{
//            throw new RuntimeException("输入的数据非法");
            throw new MyException("输入数据非法");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值