黑马程序员--第九天:内部类及异常

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

 

/**9-1
title: 内部类访问规则
		//成员内部类。将一个类定义在另一个类的里面,对里面那个类就称为内部类(内置类,嵌套类)。
1.内部类可以直接访问外部类中的成员,包括私有成员,
	之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,
	格式为	外部类名.this.成员;
2.外部类要访问内部类中的成员必须要建立内部类的对象。

*/

 class Outer{
	 
	 private int x = 3;

	 class Inner{    //内部类
		 //int x = 4;
		 void function(){
			 //int x = 6;
			 System.out.println("innner :"+Outer.this.x);//在内部类中访问外部类。
		 }
	 }

	void method(){
		 Inner in = new Inner();
		 in.function();
	 }
 }

class  InnerClassDemo{
	 public static void main(String[] args) {
		 Outer out = new Outer();

		 out.method();
		 //直接访问内部类中的成员。
		 Outer.Inner in = new Outer().new Inner();//
		 
		 in.function();
	 }
 }



/**9-2
内部类访问规则
		//成员内部类。将一个类定义在另一个类的里面,对里面那个类就称为内部类(内置类,嵌套类)。
1.内部类可以直接访问外部类中的成员,包括私有成员,
	之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,
	格式为	外部类名.this.成员;
2.外部类要访问内部类中的成员必须要建立内部类的对象。

访问格式:
1.	当内部类定义在外部类的成员位置上,而且非私有,可以在 外部类的其他类(不是该外部类) 中。
	可以自己建立内部类对象。
	格式:
		外部类名.内部类名	变量名 = 外部类对象.内部类对象;
		Outer.Inner in = new Outer().new Inner();

2.  当内部类在成员位置上,就可以被成员修饰符修饰。
	 比如:private,将内部类在外部类进行封装。
	       static:内部类就具备了static的特性。
		   当内部类被static修饰后,只能直接访问外部类中的static成员。
		   出现访问局限。

		在外部其他类中,如何直接访问static内部类的非静态成员呢?
		new Outer.Inner().function();

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

		Outer.Inner.function();

	注意:1当内部类中定义了静态成员,该内部类必须是static。//??? 个人理解:
				当内部类定义了static的成员是,就意味着@1 = Outer.Inner.function(); 是
				可以正确执行的,但是该内部类却不是static,则意味着 @2 = Outer.Inner   是
				错误的,因此@1和@2是矛盾的。因此必须符合注意1。
		  2当外部类中的静态方法访问内部类时,内部类也必须是static。
		  //疑问:static main 可以访问非静态的成员,为何内部类就不行?
				 //个人理解:内部类 创建对象时,依赖外部类的数据。
				 因此该方法可以以 
				 //new Outer().new Inner2().show(); 正常运行。

		  3 非static 的内部类 创建对象时,依赖外部类的数据。
*/

//下面是一个静态内部类的例子。

 class Outer{
	 
	 private static int x = 3;
	 int y;

	// Outer(int y){
	//	 this.y = y;
	// }

	 static class Inner{    
		// int z = y;  编译通不过。
		 int z = x;  //而static的内部类 可以用 new Outer.Inner() 来创建对象。而此时 y 并不存在,x存在,
		 //所以static 内部类里面的成员 只能访问外部类中static 的成员。
		  static void function(){
			 //在该方法中可以用 new Outer().x   
			 //但是不能用Outer.this.x   因为访问该方法的未必是对象,可以是类,
			 //所以this是不能用的。
			 System.out.println("innner :"+ x);//在内部类中访问外部类。
		 }
	 }

	 class Inner2
	 {
		 void show(){
			 System.out.println("inner2 show" + y);//因为要要的成员变量 y, 所以内部类Inner2
			 //必须在 new Outer() 对象后才能 创建对象。 即非static 的内部类 创建对象时,
			 //依赖外部类的数据。
		 }
	 }

	 public static void method(){
		 Inner.function();
		 new Outer().new Inner2().show();//疑问:static main 可以访问非静态的成员,为何内部类就不行?
							 //个人理解:内部类像成员变量一样只有在new 出对象是才会存在于
							 //内存中,即加载内部类。因此该方法可以以 
							 //new Outer().new Inner2().show(); 正常运行。
	 }
 }


