-------android培训、java培训、java学习型技术博客、期待与您交流! ----------
今天学习了毕老师java基础第9天的内容,在这里总结一下。
一.内部类
将一个类定义在另一个类的里面,对里面那个类就称为内部类(内置类,嵌套类)。
访问特点:
内部类可以直接访问外部类中的成员,包括私有成员。
而外部类要访问内部类中的成员必须要建立内部类的对象。
/*
内部类的访问规则:
1,内部类可以直接访问外部类中的成员,包括私有。
2,外部类要访问内部类,必须建立内部类对象。
*/
class Outer
{
int x = 3;
class Inner//内部类
{
void function()
{
System.out.println("inner :"+x);(内部类直接访问外部类的成员)
}
}
void method()
{
System.out.println(x);
Inner in = new Inner();
in.function();
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
Outer out = new Outer();
out.method();
}
}
运行结果:
能不能直接访问内部类中的成员?
Inner in = new Inner();
in.function();
但是如果有个class 叫Outer2,里面也有一个内部类叫Inner,这样是没有办法区分的,所以要标明Inner所属的外部类:
Outer.Inner in =new Outer. new Inner();
内部类可以被private修饰,当其实外部类的成员时。
class Outer
{
int x = 3;
class Inner
{
int x = 4;
void function()
{
int x = 6;
System.out.println("inner :"+x);
}
}
打印的结果为6(局部有,就不去外面找了)。
想打印4,怎么办?
加this.
class Outer
{
int x = 3;
class Inner
{
int x = 4;
void function()
{
int x = 6;
System.out.println("inner :"+this.x);
}
}
想打印3,怎么办?
Outer.this.x
12
class Outer
{
int x = 3;
class Inner//内部类
{
int x = 4;
void function()
{
int x = 6;
System.out.println("inner :"+Outer.this.x);
}
}
Outer.this其实被省略了:
class Outer
{
int x = 3;
class Inner//内部类
{
void function()
{
System.out.println("inner :"+x);
}
}
内部类能直接访问外部类中的成员的原因:内部类中持有了一个外部类的引用,格式:
外部类名.this
访问格式:
1,当内部类定义在外部类的成员位置上,而且非私有,可以在外部其它类中,可以直接建立内部类对象。
格式:
外部类名.内部类名 变量名 = 外部类对象.内部类对象;
Outer.Inner in = new Outer().new Inner();
2,当内部类在成员位置上,就可以被成员修饰符所修饰。
比如,private:将内部类在外部类中进行封装。
static:内部类就具备了static的特性。
class Outer
{
private int x = 3;
static class Inner//静态内部类
{
void function()
{
System.out.println("inner :"+x);
}
}
运行结果:
当内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限。
在外部其它类中,如何直接访问static内部类的非静态成员呢?
new Outer.Inner().function();
class Outer
{
private static int x = 3;
static class Inner//静态内部类
{
void function()
{
System.out.println("inner :"+x);
}
}
void method()
{
System.out.println(x);
Inner in = new Inner();
in.function();
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
new Outer.Inner().function();
}
}
在外部其它类中,如何直接访问static内部类的静态成员呢?
Outer.Inner.function();
注意:
当内部类中定义了静态成员,该内部类必须是static的。
class Outer
{
private static int x = 3;
class Inner//静态内部类
{
static void function()
{
System.out.println("inner :"+x);
}
}
运行结果:
class Outer
{
private static int x = 3;
static class Inner//静态内部类
{
static void function()
{
System.out.println("inner :"+x);
}
}
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();
}
}
运行结果:
如果想访问Inner2里的show()呢?
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()
{
new Inner2.show();
}
}
当外部类中的静态方法访问内部类时,内部类也必须是static的。
当描述事物时,事物的内部还有事物,该事物用内部类来描述。因为内部事物在使用外部事物的内容。
例子:
描述人体,由器官组成。
描述心脏的时候,心脏比较复杂,具备的功能不唯一,包含各种属性和行为,所以要对其封装,用类描述。
心脏要访问人体里面的东西:
class Body
{
private class Xinzang//不需要对外暴露
{
}
}
记住:内部类只有在定义在外部类成员位置上,才能被静态或私有修饰,一般内部类不会被public修饰。
内部类可以被定义在外部类的任意位置:
class Outer
{
int x = 3;
void method()
{
class Inner//不能被再被修饰了,因为定义在局部了
{
void function()//局部内部类是不能定义静态成员的
{
System.out.println(x);
}
}new Inner().function();//要不然没对象,执行method()时,inner不会执行。
}
}
class InnerClassDemo3
{
public static void main(String[] args)
{
new Outer().method();
}
}
运行正确。
注意:这样不行:
class Outer
{
int x = 3;
void method()
{
new Inner().function();
class Inner
{
void function()
{
System.out.println(x);
}
}
}
}
class InnerClassDemo3
{
public static void main(String[] args)
{
new Outer().method();
}
}
运行结果:
class Outer
{
int x = 3;
void method()
{
int y = 4;
class Inner
{
void function()
{
System.out.println(y);
}
}
new Inner().function();
}
}
class InnerClassDemo3
{
public static void main(String[] args)
{
new Outer().method();
}
}
运行结果:
需要声明为:
final int y = 4;
内部类定义在局部时,
1,不可以被成员修饰符修饰
2,可以直接访问外部类中的成员,因为还持有外部类中的引用。但是不可以访问它所在的局部中的变量,只能访问被final修饰的局部变量。
class Outer
{
int x = 3;
void method(final int a)
{
int y = 4;
class Inner
{
void function()
{
System.out.println(y);
}
}
new Inner().function();
}
}
class InnerClassDemo3
{
public static void main(String[] args)
{
Outer out = new Outer().method();
out.method(7);
out.method(8);
}
}
这样是可以的,虽然是final int a,但是它是局部变量,method(final int a)是方法,执行完就在内存中消失了。
匿名内部类
1,匿名内部类其实就是内部类的简写格式。
2,定义匿名内部类的前提:内部类必须继承一个类或者实现接口。
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();
}
}
class InnerClassDemo4
{
public static void main(String[] args)
{
new Outer().function();
}
}
运行结果:
简化成匿名内部类:
abstract class AbsDemo
{
abstract void show();
}
class Outer
{
int x = 3;
/*
class Inner extends AbsDemo
{
void show()
{
System.out.println("show :"+num);
}
}
*/
public void function()
{
//匿名内部类
//创建对象后,对象还带着内容
//是以上注释部分的简化写法
new AbsDemo()
{
void show()
{
System.out.println("x="+x);
}
}.show();//AbsDemo的匿名子类对象调研show()方法。
}
}
匿名内部类的格式:
new父类或者接口(){定义子类的内容}
其实匿名内部类就是一个匿名子类对象。而且这个对象有点胖。可以理解为带内容的对象。
abstract class AbsDemo
{
abstract void show();
}
class Outer
{
int x = 3;
/*
class Inner extends AbsDemo
{
void show()
{
System.out.println("show :"+num);
}
}
*/
public void function()
{
//匿名内部类
//创建对象后,对象还带着内容
//是以上注释部分的简化写法
AbsDemo d = new AbsDemo()//多态
{
void show()
{
System.out.println("x="+x);
}
void abc()
{
System.out.println("haha");
}
};
d.show();
//d.abc();不行,父类引用只能调用父类方法
}
}
class InnerClassDemo4
{
public static void main(String[] args)
{
new Outer().function();
}
}
匿名内部类中定义的方法最好不要超过3个。
练习:
interface Inter
{
void method();
}
class Test
{
//补足代码。通过匿名内部类。
static class Inner implements Inter
{
public void method()
{
System.out.println("method run");
}
}
static Inter function()
{
return new Inner()
}
}
class InnerClassTest
{
public static void main(String[] args)
{
Test.function().method();
}
}
匿名:
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().method();
}
}
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");
}
});
异常
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="+x);
System.out.println("over");
}
}
运行结果:
异常:就是程序在 运行是出现的不正常情况。
异常的由来:问题也是现实生活中一个具体的事物,也可以通过java类的形式进行描述,并封装成对象。
其实就是java对不正常情况进行描述后的对象体现。
把问题封装成对象就是异常。
对于问题的划分,两种:一种是严重的问题,一种是非严重的问题。
对于严重的,java通过Error类进行描述。对于Error,一般不编写针对性的代码对其进行处理。
对于非严重的,java通过Exception类进行描述。对于Exception可以使用针对性的处理方式进行处理。
Error举例:NoClassDefFoundError,OutofMemoryError
无论Error或者Exception都具有一些共性内容。比如:不正常情况的信息,引发原因等:
Throwable
|--Error
|--Exception
异常的处理
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);
System.out.println("x="+x);
}
catch (Exception e)
{
System.out.println("除零啦");
}
System.out.println("over");
}
}
运行结果:
对捕获到的异常对象进行常见方法操作
String getMessage():获取异常信息。
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);
System.out.println("x="+x);
}
catch (Exception e)
{
System.out.println("除零啦");
System.out.println(e.getMessage());
System.out.println(e.toString());
}
System.out.println("over");
}
}
运行结果:
e.printStackTrace();//异常名称,异常信息,异常出现的位置。其实jvm默认的异常处理机制,就是在调用printStackTrace()方法。打印异常的堆栈中的跟踪信息
throws关键字
class Demo
{
int div(int a, int b) throws Exception//在功能上通过throws关键字声明了该功能有可能会出现问题。
{
return a/b;
}
}
class ExceptionDemo
{
public static void main(String[] args)
{
Demo d = new Demo();
int x = d.div(4,1);
System.out.println("x="+x);
System.out.println("over");
}
}
运行结果:
class Demo
{
int div(int a, int b) throws Exception//在功能上通过throws关键字声明了该功能有可能会出现问题。
{
return a/b;
}
}
class ExceptionDemo
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,1);
System.out.println("x="+x);
}
catch (Exception e)
{
System.out.println(e.toString());
}
System.out.println("over");
}
}
在函数上声明异常,便于提高安全性,让调用进行处理,不处理编译失败。
对多异常的处理
1,声明异常时,建议声明更为具体的异常,这样处理得可以更具体。
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 ExceptionDemo
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,1);
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("角标越界啦!!");
}
System.out.println("over");
}
}
不可能两个异常同时发生,异常发生的时候,程序就结束了。
2,对方声明几个异常,就对应有几个catch块。如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。不要定义多余的catch块
建议在进行catch处理时,catch中一定要定义具体处理方式。不要简单定义一句e.printStackTrace(),也不要简单书写一条输出语句。
自定义异常
因为项目中会出现特有的问题,而这些问题并未被java所描述并封装对象。所以对于这些特有的问题可以按照java的对问题封装的思想,将特有的问题,进行自定义的异常封装。
自定义异常:
举例:
需求:在本程序中,对于除数是负数,也视为是错误的,是无法进行运算的,那么就需要对这个问题进行自定义的描述。
class FuShuException extends Exception
{
}
class Demo
{
int div(int a, int b)
{
if (b<0)
{
throw new FuShuException();//手动通过throw关键字抛出一个自定义异常对象。
}
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args)
{
Demo d = new Demo();
int x = d.div(4,1);
System.out.println("x="+x);
System.out.println("over");
}
}
运行结果:
当在函数内部出现了throw抛出异常对象,那么就必须要给出对应的处理动作。
要么在内部try catch处理,要么在函数上声明让调用者处理。
一般情况下,函数内出现异常,函数上需要声明:
<pre name="code" class="java">class FuShuException extends Exception
{
}
class Demo
{
int div(int a, int b) throws FuShuException
{
if (b<0)
{
throw new FuShuException();//手动通过throw关键字抛出一个自定义异常对象。
}
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,-1);
System.out.println("x="+x);
}
catch (FuShuException)
{
System.out.println(e.toString());
System.out.println("除数出现负数了");
}
System.out.println("over");
}
}
运行结果:
发现打印的结果中只有异常的名称,却没有异常的信息。因为自定义的异常并未定义信息。
如何定义异常信息呢?
首先:
class Throwable
{
private String message;
Throwable(String message)
{
this.message = message;
}
public String getMessage()
{
return message;
}
}
class Exception extends Throwable
{
Exception(String message)
{
super(message);
}
}
class Person
{
String name;
Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
class Student extends Person
{
Student (String name)
{
super(name);
}
}
new Sttdent("lisi").getName();
所以直接super(msg);就可以了:
class FuShuException extends Exception
{
FuShuException(String msg)
{
super(msg);
}
}
class Demo
{
int div(int a, int b) throws FuShuException
{
if (b<0)
{
throw new FuShuException("出现了除数是负数的情况------ / by fushu");//手动通过throw关键字抛出一个自定义异常对象。
}
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,1);
System.out.println("x="+x);
}
catch (FuShuException)
{
System.out.println(e.toString());
System.out.println("除数出现负数了");
}
System.out.println("over");
}
}
因为父类中已经把异常信息的操作都完成了,所以子类只要在构造时,将异常信息传递给父类通过super语句,那么就可以直接通过getMessage方法获取自定义的异常信息。
传递异常特有的值:
class FuShuException extends Exception
{
private int value;
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("出现了除数是负数的情况------ / by fushu",b);//手动通过throw关键字抛出一个自定义异常对象。
}
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,1);
System.out.println("x="+x);
}
catch (FuShuException)
{
System.out.println(e.toString());
System.out.println("错误的负数是 "+e.getValue());
}
System.out.println("over");
}
}
现在可以把错误的负数打印出来了。
自定义异常:
必须是自定义类继承Exception。
继承Exception原因:异常体系有一个特点:异常类和异常对象都需要被抛出。他们都具备可抛性,这个可抛性是Throwable这个体系中的独有特点,只有这个体系中的类和对象才可以被throws和throw操作。
throws和throw的区别:throws使用在函数上(小括号和大括号之间),throw使用在函数内。throws后面跟的是异常类,可以跟多个,用逗号隔开。throw后面跟的是异常对象。
123
RuntimeException
class Demo
{
int div(int a,int b)
{
if (b==0)
{
throw new ArithmeticException("被零除啦");
return a/b;
}
}
}
class ExceptionDemo4
{
public static void main(String[] args)
{
Demo d = new Demo();
int x = d.div(4,0);
System.out.println("x="+x);
System.out.println("over");
}
}
Exception中有一个特殊的子类异常RuntimeException运行时异常,如果在函数内抛出异常,函数上可以不用声明,编译一样通过。如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过。
注意:之所以不用在函数上声明,是因为不需要让调用者处理。当该异常发生,希望程序停止,因为在运行时,出现了无法继续运算的情况,希望停止程序后,由程序员对代码进行修正。
NullPointerException:
class Person
{
public void checkName(String name)
{
//if(name.equals("lisi"))//NullPointerException
if("lisi".equals(name))//if(name!=null && name.equals("lisi"))
System.out.println("YES");
else
System.out.println("no");
}
}
main()
{
Person p = new Person();
p.checkName(null);
}
class FuShuException extends RuntimeException
{
FuShuException(String msg)
{
super(msg);
}
}
class Demo
{
int div(int a,int b)throws Exception//throws ArithmeticException
{
if(b<0)
throw new Exception("出现了除数为负数了");
if(b==0)
throw new ArithmeticException("被零除啦");
return a/b;
}
}
class ExceptionDemo4
{
public static void main(String[] args)
{
Demo d = new Demo();
int x = d.div(4,-9);
System.out.println("x="+x);
System.out.println("over");
}
}
对于异常分两种:
1,编译时被检测的异常。
2,编译时不被检测的异常(运行时异常,RuntimeException以及其子类)
-------android培训、java培训、java学习型技术博客、期待与您交流! ----------