日期:2014-05-20 浏览次数:20949 次
public class Test
{
public Test(String iceboy)
{
System.out.println(iceboy);
}
public void Test(String iceboy)
{
System.out.println(iceboy);
}
public static void main(String[] args)
{
Test a = new Test("abc");//输出“abc”
a.Test("iceboy");//输出“iceboy”
}
}
public class Test
{
public static void main(String[] args)
{
ThreadClass t = new ThreadClass();
t.start();
System.out.println("end");//输出“end”
}
}
class ThreadClass extends Thread //Thread类已经实现了空的run()方法。
{
}
第二种方法:实现Runnable接口
public class Test
{
public static void main(String[] args)
{
ThreadClass t = new ThreadClass();
Thread thread = new Thread(t);
thread.start();
System.out.println("end");
}
}
class ThreadClass implements Runnable
{
public void run() //必须有此方法否则编译报错。它是Runnable接口中的抽象方法。
{
System.out.println("Threads");
}
}
4.局部内部类是否可以访问非final变量?
答案:不能访问局部的,可以访问成员变量(全局的)。
class Out
{
private String name = "out.name";
void print()
{
final String work = "out.local.work";//若不是final的则不能被Animal 使用.
int age=10;
class Animal
//定义一个局部内部类.只能在print()方法中使用.
//局部类中不能使用外部的非final的局部变量.全局的可以.
{
public void eat()
{
System.out.println(work);//ok
//age=20;error not final
System.out.println(name);//ok.
}
}
Animal local = new Animal();
local.eat();
}
}
public class Test
{
public static void main(String[] args)
{
int a=1;
try
{
a=a/0;
}catch(Exception e)
{
System.out.println("catch");
return;//当return时,finally中的语句会执行。
//System.exit(0);//若用上这句,finally中的语句不会执行。直接返回,退出程序。
}
finally //当没有System.exit(0);时,无论是否发生异常它都会执行。
{
System.out.println("finally");
}
}
}