class  InnerClassDemo{
	 public static void main(String[] args) {
		
		 new Outer.Inner().function();//直接访问内部类中的成员。
		Outer.method();
	 }
 }


/**9-3
内部类定义原则:
	当描述事物时,事物的内部还有事物,该事物用内部类来描述。
	疑问内部事物在使用外部事物的内容。
*/

 class Outer{
	 
	 private int x = 3;

	 static class Inner{    

		 static void function(){
			 System.out.println("innner :"+Outer.this.x);//在内部类中访问外部类。
		 }
	 }

	 class Inner2
	 {
		 void show(){
			 System.out.println("inner2 show");
		 }
	 }

	 public static void method(){
		 Inner.function();
		 new Inner2().show();//疑问:static main 可以访问非静态的成员,为何内部类就不行?
	 }
 }


class  InnerClassDemo{
	 public static void main(String[] args) {
		
		 new Outer.Inner().function();//直接访问内部类中的成员。

	 }
 }


/*9-4
匿名内部类:
1.内部类的简写格式
2.定义匿名内部类的前提:
	内部类必须继承一个类或实现接口。
3.匿名内部类的格式: new 父类或者接口(){定义子类内容}
4.其实匿名内部类就是一个匿名子类对象。也可理解为带内容的子类对象。
5.匿名内部类中定义的方法最好不超过3个。	
*/

class AbsDemo
{
	abstract void show();
}

class Outer
{
	int x = 3;

	class Innter extends AbsDemo
	{
		void method(){
			System.out.println("method :" + x)
		}
	}

	public void funciton(){
		new Inner().method();

		new AbsDemo(){
			void show(){
				System.out.println("method :" + x);
			}
		}.show();
	}
}

class  
{
	public static void main(String[] args) 
	{
		new Outer().function();
	}
}


/**9-4
局部内部类:内部类定义在局部
1.不可以被成员修饰符修饰。
2.可以自己访问外部类中的成员,因为还有外部类中的引用。
	但是不可以访问它所在的局部中的变量,只能访问被final修饰的局部变量。//??什么原因?

*/

class  Outer
{
	int x = 3;

	void method(final int a){
		final int y  = 4;

		class Inner
		{
			void function(){
				System.out.println(a);
			}
		}
		new Inner().function();
	}

}

class InnerClassDemo
{
	public static void main(String[] args){
		Outer out = new Outer();
		out.method(7);
		out.method(8);
	}
}


/**9-5
异常:就是程序出现不正常的情况。
异常的由来:问题也是生活中的一个具体的事物,可以通过类来描述,并封装成对象。

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

对于严重问题,java通过Error类 描述
	对于Error一般不编写针对性的代码进行处理。

对于非严重问题,java通过Exception类 描述
	对于Exception可以使用针对性的方式进行处理。


Throwable
	|--Error
	|--Exception
*/

class  
{
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
	}
}


/**9-6
异常处理
try{
	需要被检测的代码
}
catch(异常类 变量){
	处理异常的代码;(处理方式)
}
finally{
	一定会执行的代码;
}

System.out.println(e.getMessage());//异常信息
System.out.println(e.toString());//异常名称,异常信息。
e.printStackTrace();//异常名称,异常信息,异常出现的位置。
					//其实jvm默认的异常处理机制就是在调用printStackTrace();
					//打印异常的堆栈的跟踪信息。
*/



class  
{
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
	}
}


/*9-7
对捕捉到的异常对象进行常见方法操作。
String getMessage(); 获取异常信息。

在函数上声明异常。
便于提高安全性,让调用函数进行处理,不处理编译失败。
*/

class Demo
{
	int div(int a, int b) throws Exception//在功能上通过throws关键字声明了该功能有可能会出现问题。
	{
		return a/b;
	}
}

class  
{
	public static void main(String[] args) throws Exception
	{
		Demo d = new Demo();

		int x = d.div(4,1);
		System.out.println("x=" + x);

		System.out.println("over");
	}
}


/*9-8
对多异常的处理
1.声明异常时,建议声明更为具体的异常,这样可以处理的更具体。
2.对方声明几个异常就有几个catch块
	如果多个catch块中异常出现继承关系,父类异常catch块放在最下面。

建议在catch处理是时,catch中一定要定义具体处理方式。
不用简单的定义一句e.printStackTrace();
也不要简单的书写一条输出语句。
*/

