这篇文章叙述了个人认为比较难理解的几个知识点,需要加以大量实践练习,以下记录这几个知识点要点,附带代码实例。
多态
abstract class Animal
{
public abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println("吃鱼");
}
public static void catchMouse()
{
System.out.println("抓老鼠");
}
}
class Demo
{
public static void main(String[] args)
{
Animal a = new Cat();
a.eat();
Cat c = (Cat)a;
c.catchMouse();
}
}
// 接口PCI
interface PCI
{
void open();
void close();
}
//网卡实现接口
class NetCard implements PCI
{
public void open()
{
System.out.println("NetCard_open");
}
public void close()
{
System.out.println("NetCard_close");
}
}
//声卡实现接口
class SoundCard implements PCI
{
public void open()
{
System.out.println("SoundCard_open");
}
public void close()
{
System.out.println("SoundCard_close");
}
}
class Mainboard
{
public static void run()
{
System.out.println("Mainboard_run");
}
public static void usePCI(PCI p)//PCI p = new NetCard()
{
if(!(p==null))
{
p.open();
p.close();
}
}
}
class Demo
{
public static void main(String[] args)
{
Mainboard m =new Mainboard();
m.run();
// m.usePCI(null);
m.usePCI(new NetCard());
m.usePCI(new SoundCard());
}
}
内部类
内部类可以直接访问外部类中的成员,包括私有。而外部类要访问内部类,必须建立内部类对象。
当内部类在外部类中的成员位置上时,可以被成员修饰符所修饰。需要注意的是:
1)当内部类中定义了静态成员时,该内部类必须是static的。
2)当外部类中的静态方法访问内部类时,内部类也必须是static的。
3)在实际应用中,内部类通常被定义为private,而很少定义为public。
而当内部类定义在局部时,
1)不可以被成员修饰符修饰。如public、private、static等修饰符修饰。它的作用域被限定在了声明这个局部类的代码块中
2)可以直接访问外部类中的成员,因为还持有外部类中的引用。
也需要注意:内部类不可以访问它所在的局部中非最终变量。只能访问被final修饰的局部变量。
下面是代码示例:
class Outer
{
int x = 3;
void method(final int a)
{
final int y = 4;
//局部内部类
class Inner
{
void function()
{
System.out.println(y);
}
}
new Inner().function();//使用局部内部类中的方法。
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
Outer out = new Outer();
out.method(7);
out.method(8);
}
}
这部分不难理解,不过是一个简化书写,覆盖方法的高效率的用法。
下面是代码示例
interface Inter
{
void method();
}
class Test
{
static Inter function()
{
return new Inter()
{
public void method()
{
System.out.println("Inner class Demo");
}
};
}
}
class InnerClassDemo
{
public static void main(String[] args)
{
Test.function().method();
}
}
异常
也就是说可以被throw和throws关键字所操作。
只有异常体系具备这个特点。
try
{
需要被检测的代码。
}
catch(异常类 变量)
{
处理异常的代码;(处理方式)
}
finally
{
一定会执行的语句;
}
为提高安全性,保证有异常程序编译不会通过,则用throws关键字声明异常。
关于throws和throw的区别:
throw定义在函数内,用于抛出异常对象。
throws定义在函数上,用于抛出异常类,可以抛出多个用逗号隔开。
1、问题在内部被解决就不需要声明。
2、catch是用于处理异常。如果没有catch就代表异常没有被处理,如果该异常是检测时异常。那么必须声明。
3、在子父类覆盖时:
a,子类抛出的异常必须是父类的异常的子类或者子集。
b,如果父类或者接口没有异常抛出时,子类覆盖出现异常,只能try不能抛。
/*
老师使用电脑讲课。
描述电脑:
1、电脑运行
2、电脑重启
描述电脑问题:
1、电脑蓝屏了
2、电脑起火了
描述老师:
1、老师使用电脑
2、老师讲课。
描述老师可能出现的问题:
1、老师不能继续讲课了,他让同学们自己做练习。
*/
//电脑蓝屏
class BlueScreenException extends Exception
{
BlueScreenException(String message)
{
super(message);
}
}
//电脑起火
class FireBreakingException extends Exception
{
FireBreakingException(String message)
{
super(message);
}
}
//老师无法继续上课
class StopTeachException extends Exception
{
StopTeachException(String message)
{
super(message);
}
}
class Computer
{
int start=1;
//电脑启动
void run()throws BlueScreenException,FireBreakingException
{
if(start==2)
throw new BlueScreenException("Computer_BlueScreen");
else if(start==3)
throw new FireBreakingException("Computer_FireBreaking");
System.out.println("Computer_run");
}
//电脑重启
void reset()
{
start=1;
System.out.println("Computer_reset");
}
}
class Teacher
{
private String name;
private Computer cpt;
//对老师进行初始化
Teacher(String name)
{
this.name=name;
cpt=new Computer();
}
//老师开始讲课
public void teach()throws StopTeachException
{
try
{
cpt.run();
}
catch (BlueScreenException e)
{
//System.out.println(e.getMessage());
cpt.reset();
}
catch (FireBreakingException e)
{
test();
//System.out.println(e.getMessage());
throw new StopTeachException("Teather_StopTeach:"+e.getMessage());
}
System.out.println(name+"Teacher_teaching");
}
void test()
{
System.out.println("学生做练习");
}
}
class ExceptionTest
{
public static void main(String[] args)
{
Teacher t=new Teacher("毕老师");
try
{
t.teach();
}
catch (StopTeachException e)
{
System.out.println(e.toString());
System.out.println("换老师或者放假");
}
}
}