该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
1、class Shape{
public void draw() { System.out.println(“draw Shape”); }
}
class Circle extends Shape{
public void draw() { System.out.println(“draw Circle”); }
}
class Rectangle extends Shape{
public void draw() { System.out.println(“draw Rectangle”); }
}
public class ShapeTest{
public static void main(String[] args){
Shape[] shapes=new Shape[3];
Shape[0]=new Shape();
Shape[1]=new Circle();
Shape[2]=new Rectangle ();
for(int i=0;i
shapes[i].draw();
}
}
2、class Person{
private static int count=0;
String name;
public Person(String name) {
this.name=name;
count++;
}
public void showData() {
System.out.println("姓名:"+name);
System.out.println("人数:"+count);
}
}
public class BookConstructor{
public static void main(String args[]) {
Person one=new Person ("黄容");
one.showData();
Person two=new Person ("郭靖");
two.showData();
}
}
3、 try{
int a=1,b=0;
System.out.println(a/=b);
}catch(ArithmeticException e){
System.out.println(“捕捉到一个算术异常”);
}finally {
System.out.println("此行总被执行");
}
4、
public class student {
String name;
int age;
student() {
this("default",20);
}
student(String a1,int a2) {
name=a1;
age=a2;
}
public static void main(String args[]){
student s1=new student();
System.out.println("name="+s1.name+", age="+s1.age);
}
}