class Demo
{
	int div(int a, int b) throws ArithmeticException,ArrayIndexOutOfBoundsException//在功能上通过throws关键字声明了该功能有可能会出现问题。
	{
		int[] arr = new int[a];
		System.out.println(arr[4]);

		return a/b;
	}
}

class  ExceptionDemo2
{
	public static void main(String[] args) throws Exception
	{
		Demo d = new Demo();

		try{
			int x = d.div(4,1);
			System.out.println("x=" + x);
		}
		/*catch(Exception e){
			System.out.println(e.toString());
		}*/

		catch(ArithmeticException e){
			System.out.println(e.toString());
			System.out.println("被零除了:::");

		}catch (ArrayIndexOutOfBoundsException e){
			System.out.println(e.toString());
			System.out.println("角标越界");
		}

		System.out.println("over");
	}
}


/*9-9
因为项目中会出现特有问题,
而这些问题并未被java所描述并封装。
所以对于这些特有的问题可以按照java对问题封装的思想。
将特有的问题,进行自定义的异常封装。

自定义异常。

需求:在本程序中对于除数是负数,也视为是错误的,是无法进行运算的。
那么就需要对这个问题进行自定义描述。

当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作:
	要么就在内部try catch处理。
	要么在函数上声明让调用者处理。

一般情况在函数内出现异常,函数上需要声明。

发现打印的结果只有异常的名称,却没有异常的信息。
因为自定义的异常并未定义信息。

如何处理异常信息?
因为父类已经把异常信息的操作都完成了。
所以之类只要在构造时,将异常信息传递给父类通过super语句。
那么就可以直接通过getMessage方法获取自定义的异常信息。


自定义异常:
必须是自定义类继承Exception。

继承Exception原因:
异常体系有一个特点,因为异常类和异常对象都被抛出。
他们都具有可抛性,这个可抛性是Throwable这个体系中的独有特点。

只有这个体系中的类和对象才可以被throws和throw操作。
*/

class FuShuException extends Exception
{
	private int value;
/*	private String msg;
	FuShuException(String msg){
		this.msg = msg;
	}
	public String getMessage(){
		return msg;
	}

	*/

	FuShuException(){
		super();
	}

	FuShuException(String msg){
		super(msg);
	}

	FuShuException(String msg, int value){
		super(msg);
	}

	public int getValue(){
		return value;
	}
}

class Demo
{
	int div(int a, int b)
	{
		if(b<0)
			throw new FuShuException("/ by minus", b);//手动通过throw关键字抛出一个自定义的异常对象。
		return a/b;
	}
}

class  ExceptionDemo2
{
	public static void main(String[] args) throws Exception
	{
		Demo d = new Demo();

		try{
			int x = d.div(4,1);
			System.out.println("x=" + x);
		}
		/*catch(Exception e){
			System.out.println(e.toString());
		}*/

		catch(ArithmeticException e){
			System.out.println(e.toString());
			System.out.println("被零除了:::");
			System.out.println("错误的负数是:"+ e.getValue());

		}catch (ArrayIndexOutOfBoundsException e){
			System.out.println(e.toString());
			System.out.println("角标越界");
		}

		System.out.println("over");
	}
}


/*9-9
因为项目中会出现特有问题,
而这些问题并未被java所描述并封装。
所以对于这些特有的问题可以按照java对问题封装的思想。
将特有的问题,进行自定义的异常封装。

自定义异常。

需求:在本程序中对于除数是负数,也视为是错误的,是无法进行运算的。
那么就需要对这个问题进行自定义描述。

当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作:
	要么就在内部try catch处理。
	要么在函数上声明让调用者处理。

一般情况在函数内出现异常,函数上需要声明。

发现打印的结果只有异常的名称,却没有异常的信息。
因为自定义的异常并未定义信息。

如何处理异常信息?
因为父类已经把异常信息的操作都完成了。
所以之类只要在构造时,将异常信息传递给父类通过super语句。
那么就可以直接通过getMessage方法获取自定义的异常信息。


自定义异常:
必须是自定义类继承Exception。

继承Exception原因:
异常体系有一个特点,因为异常类和异常对象都被抛出。
他们都具有可抛性,这个可抛性是Throwable这个体系中的独有特点。

只有这个体系中的类和对象才可以被throws和throw操作。
*/

