1.
public class SimpleThread extends Thread{
SimpleThread(String s){
super(s);
}
public void run(){
String name = this.getName();
for(int i = 1 ; i <= 5 ; i++){
System.out.println(name + "第" + i + "次输出!");
try {
sleep((int)(Math.random()*1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String [] args){
SimpleThread s1 = new SimpleThread("线程1");
SimpleThread s2 = new SimpleThread("线程2");
s1.start();
s2.start();
}
}
2.
class People {
double height,weight;
public void speakHello(){}
public void qverageHeight(){}
public void averageWeight(){}
}
class ChinaPeople extends People{
public void chinaGongu(){}
public void averageWeight(){
System.out.println("chinese weight!");
}
public void speakHello(){
System.out.println("chinees speaking!");
}
public void qverageHeight(){
System.out.println("chinese heght!");
}
}
class AmericanPeople extends People{
public void americanBoxing(){}
public void averageWeight(){
System.out.println("AmericanPeople weight!");
}
public void speakHello(){
System.out.println("AmericanPeople speaking!");
}
public void qverageHeight(){
System.out.println("AmericanPeople heght!");
}
}
class BeijingPeople extends People{
public void beijingOpera(){}
public void averageWeight(){
System.out.println("BeijingPeople weight!");
}
public void speakHello(){
System.out.println("BeijingPeople speaking!");
}
public void qverageHeight(){
System.out.println("BeijingPeople heght!");
}
}
3.
class StringException extends Exception{
String s;
StringException(String s){
this.s = s;
}
String getS(){
return s;
}
}
class TestString{
public TestString(String s) throws StringException{
if(s.equals("haha")){
throw new StringException(s);
}
}
}
public class ExampleException {
public static void main(String [] args){
try{
TestString ts = new TestString("haha");
}
catch(StringException se){
System.out.println("输入的数据有异常,不能取值"+se.getS());
}
finally{
System.out.println("finally 被运行!");
}
}
}
温馨提示:答案为网友推荐,仅供参考