学习笔记_毕向东 Java_内部类_异常_多线程 2014.7.15

一、内部类

1、内部类

代码:InnerClassDemo.java

/*
内部类的访问规则:
1、内部类可以直接访问外部类中的成员,包括私有。
	之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式 外部类名.this
2、外部类要访问内部类,必须建立内部类对象。

*/

class Outer
{
	private int x = 3;

	class Inner  //内部类
	{
		int x = 4;
		void function()
		{
			int x = 6;
			System.out.println("inner:" + x);  //得6,内部有就不出去找
			System.out.println("inner:" + this.x);  //得4
			System.out.println("inner:" + Outer.this.x);  //得3
		}
	}

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

class InnerClassDemo
{
	public static void main(String[] args)
	{
		//Outer out = new Outer();
		//out.method();  //得inner:3

		//直接访问内部类中的成员
		Outer.Inner in = new Outer().new Inner();  //一个格式,防止面试的,一般用不到,因为内部类一般都私有了
		in.function();
	}
}

代码:InnerClassDemo2.java

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

2、当内部类在成员位置上,就可以被成员修饰符所修饰。                        (不做重点掌握,要知道。开发一般不写)
	比如,private:将内部类在外部类中进行封装。
		static:内部类就具备static的特性。
		当内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限。

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

		在外部其他类中,如何直接访问static内部类的静态成员呢:
		Outer.Inner.function();  //这种方式使用频率很低

	注意:当内部类中定义了静态成员,该内部类必须是static的。
		  当外部类中的静态方法访问内部类时,内部类也必须是static的。


*/

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();
		new Inner2().show();
	}
}

class InnerClassDemo2
{
	public static void main(String[] args)
	{
		Outer.method();
	}
}
/*
当描述事物时,事物的内部还有事物,该事物用内部类来描述。
因为内部事务在使用外部事物的内容。

class Body
{
	private class XinZang
	{

	}

	public void show()
	{
		new XinZang().
	}	
}

*/

代码:InnerClassDemo3.java

/*
内部类定义在局部时,
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 InnerClassDemo3
{
	public static void main(String[] args) 
	{
		Outer out = new Outer();
		out.method(7);  //得7
		out.method(8);  //得8    为什么a被final修饰了,还能被改变
	}
}

 

2、匿名内部类:

代码:InnerClassDemo4.java

/*
匿名内部类:
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);
		}
	}
	*/

	public void function()
	{
		//new Inner().show();

		new AbsDemo()
		{
			void show()
			{
				System.out.println("x=" + x);
			}
			void abc()
			{
				System.out.println("haha");
			}
		}.abc();

		new AbsDemo()
		{
			void show()
			{
				System.out.println("x=" + x);
			}
			void abc()
			{
				System.out.println("haha");
			}
		}.show();
	}
}

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

代码:InnerClassTest.java

interface Inter
{
	void method();
}

class Test 
{
	//补足代码。通过匿名内部类。
	/*
	static class Inner implements Inter
	{
		public void method()
		{
			System.out.println("method run");
		}
	}
	*/

	static Inter function()
	{
		return new Inter()
		{
			public void method()
			{
				System.out.println("method run");
			}
		};
	}

}



class InnerClassTest 
{
	public static void main(String[] args) 
	{

		//Test.function():Test类中有一个静态的方法function。
		//.method():function这个方法运算后的结果是一个对象。而且是一个Inter类型的对象。
		//因为只有是Inter类型的对象,才可以调用method方法。
		Test.function().method();
		

//		Inter in = Test.function();
//		in.method();


		show(new Inter()
		{
			public void method()
			{
				System.out.println("method show run");
			}
		});

	}

	public static void show(Inter in)
	{
		in.method();
	}
}

class InnerTest  //一个面试题?
{

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


	}
}

 

 

二、异常

1、代码:ExceptionDemo.java

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

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

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

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

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

Throwable
	|--Error
	|--Exception




2,异常的处理

java 提供了特有的语句进行处理。
try
{
	需要被检测的代码;
}
catch(异常类 变量)
{
	处理异常的代码;(处理方式)
}
finally
{
	一定会执行的语句;
}


3,对捕获到的异常对象进行常见方法操作。
	String getMessage():获取异常信息。


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

*/

class Demo
{
	int div(int a,int b)
	{
		return a/b;
	}
}


class  ExceptionDemo1
{
	public static void main(String[] args) //throws Exception
	{
		Demo d = new Demo();
		try
		{
			int x = d.div(4,0);
			System.out.println("x="+x);
		}
		catch (Exception e)
		{
			System.out.println(e.toString());
		}
		
		

		System.out.println("over");

	}
}

代码:ExceptionDemo1.java

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

*/

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


class  ExceptionDemo1
{
	public static void main(String[] args) //throws Exception
	{
		Demo d = new Demo();
		try
		{
			int x = d.div(4,0);
			System.out.println("x="+x);
		}
		catch (Exception e)
		{
			System.out.println(e.toString());
		}
		
		

		System.out.println("over");

	}
}

代码:ExceptionDemo2.java

/*
对多异常的处理。

1,声明异常时,建议声明更为具体的异常。这样处理的可以更具体。
2,对方声明几个异常,就对应有几个catch块。不要定义多余的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(5,0);
			System.out.println("x="+x);
		}
		

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

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

		catch(Exception e)  //多态性,你抛什么异常,我都能处理
		{
			System.out.println("hahah:"+e.toString());
		}	
		/**/
		

		System.out.println("over");

	}
}

代码:ExceptionDemo3.java

 

三、多线程

1、创建两个线程,和主线程交替运行。

/*
创建线程的第一种方式:继承Thread类。
步骤:
1、定义类继承Thread。
2、复写Thread类中的run方法。
	目的:将自定义代码存储在run方法。让线程运行。
3、调用线程的start方法,
	该方法两个作用:启动线程,调用run方法。
*/

class Thread_dd extends Thread
{
	public void run()
	{
		int y;
		for(y = 0; y < 5; y++)
			System.out.println("thread--" + y);
	}
}

class MyThread
{
	public static void main(String[] args) 
	{
		Thread_dd t = new Thread_dd();
		t.start();

		for(int x = 0; x < 5; x++)
			System.out.println("Hello World----" + x);

		Thread_dd t1 = new Thread_dd();
		t1.start();
	}
}





 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值