面试的时候记得A(B,a)吧

来源:互联网 发布:百度关键词优化 编辑:程序博客网 时间:2024/06/03 12:36
public class Shape    
{   
   public static void main(String[] args){   
     Triangle tri = new Triangle();   
     System.out.println("Triangle is a type of shape? " + tri.isShape());// 继承   
     Shape shape = new Triangle();   
     System.out.println("My shape has " + shape.getSides() + " sides."); // 多态   
     Rectangle Rec = new Rectangle();   
     Shape shape2 = Rec;   
     System.out.println("My shape has " + shape2.getSides(Rec) + " sides."); //重载   
   }   
   public boolean isShape(){   
     return true;   
   }   
   public int getSides(){   
     return 0 ;   
   }   
   public int getSides(Triangle tri){ //重载   
     return 3 ;   
   }   
   public int getSides(Rectangle rec){ //重载   
    return 4 ;   
   }   
}   
class Triangle extends Shape    
{   
   public int getSides() { //重写,实现多态   
     return 3;   
   }   
}   
class Rectangle extends Shape    
{   
   public int getSides(int i) { //重载   
    return i;   
   }   
}
原创粉丝点击