第一道:
class Person {
private String Name;
private int Age;
public void setName(String name){
this.Name=name;
}
public void setAge(int age){
this.Age=0;
}
public String getName(){
return this.Name;
}
public int getAge(){
return this.Age;
}
}
//子类Employee
class Employee extends Person{
private int empno;
public void setEmpno(int empno){
this.empno=empno;
}
public int getEmpno(){
return this.empno;
}
}
第二道:
class Point{
float x;
float y;
//无参构造方法
Point(){
this.x=0;
this.y=0;
}
//有参构造方法
Point(int x,int y){
this.x=x;
this.y=y;
}
public void show(){
System.out.println("("+this.x+","+this.y+")");
}
}
第三道:
//抽象父类
abstract class Shape{
abstract double area();
}
//子类Circle
class Circle extends Shape{
double r;
Circle(double r){
this.r=r;
}
public double area(){
return Math.PI*r*r;
}
}
//子类Rectangle
class Rectangle extends Shape{
double l;
double w;
Rectangle(double l,double w){
this.l=l;
this.w=w;
}
public double area(){
return l*w;
}
}
第四道:
//自定义的异常类
class NumberRangeException extends Exception{
NumberRangeException(){}
private static final long serialVersionUID = 4022505431566019850L;
}
//类SelfException,用于测试自定义的异常
public class SelfException{
static int selfExceptionTest(int a,int b)throws NumberRangeException{
//当两个数相加后,大于20或小于10,抛出自定义的异常
if(a+b<10||a+b>20) throw new NumberRangeException();
else return a+b;
}
public static void main(String[] args){
int x=10;
int y=20;
try{
System.out.println(x+"+"+y+"="+selfExceptionTest(x,y));
}
catch(NumberRangeException e){
System.out.println("数值越界异常:"+x+"+"+y);
}
}
}
第五道:
//线程类TeacherThread
class TeacherThread extends Thread{
public void run(){
for(int i=0;i<5;i++)
System.out.println("I am a teacher");
}
}
//线程类StrudentThread
class StrudentThread extends Thread{
public void run(){
for(int i=0;i<5;i++)
System.out.println("you are a student");
}
}
//类ThreadTest
public class ThreadTest {
public static void main(String[] args) {
TeacherThread tt=new TeacherThread();
StrudentThread st=new StrudentThread();
tt.start();
st.start();
}
}
本人也是初学Java,正好也当做练习了。
哇 高手啊 呵呵 看着蛮多的 顶下