Java基础_面向对象(内部类、异常1)


一、面向对象(内部类访问规则)

内部类的访问规则:

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();

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

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

/*
内部类访问规则:
*/
class Outer
{
	private int x=3;

	class Inner//内部类
	{
		int x=5;
		void function()
		{
			int x=6;
			System.out.println("inner:"+x);
			System.out.println("inner:"+this.x);
			System.out.println("Outer:"+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();
	}
}


 

/*
静态内部类:
*/
class Outer
{
	private static int x=3;

	static class Inner//内部类
	{
		static void function()
		{
			System.out.println("inner:"+x);
		}
	}
	static class Inner2
	{
		void show()
		{
			System.out.println("inner2 show");
		}
	}
	public static void method()
	{
		Inner.function();
	}
	
}
class  InnerClassDemo
{
	public static void main(String[] args) 
	{
		Outer.method();
	}
}

 


二、面向对象(内部类定义原则)

当描述事物时,事物的内部还有事物,该事物用内部类来描述。这是因为内部事物在使用外部事物的内容。

/*
例如:身体和心脏的关系
*/
class Body//身体
{
	private class Heart//心脏。进行封装
	{
		void run()//工作
		{
			System.out.println("run ");
		}
	}

	void show()//对外提供的方法
	{
		new Heart().run();
	}

}


三、面向对象(匿名内部类)

内部类定义在局部时:

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

2.可以直接访问外部类中的成员,因为还持有外部类中的引用。但是不可以访问它所在的局部中的变量,只能访问被final修饰的局部变量。

匿名内部类:

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

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

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

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

5.匿名内部类中定义的方法最好不要超过3个。

/*
匿名内部类:
*/
abstract class AbsDemo
{
	abstract void show();
}

class Outer
{
	int x=3;
	/*
	class Inner extends AbsDemo
	{
		void show()
		{
			System.out.println("show:"+x);
		}
		void show2()
		{
			System.out.println("show");
		}
	}
	*/

	public void function()
	{
		//new Inner().show();
		AbsDemo a=new AbsDemo()
		{
			void show()
			{
				System.out.println("x:"+x);
			}
			void show2()
			{
				System.out.println("show");
			}
		};
		a.show();
		//a.show2();//父类引用不能调用子类添加的方法
	}
}
class InnerClassDemo3 
{
	public static void main(String[] args) 
	{
		new Outer().function();

	}
}


 

/*
匿名内部类练习:
*/
interface Inner
{
	public abstract void method();
}
class Test
{
	//补足代码。通过匿名内部类。
	static Inner function()
	{
		return new Inner()
		{
			public void method()
			{
				System.out.println("method show");
			}
		};
		
	}

}
class InnerClassTest
{
	public static void main(String[] args)
	{
		Test.function().method();
	}
}


 

/*
没有父类或接口,使用匿名内部类运行一个函数
*/

class InnerTest
{
	public static void main(String[] args)
	{
		new Object()
		{
			public void function()
			{
				System.out.println("function run");
			}
		}.function();
	}
}


四、面向对象(异常概述)

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

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

对于问题的划分:

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

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

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

Throwable

----Error

----Exception

/*
异常:
*/

class Demo
{
	int div(int a,int b)
	{
		return a/b;
	}
}
class ExceptionDemo 
{
	public static void main(String[] args) 
	{
		System.out.println(new Demo().div(4,0));
		System.out.println("结束");
	}
}


五、面向对象(异常的处理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) 
	{
		try
		{
			System.out.println(new Demo().div(4,0));
		}
		catch (Exception e)//Exception e=new ArithmeticException();
		{
			System.out.println("被除数不能为0");
			System.out.println(e.getMessage());//    /by zero
			System.out.println(e.toString());//异常名称: 异常信息

			e.printStackTrace();//JVM默认异常处理机制
		}
		
		System.out.println("结束");
	}
}

 


 

六、面向对象(异常声明throws

/*
异常声明throws
*/

class Demo
{
	int div(int a,int b)throws Exception//在功能上通过throws的关键字声明了该功能有可能会出现的问题
	{
		return a/b;
	}
}
class ExceptionDemo 
{
	public static void main(String[] args) 
	{
		System.out.println(new Demo().div(4,1));
		System.out.println("结束");
	}
}


 

 

七、面向对象(多异常处理)

对多异常的处理:

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

2.对方声明几个异常,就应该有几个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[5]);//数组角标越界
		return a/b;
	}
}
class ExceptionDemo 
{
	public static void main(String[] args) 
	{
		try
		{
			System.out.println(new Demo().div(5,0));
		}
		catch (ArithmeticException e)
		{
			System.out.println("除数不能为0");
		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			System.out.println("数组角标越界");
		}
		catch (Exception e)
		{
			System.out.println(e.toString());
		}


		System.out.println("结束");
		
	}
}


八、面向对象(自定义异常)

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

当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作:

1.在内部try catch处理

2.在函数上声明让调用者处理

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

定义异常信息:

父类中已经把异常信息的操作都完成了,所以子类只需要在构造时将异常信息传递给父类(通过super语句),那么就可以直接使用getMessage方法获取自定义的异常信息。

