黑马程序员------面向对象(No.7)(内部类、异常概述、异常try-catch、异常声明throws、多异常处理)

 ---------------------- ASP.Net+Android+IO开发S.Net培训、期待与您交流! ---------------------- 

 

微笑内部类

将一个类定义在另一个类的里面,里面那个类就称作内部类(内置类、嵌套类)。

 

访问规则:

1.内部类可以直接访问外部类中的成员,包括私有成员。

之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式 外部类 外部类名.this

2.而外部类要访问内部类中的成员必须建立内部类的对象。

class Outer{
	private int x = 3;
	void method(){
		Inner in = new Inner();
		in.function();
	}
	class Inner	{
		int x = 4;
		void function(){
			int x =6;
			System.out.println("内部类:"+x);//输出 内部类:6
			System.out.println("内部类:"+this.x);//输出 内部类:4
			System.out.println("内部类:"+Outer.this.x);//输出 内部类:3
		}
	}
}
class InnerClassDemo{
	public static void main(String[] args){
		//Outer o = new Outer();
		//o.method();
		Outer.Inner in = new Outer().new Inner();
		in.function();
	}
}


访问格式

1.当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中直接建立内部类对象。

外部类名.内部类名 变量名 = new 外部类名( ).new 内部类名( ) ;

Outer.Inner in = new Outer().new Inner();

2.当内部类在成员位置上,就可以被成员修饰符所修饰。

比如private:将内部类在外部类中进行封装。

static:内部类就具备static特性。

      当内部类被static修饰后,只能直接访问外部类中的static成员,访问出现了局限。

      在外部其他类中,如何直接访问static内部类的非静态成员呢?

      new Outer.Inner().function();

      在外部其他类中,如何直接访问static内部类的静态成员呢?

      Outer.Inner.function();

注意:当内部类中定义了静态成员,给内部类必须是静态内部类。

     当外部类中静态方法访问内部类时,内部类也必须是静态的。

class Outer{
	private static int x = 3;
	static class Inner	{
		static void function(){
			System.out.println("内部类:"+x);
		}
	}
}
class InnerClassDemo{
	public static void main(String[] args){
		new Outer.Inner().function();
	}
}

 

当描述事物时,事物的内部还有事物,该事物用内部类来描述。

因为内部事物在使用外部事务内容。

 class Body{

       private class XinZang{

     }

      public void show(){

      new  XinZang();

     }

}

内部类定义在局部时

1.不可以被成员修饰符修饰

2.可以直接访问外部类中的成员,因为还持有外部类中的引用。

   但是不可以访问它所在的局部变量中的变量,只能访问被final修饰的局部变量。

 

匿名内部类

1.匿名内部类其实就是内部类的简写格式。

2.定义匿名内部类的前提,内部类必须是继承一个类或者实现接口。

3.匿名内部类的格式: new 父类或者接口(){定义子类的内容}

4.其实匿名内部类就是一个匿名子类对象。而且这个对象有点胖。可以理解为带内容的对象。

interface Inter{
	void method();
}
class Test{
	//通过匿名内部类补足代码。
	static Inter function(){
		return new Inter(){
			public void method(){
				System.out.println("method");
			}
		};
	}
}
class InnerClassTest{
	public static void main(String[] args){
		//Test.function():Test类中有一个静态的方法function。
		//.method():function这个方法运算后的结果是一个对象。而且是一个Inter类型的对象。
		//因为只有是Inter类型的对象,才可以调用method方法。
		Test.function().method();//输出method
	}
}

 

微笑内部类

 

异常的体系

Throwable

1.Error

     通过出现重大问题时,运行的类不存在。或者内存溢出来。

    不编写针对代码对其处理

2.Exception

   在运行时运行出现的一切情况,可以通过try catch finally捕捉处理

class Demo{
	int div(int a,int b){
		return a/b;
	}
}
class ExceptionDemo{
	public static void main(String[] args){
		Demo d = new Demo();
		int x = d.div(4,0);//异常
		System.out.println(x);
		System.out.println("over");
	}
}

编译通过,运行时报 ArithmeticException异常。

 

异常:就是程序在运行时出现不正常的情况。

 

异常的由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述,并封装成对象。其实就是java对不正常情况进行描述后的对象体现。

 

对问题的划分:一种是严重的问题,一种是非严重的问题。

 

对于严重的问题,java是通过Error类进行描述。

             对于Error一般不编写针对性代码对其进行处理。

对于非严重的,java是通过Exception类进行描述。

            对于Exception可以使用针对性的处理方式进行处理。

 

无论Error或者Exception都具有一些共性内容。比如:不正常情况的信息,引发原因等。

Throwable

      |------Error

      |------Exception

 

异常try-catch

 

java提供了特有语句进行处理

 

try{

     需要被检测的代码

}catch(异常类 变量){

     处理异常类代码(处理方式)

}finally{

      一定会执行的语句

}

class Demo{
	int div(int a,int b){
		return a/b;
	}
}
class ExceptionDemo{
	public static void main(String[] args){
		Demo d = new Demo();
		try{
			int x = d.div(4,0);//异常  new AritchmeticException() 跳转
			System.out.println(x);
		}catch(Exception e){     //Exception e = new AritchmeticException()
			System.out.println("除0了");//处理问题
			System.out.println(e.getMessage());// /by zero
			System.out.println(e.toString());// 异常名称,异常信息。
			e.printStackTrace();//异常名称,异常信息。异常出现的位置。
			//(jvm默认的异常处理就是调用printStackTrace打印异常在堆栈中的跟踪信息)
		}
		System.out.println("over");
	}
}


输出情况

 

异常的声明throws

在功能上通过throws的关键字声明了该功能有可能出现问题。

class Demo{
	int div(int a,int b)throws Exception{
		return a/b;
	}
}
class ExceptionDemo{
	//方式一
	public static void main(String[] args)throws Exception{
		Demo d = new Demo();
		int x = d.div(4,0);
		System.out.println(x);
		System.out.println("over");
	}
	/*
	//方式二
	public static void main(String[] args){
		Demo d = new Demo();
		try{
			int x = d.div(4,0);
			System.out.println(x);
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println("over");
	}
	*/
}


 

多异常处理 

 

1.声明异常时,建议声明更为具体的异常。这样的处理更具体。

2.对方声明几个异常,就对应应有几个catch块,不要定义多余的catch块。

     如果多个catch块中的异常出现继承关系,父类异常catch块放最下面。

 

建立在进行catch处理时,catch中一定要定义具体处理方式。不要简单定义一句e.printStackTrace(),也不要简单的就书写一条输出语句。

(下面为了描述清楚,才使用简单的输出语句,平时操作不能这样)

class Demo{
	int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException{
		int[] arr = new int[a];
		System.out.println(arr[4]);
		return a/b;
	}
}
class ExceptionDemo{
	public static void main(String[] args){
		Demo d = new Demo();
		try{
			int x = d.div(5,0);
			System.out.println(x);
		}catch(ArithmeticException e){
			System.out.println(e.toString()+" 被0整除了");
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println(e.toString()+" 下标越界了");
		}catch(Exception e){
			System.out.println(e.toString()+" 父类异常catch块放最下面");
		}
		System.out.println("over");
	}
}


 

 

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ---------------------- 

详情请查看:http://edu.csdn.net

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值