JavaDay07-代码块、内部类、抽象类、接口、异常
一、代码块
- 调用顺序:
1、静态代码块 2、匿名代码块 3、构造器
public class CodeBlock {
//静态代码块 只调用一次
static {
System.out.println("静态代码块");
}
//匿名代码块 先于构造器调用 可用于初始化一些值
{
System.out.println("匿名代码块");
}
//
public CodeBlock() {
System.out.println("构造器");
}
//主方法
public static void main(String[] args) {
CodeBlock codeBlock1 = new CodeBlock();
System.out.println("===========");
CodeBlock codeBlock2 = new CodeBlock();
}
}
- 输出结果:
静态代码块
匿名代码块
构造器
===========
匿名代码块
构造器
- static关键字总结
1、静态导入包 import static java.lang.Math.random 后面直接在方法中使用该方法即可
2、静态方法 由类名直接调用
3、静态代码块 不管生成多少个对象 只执行一次static代码块
二、内部类
1、成员内部类:可以访问外部类的属性和方法
2、静态内部类:只能访问外部类的静态属性和方法
3、局部内部类-写在方法里面
4、匿名内部类
public class InnerClass {
private int id=10;
public void out(){
System.out.println("这是外部类的方法。");
}
//成员内部类:可以访问外部类的属性和方法
class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的属性
public void get(){
System.out.println(id);
}
}
//静态内部类:只能访问外部类的静态属性和方法
static class B{
//static写在class后面会报错
}
//局部内部类-写在方法里面
public void amazingMethod() {
class Orange {
public void eat() {
System.out.println("Eat orange.");
}
}
}
public static void main(String[] args) {
InnerClass out = new InnerClass();
Inner in = out.new Inner();//成员内部类的定义
//匿名内部类
new Fruit().eat();
Mouse mouse=new Mouse(){//定义了一个没有名字的接口的实现类,需要实现接口的所有方法,最后需要有分号
@Override
public void eat() {
}
};
}
}
class Fruit{
public void eat(){
System.out.println("Nothing to eat.");
}
}
interface Mouse{
void eat();
}
三、抽象类
1、由abstract修饰的类
2、只能被继承,不能被实例化,但有构造器。
3、继承了抽象类的子类必须实例化抽象类中的抽象方法,除非这个子类也是抽象类。
4、抽象类中的方法只能通过继承来实现
5、抽象类和接口的区别:抽象类是单继承,接口可以多继承
- 普通类-只有具体实现
- 抽象类-具体实现+规范(抽象方法)
- 接口-只有规范
//抽象类-由abstract修饰的类
public abstract class AbstractDemo {
public abstract void eat();//抽象方法 只能存在于抽象类中
public void run(){//可以存在普通方法
System.out.println(" ");
}
public static void talk(){
System.out.println("talk");
}
public static void main(String[] args) {
AbstractDemo.talk();
}
}
四、接口
接口的定义:interface关键词
1、接口中的方法默认为public abstract类型
2、接口中的变量默认为public static final类型
3、接口没有构造器,不能实例化
4、接口必须被实现
5、实现接口的类必须实现接口中的所有方法
接口的作用:
1、接口本身是一种约束
2、定义了一些方法,可以有不同的类实现
//接口-interface关键词
public interface UserService {
void add();
void delete();
void update();
void query();
}
public interface TimeService {
void time();
}
//接口的实现
public class UserServiceImpl implements UserService,TimeService{
@Override
public void add() {
}
@Override
public void delete() {
}
@Override
public void update() {
}
@Override
public void query() {
}
@Override
public void time() {
}
}
五、异常
Exception-异常的三种类型:
1、检查性异常:用户输入的错误或问题引起的异常,程序员无法预见
2、运行时异常:程序运行起来才会被发现,程序员可以编写代码使其编译时忽略这种异常
3、Error错误:如栈溢出,JVM导致,程序猿无法控制
public class ExceptionDemo {
//Error: StackOverflowError 栈溢出
public static void main(String[] args) {
new ExceptionDemo().a();
}
public void a(){
b();
}
public void b(){
a();
}
}
public class ExceptionDemo02 {
//Exception: ArithmeticException 算数异常(运行时异常)
public static void main(String[] args) {
System.out.println(11/0);
}
}
异常处理机制:
1、抛出异常
2、捕获异常
异常处理关键字:
1、try
2、catch-捕获
3、finally
4、throw-抛出
5、throws-方法中抛出
public class CatchExceptionDemo03 {
public static void main(String[] args) {
int a=2;
int b=1;
int c=0;
//捕获异常
try {
System.out.println(a/b);
System.out.println(a/c);
}catch (ArithmeticException e){//需要知道异常类型,小括号里是形参
System.out.println("算术异常:除数不能为0.");
}finally {//可以不用finally,通常用来关闭资源;处理善后工作,无论有没有捕获到异常都会执行finally
System.out.println("Finally.");
}
}
}
//快捷键:Ctrl+Alt+T
public class CatchExceptionsDemo04 {
public static void main(String[] args) {
int a=2;
int b=1;
int c=0;
//捕获多个异常: 从小到大,层层递进
try {
System.out.println(a/c);
}catch (Error e){
System.out.println("Error");
}catch (Exception e){
System.out.println("Exception");
}catch (Throwable t){
System.out.println("Throwable");
}
finally{
System.out.println("Finally.");
}
}
}
抛出异常:
在方法内部用throw抛出异常,
抛出异常后最好在声明中throws异常,
并且需要在调用此方法的地方捕获异常。
- throw与throws的比较:
1、throws出现在方法函数头;而throw出现在函数体。
2、throws表示出现异常的一种可能性,并不一定会发生这些异常;
throw则是抛出了异常,执行throw则一定抛出了某种异常对象。
public class ThrowExceptionsDemo05 {
public static void main(String[] args) {
int a=2;
int b=1;
int c=0;
System.out.println(a/b);
//需要捕获抛出的异常,否则程序不会继续执行
try {
new ThrowExceptionsDemo05().test(0);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//主动抛出异常
public void test(int a)throws ArithmeticException{//throws
if(a==0){
throw new ArithmeticException();//throw
}
}
}
自定义异常:
1、extends Exception
2、构造器接收异常
3、打印异常信息
public class CustomizeExceptionDemo06 extends Exception{
int detail;
public CustomizeExceptionDemo06(int a) {
detail=a;
}
@Override
public String toString() {
return "CustomizeExceptionDemo06{" +
"detailmessage='" + detail + '\'' +
'}';
}
}
public class Test01 {
//主方法中捕获异常
public static void main(String[] args) {
try {
new Test01().test(11);
} catch (CustomizeExceptionDemo06 e) {
//这里可以增加一些处理异常的代码块
System.out.println("Exception"+e);
}
}
//抛出异常
public void test(int a) throws CustomizeExceptionDemo06 {
System.out.println("传递的参数为:"+a);
if (a>10){
throw new CustomizeExceptionDemo06(a);
}
System.out.println("OK");
}
}
异常经验总结:
1、多重catch,增加一个catch(Exception)来处理可能遗漏的异常
2、尽量处理异常,而不是简单打印
3、添加finally释放资源

浙公网安备 33010602011771号