首页 > 应用开发 > IT综合 > 重写、覆盖、重载、多态几个概念的区别分析
举个例子:
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;
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31559985/viewspace-2639338/ ,如需转载,请注明出处,否则将追究法律责任。