(三)接口练习:
编译失败:因为A接口中未定义func方法;
向上转型:父类引用指向子类实例化对象,父类引用只能调用子类继承父类的功能;
===============================================================
(四)子父类覆盖(向上转型与向下转型):
结果:编译成功
A
B
===============================================================
(五)接口的覆盖:
编译失败:因为A接口中没有定义test方法
===============================================================
(六)子父类覆盖:
结果:编译通过
B C 5
===============================================================
(七)匿名内部类
===============================================================
(八)内部类:
编译失败:非静态内部类不可以定义静态成员;(本题关键)
内部类可以访问外部类中的所有拥有不同访问权限的属性和方法;
静态内部类只能访问外部类的静态属性和静态方法;
内部类中如果定义了静态成员,该内部类必须被静态修饰
===============================================================
(九)子父类成员变量
4
5
showZi
showZi
===============================================================
(十)子父类的覆盖
编译失败:因为子类没有覆盖父类中的方法,但是子类调用的时候不能明确返回的值是什么类型
这样的函数不能存在于子父类中。
===============================================================
(十一)
编译失败:try中的语句抛出异常后,无法访问输出语句,出现异常
注意:throw单独存在,下面不要定义语句,因为执行不到(类似:continue,break)
===============================================================
(十二)内部类的调用
A.在位置一: new Inner();//正确
B.在位置二: new Inner();//错误
C.在位置三: new d.Inner();//错误,格式错误,d.new Inner();
D.在位置四: new Demo.Inner();//错误,因为inner不是静态的 new Demo().new Inner();
===============================================================
(十三)子父类异常
编译失败:多个catch时,父类的catch要放在最下面
错误:已捕获到异常错误 Exc0 e(320行)
===============================================================
(十四)匿名内部类
===============================================================
(十五)静态成员变量与异常
class Test
{
public static String output="";
public static void foo(int i)
{
try
{
if(i==1)
throw new Exception();
output+="1";
}
catch (Exception e)
{
output+="2";
return;
}
finally
{
output+="3";
}
output+="4";
}
public static void main(String[] args)
{
foo(0);
System.out.println(output);
foo(1);
System.out.println(output);
}
}
结果:"134"
"13423",output为静态成员变量(类变量),存储在方法区内
===============================================================
(十六)
建立一个图形接口,声明一个面积函数,圆形和矩形都实现这两个接口,并得出两个图形的面积
注:体现面向对象的特征,对数值进行判断,用异常进行处理,不合法的数值出现非法提示,不再进行运算
interface A
{
}
class B implements A
{
public String func()
{
return "func";
}
}
class Test
{
public static void main(String[] args)
{
A a=new B();
System.out.println(a.func());
}
}
编译失败:因为A接口中未定义func方法;
向上转型:父类引用指向子类实例化对象,父类引用只能调用子类继承父类的功能;
===============================================================
(四)子父类覆盖(向上转型与向下转型):
class Fu
{
boolean show(char a)
{
System.out.println(a);
return true;
}
}
class Test extends Fu
{
public static void main(String[] args)
{
int i=0;
Fu f=new Test();
Test t=new Test();
for(f.show('A');f.show('B')&&(i<2);f.show('c'))
{
i++;
t.show('D');
}
}
public boolean show(char a)
{
System.out.println(a);
return false;
}
}
结果:编译成功
A
B
===============================================================
(五)接口的覆盖:
interface A
{
}
class B implements A
{
public String test()
{
return "yes";
}
}
class Demo
{
static A get()//A为方法的返回值类型
{
return new B();
}
public static void main(String[] args)
{
A a=get();//编译通过,向上转型,a引用指向子类B对象
System.out.println(a.test());
}
}
编译失败:因为A接口中没有定义test方法
===============================================================
(六)子父类覆盖:
class Super
{
int i=0;
public Super(String a)
{
System.out.println("A");
i=1;
}
public Super()
{
System.out.println("B");
i+=2;
}
}
class Demo extends Super
{
public Demo(String a)
{
//super();//默认调用无参构造函数
System.out.println("C");
i=5;
}
public static void main(String[] args)
{
int i=4;
Super d=new Demo("A");
System.out.println(d.i);
}
}
结果:编译通过
B C 5
===============================================================
(七)匿名内部类
interface Inter
{
void show(int a,int b);
void func();
}
class Test
{
public static void main(String[] args)
{
//实现多个方法(结果:4,5 调用函数!)
Inter in=new Inter()
{
public void show(int a,int b)
{
System.out.println(a+","+b);
}
public void func()
{
System.out.println("调用函数!");
}
};
in.show(4,5);
in.func();
----------------------
//实现一个方法(结果:调用函数!)
new Inter()
{
public void show(int a,int b)
{
System.out.println(a+","+b);
}
public void func()
{
System.out.println("调用函数!");
}
}.func();
}
}
===============================================================
(八)内部类:
class TD
{
int y=6;
class Inner
{
static int y=3;
void show()
{
System.out.println(y);
}
}
}
class Test
{
public static void main(String[] args)
{
TD.Inner in=new TD().new Inner();
in.show();
}
}
编译失败:非静态内部类不可以定义静态成员;(本题关键)
内部类可以访问外部类中的所有拥有不同访问权限的属性和方法;
静态内部类只能访问外部类的静态属性和静态方法;
内部类中如果定义了静态成员,该内部类必须被静态修饰
===============================================================
(九)子父类成员变量
class Fu
{
int num=4;
void show()
{
System.out.println("showFu");
}
}
class Zi extends Fu
{
int num=5;
void show()
{
System.out.println("showZi");
}
}
class Test
{
public static void main(String[] args)
{
Fu f=new Zi();
Zi z=new Zi();
System.out.println(f.num);
System.out.println(z.num);
f.show();
z.show();
}
}
4
5
showZi
showZi
===============================================================
(十)子父类的覆盖
class Super
{
public int get()
{
return 4;
}
}
class Test extends Super
{
public long get()
{
return 5;
}
public static void main(String[] args)
{
Super s=new Test();
system.out.println(s.get());
}
}
编译失败:因为子类没有覆盖父类中的方法,但是子类调用的时候不能明确返回的值是什么类型
这样的函数不能存在于子父类中。
===============================================================
(十一)
class Demo
{
public static void main(String[] args)
{
try
{
throw new Exception();
System.out.println("A");
}
catch (Exception e)
{
System.out.println("B");
}
finally
{
System.out.println("D");
}
}
public static void showExce() throws Exception
{
throw new Exception();
}
}
编译失败:try中的语句抛出异常后,无法访问输出语句,出现异常
注意:throw单独存在,下面不要定义语句,因为执行不到(类似:continue,break)
===============================================================
(十二)内部类的调用
class Demo
{
public void func()
{
//位置一
}
class Inner
{
}
public static void main(String[] args)
{
Demo d=new Demo();
//位置二
d.new Inner();
}
}
A.在位置一: new Inner();//正确
B.在位置二: new Inner();//错误
C.在位置三: new d.Inner();//错误,格式错误,d.new Inner();
D.在位置四: new Demo.Inner();//错误,因为inner不是静态的 new Demo().new Inner();
===============================================================
(十三)子父类异常
class Exc0 extends Exception{}
class Exc1 extends Exc0{}
class Demo
{
public static void main(String[] args)
{
try
{
throw new Exc1();
}
catch (Exception e)
{
System.out.println("Exception");
}
catch(Exc0 e)
{
System.out.println("Exc0");
}
}
}
编译失败:多个catch时,父类的catch要放在最下面
错误:已捕获到异常错误 Exc0 e(320行)
===============================================================
(十四)匿名内部类
interface Test
{
void func();
}
class Test
{
public static void main(String[] args)
{
//补足代码,匿名内部类
new Demo().show(new Test()
{
public void func()
{
}
});
}
void show(Test t)
{
t.func();
}
}
===============================================================
(十五)静态成员变量与异常
class Test
{
public static String output="";
public static void foo(int i)
{
try
{
if(i==1)
throw new Exception();
output+="1";
}
catch (Exception e)
{
output+="2";
return;
}
finally
{
output+="3";
}
output+="4";
}
public static void main(String[] args)
{
foo(0);
System.out.println(output);
foo(1);
System.out.println(output);
}
}
结果:"134"
"13423",output为静态成员变量(类变量),存储在方法区内
===============================================================
(十六)
建立一个图形接口,声明一个面积函数,圆形和矩形都实现这两个接口,并得出两个图形的面积
注:体现面向对象的特征,对数值进行判断,用异常进行处理,不合法的数值出现非法提示,不再进行运算
interface Print
{
double area();
}
class Exc extends RuntimeException
{
Exc(String msg)
{
super(msg);
}
}
class Circle implements Print
{
private double radius;
private static final double PI=3.14;
Circle(double radius)
{
this.radius=radius;
}
public double area()
{
if(this.radius<0)
throw new Exc("这个数值是非法的");
return PI*this.radius*this.radius;
}
}
class Rec implements Print
{
private double width;
private double height;
Rec(double width,double height)
{
this.width=width;
this.height=height;
}
public double area()
{
if(this.width<0||this.height<0)
throw new Exc("这个数值是非法的");
return this.width*this.height;
}
}
class Test
{
public static void main(String[] args) throws Exc
{
double val=0;
----------------------------
try
{
Circle c=new Circle(-1);
val=c.area();
System.out.println("面积:"+val);
}
catch (Exc e)
{
//System.out.println(e.toString());
//System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("面积:");
---------------------------
Circle c=new Circle(-1);
val=c.area();
System.out.println("面积:"+val);
}
}