注意:

自定义异常必须是自定义类继承Exception。原因:异常体系有一个特点,因为异常类和异常对象都需要被抛出,它们都具有可抛性。这个可抛性是Throwable这个体系中独有特点,只有这个体系中的类和对象才可以被throwsthrow操作。

/*
自定义异常:
需求:在本程序中,对于出书是-1,也视为错误的是无法进行运算的,那么
就需要对这个问题进行自定义的描述。

*/
class FuShuException extends Exception
{
	/*
	private String msg;
	FuShuException(String msg)
	{
		this.msg=msg;
	}

	public String getMessage()//复写getMessage方法
	{
		return msg;
	}
	*/
	private int value;
	FuShuException()
	{
		super();
	}
	FuShuException(String msg,int value)
	{
		super(msg);
		this.value=value;
	}
	public int getValue()
	{
		return value;
	}
}
class Demo
{
	int div(int a,int b)throws FuShuException
	{
		if(b<0)
			throw new FuShuException("除数不能为负数",b);//手动通过throw关键字抛出一个自定义异常对象
		return a/b;
	}
}
class ExceptionDemo4 
{
	public static void main(String[] args) 
	{
		try
		{
				System.out.println(new Demo().div(4,-1));
		}
		catch (FuShuException e)
		{
			System.out.println(e.toString());
			System.out.println("错误的负数是:"+e.getValue());
		}
		System.out.println("结束");
	}
}


九、面向对象(throwthrows的区别)

throws:在方法的定义中说明方法可能抛出的异常,后面跟异常类的名字,声明这个方法将不处理异常,把异常交给上一级方法处理。可以跟多个,用逗号隔开。

throw:使用在函数内,后面跟异常对象。

注意:调用时,调用者不能抛出范围更小的异常。

对于方法a,如果它定义了throws Exception。那么当它调用的方法b返回异常对象时,方法a并不处理,而将这个异常对象向上一级返回,如果所有的方法均不进行处理,返回到主方法,如主方法也不进行处理,则到虚拟机中,程序中止。

如果在方法的程序中有一行throw new Exception(),那么其后的程序不执行,如果没有对这个可能出现的检查结果进行处理,那么程序就会报错。

十、面向对象(RuntimeException

java.lang.Object

  java.lang.Throwable

      java.lang.Exception

          java.lang.RuntimeException

              java.lang.ArithmeticException

Exception中有一个特殊的子类异常RuntimeException运行时异常。

如果在函数内抛出该异常,函数上可以不用声明该异常,编译一样可以通过。

如果在函数上声明该异常,调用者可以不用进行处理,编译一样可以通过。

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

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

对于异常分两种:

1.编译时被检测的异常。

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

/*
RuntimeException:
*/
class FuShuException extends RuntimeException
{
	FuShuException(String msg)
	{
		super(msg);
	}
}
class Demo
{
	int div(int a,int b)
	{
		if(b<0)
			throw new FuShuException("除数不能为负数");
		if(b==0)
			throw new ArithmeticException("除数不能为0");
		return a/b;
	}
}
class ExceptionDemo5 
{
	public static void main(String[] args) 
	{
		System.out.println(new Demo().div(4,-1));
		System.out.println("结束");
		
	}
}

 


十一、面向对象(异常练习)

/*
毕老师用电脑上课。

上课时可能出现的问题:电脑蓝屏,电脑冒烟。

要对问题进行描述,封装成对象。

当冒烟发生后,会发生讲课无法继续,就出现了讲师的问题:课时计划无法完成。
*/
class LanPingException extends Exception//蓝屏是可以处理的
{
	LanPingException(String message)
	{
		super(message);
	}
}
class MaoYanException extends Exception//冒烟
{
	MaoYanException(String message)
	{
		super(message);
	}
}

class NoPlanException extends Exception//无法完成课时计划
{
	NoPlanException(String msg)
	{
		super(msg);
	}
}
class Computer
{
	private int state=3;//状态值:1正常 2出异常
	public void run()throws LanPingException,MaoYanException
	{
		if(state==2)
			throw new LanPingException("电脑蓝屏了!");
		if(state==3)
			throw new MaoYanException("电脑冒烟了!");

		System.out.println("电脑运行");
	}
	public void reset()
	{
		System.out.println("电脑重启");
	}
}
class Teacher
{
	private String name;
	private Computer cmpt;
	Teacher(String name)
	{
		this.name=name;
		cmpt=new Computer();
	}
	public void prelect()throws NoPlanException
	{
		try
		{
			cmpt.run();	
		}
		catch (LanPingException e)
		{
			cmpt.reset();//重启
		}
		catch (MaoYanException e)
		{
			test();
			throw new NoPlanException("课时无法继续"+"原因:"+e.getMessage());
			
		}
		System.out.println("讲课");
	}
	public void test()
	{
		System.out.println("学生们去做练习");
	}
}



class ExceptionTest 
{
	public static void main(String[] args) 
	{
		Teacher t=new Teacher("毕老师");
		
		try
		{
			t.prelect();
		}
		catch (NoPlanException e)
		{
			System.out.println(e.toString());
			System.out.println("换老师或者换电脑");
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值