class FuShuException extends Exception
{
	private int value;
/*	private String msg;
	FuShuException(String msg){
		this.msg = msg;
	}
	public String getMessage(){
		return msg;
	}

	*/

	FuShuException(){
		super();
	}

	FuShuException(String msg){
		super(msg);
	}

	FuShuException(String msg, int value){
		super(msg);
	}

	public int getValue(){
		return value;
	}
}

class Demo
{
	int div(int a, int b)
	{
		if(b<0)
			throw new FuShuException("/ by minus", b);//手动通过throw关键字抛出一个自定义的异常对象。
		return a/b;
	}
}

class  ExceptionDemo2
{
	public static void main(String[] args) throws Exception
	{
		Demo d = new Demo();

		try{
			int x = d.div(4,1);
			System.out.println("x=" + x);
		}
		/*catch(Exception e){
			System.out.println(e.toString());
		}*/

		catch(ArithmeticException e){
			System.out.println(e.toString());
			System.out.println("被零除了:::");
			System.out.println("错误的负数是:"+ e.getValue());

		}catch (ArrayIndexOutOfBoundsException e){
			System.out.println(e.toString());
			System.out.println("角标越界");
		}

		System.out.println("over");
	}
}


/*9-11
Exception 有一个特殊的子类异常RuntimeException 运行时异常,
如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过,
如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过。

之所以不用在函数上声明是因为不需要让调用者处理。
当该异常发生,希望程序停止,因为运行时,出现了无法继续的运算情况,
希望停止程序后对代码进行修正。

自定义异常时,如果该异常的发生,无法继续进行运算,
就让自定义异常继承RuntimeException。

对于异常分两种:
1.编译时被检测的异常。

2.编译时不被检测的异常(运行时异常。RuntimeException以及其子类)。

个人理解:RuntimeException 主要面向程序员,出现这种exception程序员必须要修改代码。
			因为程序一旦出现这种情况就会无法运行,影响用户体验。
		  而其他Exception 主要面向用户, 出现这种exception只是提醒用户,
			操作不符合所要求的规范。比如IOExcption的文件不存在。
*/

class Demo
{
	int div(int a, int b)
	{
		if(b==0)		//手动通过throw关键字抛出一个自定义的异常对象。
			throw new ArithmeticException("被零除 / by minus", b);
		return a/b;
	}
}

class  ExceptionDemo4
{
	public static void main(String[] args) throws Exception
	{
		Demo d = new Demo();

		int x = d.div(4,-9);
		System.out.println("x=" + x);

		System.out.println("over");
	}
}

//个人扩展:

public class Demo{

	public static void main(String[] args) {
		int x = 1;
		
		Object c = new Fin().getC();//c引用了局部内部类对象。
		System.out.println(c.toString());
	}
	
}

class Fin{
	int x = 0;
	
	class In{
		public void showX(){
			System.out.println(x);
		}
	}
	
	public Object getC(){
		final int y = 1;//如果 这里的 y 没有被final 修饰
		//在内部类Inner 中就可以出现 y = y+1;
		
		//Inner in = new Inner();
		class Inner{
			int z = 5;
			public int showY(){
				//y = y +1;
				System.out.println(y);
				x = x +1;
				return x; //之所以可以引用外部类的成员,是因为在使用getC方法时
				//必须先创建外部类Fin的对象。
			}
			
			public String toString(){
				System.out.println("reachable?");
				return (y+z+showY())+"";
			}
		}
		
		Inner in = new Inner();
		
		in.showY();
		
		return in;
		//因为getC返回Inner对象,
		//该对象被其他外部类的参数引用,所以当getC 生命周期结束时,
		//Inner 对象的生命周期没有结束。(第9行 被 c 引用). 因此 y的生命周期已经结束
		//Inner 要使用y的值就必须 把y 复制到 它的对象中,而不能直接引用。
		//所以变量 y 的值是不能在内部类中被赋值的, 即 y = y + 1;这样的操作不会把 y的 赋值为 2;
		//所以为了表明这种操作不成立,或为了避免这样的操作,就必须保证y 是不可变的,即 
		//y 的值要用final修饰。
	}
}


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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值