package com.cn.test4;
public class Inherit {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=0;int b=9;int c=2;
try{
c=b/a;
}catch(Exception e){
//System.out.println("ninhao");
c=9;
}
System.out.println(c);
circle cir=new circle();
triangle tri=new triangle();
line lin=new line();
dosomthing(cir);
dosomthing(tri);
dosomthing(lin);
}
static public void dosomthing(shape shape){
shape.erase();
shape.draw();
}
}
class shape{
public void erase(){
System.out.println("shape i can erase");
}
public void draw(){
System.out.println("shape i can draw");
}
}
class circle extends shape{
public void erase(){
System.out.println("circle i can erase");
}
public void draw(){
System.out.println("circle i can draw");
}
}
class triangle extends shape{
public void erase(){
System.out.println("triangle i can erase");
}
public void draw(){
System.out.println("triangle i can draw");
}
}
class line extends shape{
public void erase(){
System.out.println("line i can erase");
}
public void draw(){
System.out.println("line i can draw");
}
}
多态
最新推荐文章于 2025-04-15 21:42:29 发布