Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java习题解析.

Similar presentations


Presentation on theme: "Java习题解析."— Presentation transcript:

1 Java习题解析

2 1、Java的变量 Java中变量分为三类:局部变量、实例变量、类变量,如下程序所示:
public class Variable { static int allClicks = 0; //类变量 String str = “Hello”; //类的实例变量 public void method() { int i = 0; //类的局部变量 } } 类变量的定义前加有static,这表示它是一个静态变量,因此类的多个实例共用一个类变量。 实例变量定义在类的起始位置,每一个类的实例对象都独自拥有一份实例变量的拷贝。 局部变量的有效范围在程序块中。

3 分析下面的代码,编译是否通过,若编译通过输出结果是什么?
public class StaticDemo extends Thread{ static int total = 100; int num; public StaticDemo(int num){ this.num=num; } public void run() { try{ sleep(num); total=total+num; System.out.println(total); catch(Exception e){ System.out.println("EXCEPTION!"); public static void main(String[] args) { StaticDemo t1 = new StaticDemo(2000); StaticDemo t2 = new StaticDemo(1000); t1.start(); t2.start();

4 2、自定义异常类 import javax.swing.*; class MyException {
void count(int year){ try{ if(2007-year-16<4&&2007-year-16>=0){ System.out.println("您的保险费用为1000元!"); } else if(2007-year-16<0)throw new userexception("您的年龄太小了,请在父母指导下购买"); "); else { System.out.println("您必须支付保险费600元"); }catch(userexception k){ System.out.println(k.toString());

5 public static void main(String agrs[]){
try{ int year=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入出生年份")); MyException t1=new MyException(); t1.count(year); }catch(java.lang.NumberFormatException e){ System.out.println("您好!错误的输入格式:"+e.toString()); } class userexception extends Exception{ userexception(){} userexception(String str){ super(str);

6 3、内部类特性 内部类的类名只用于定义它的类或语句块之内,在外部引用它时必须给出带有外部类名的完整名称,并且内部类的名字不允许与外部包类的名字相同。 内部类可以访问外部类的static或实例成员变量 内部类可以在成员方法中定义,该成员方法的局部变量及参数必须是final的才能被该内部类使用 内部类可以是抽象类或接口,若是接口,则可以由其它内部类实现

7 3、内部类特性(续) 内部类可以被声明为static(普通类则不可以),这样的内部类变成顶层类,相当于把它放到外面,不再是嵌套的内部类。
只有顶层类可以声明static成员,如果内部类需要定义static成员,则该内部类必须声明为static。

8 内部类访问外包类成员 public class Outer{ private int size; /** 定义内部类Inner */
public class Inner{ //将外包类的成员变量size递增 public void doStuff(){ size++; } Inner i=new Inner(); //成员变量i指向Inner类的对象 public void increaseSize(){ i.doStuff(); //调用内部类Inner的方法 public static void main(String[] a){ Outer o=new Outer(); for (int i = 0; i<4; i++){ o.increaseSize(); System.out.println("The value of size : "+o.size);

9 内部类中加上修饰符访问同名外部包类成员 public class Outer{ private int size;
/** 定义内部类Inner */ public class Inner{ public void doStuff(int size){ size++; //存取局部变量 this.size++; //存取内部类的成员变量 Outer.this.size++; //存取其外包类Outer的成员变量 System.out.println("size in Inner.doStuff(): "+size); System.out.println("size of the Inner class: "+this.size); System.out.println("size of the Outer class: "+Outer.this.size); } Inner i=new Inner(); //成员变量i指向Inner类的对象 public void increaseSize(int s){ i.doStuff(s); //调用内部类Inner的方法 public static void main(String[] a){ Outer o=new Outer(); o.increaseSize(10);

10 在外包类的语句块中定义内部类 class Outer{ private int size=5;
/** 方法makeInner(),返回一内部类对象 */ public Object makeInner(final int finalLocalVar){ int LocalVar=6; class Inner{ public String toString(){ return ("#<Inner size="+size+ " finalLocalVar="+finalLocalVar+">"); } return new Inner(); //方法makeInner()返回一内部类对象 public static void main(String[] args){ Outer outer=new Outer (); Object obj=outer.makeInner(40); System.out.println("The object is "+obj.toString());

11 在外包类的其它类中访问内部类 class Outer{ private int size; class Inner{
void doStuff(){ size++; System.out.println("The size value of the Outer class: "+size); } public class TestInner{ public static void main(String[] a){ Outer out=new Outer(); Outer.Inner in=new out.Inner(); in.doStuff();


Download ppt "Java习题解析."

Similar presentations


Ads by Google