类的重载是指子类的方法与父类的方法具有相同的方法名,但方法参数不同。
示例
<pre name="code" class="java">public class OverLoadTest {
@Test
public void test() {
Shape shape=new Shape();
Circle circle=new Circle();
shape.print(1);
circle.print(1,2);
}
}
class Shape
{
protected void draw() {
}
public void print(int x){
System.out.println(x+1);
}
}
class Circle extends Shape{
protected void draw(){
System.out.println("draw a circle");
}
public void print(int x,int y){
System.out.println(x+y);
}
}
class Square extends Shape{
protected void draw(){
System.out.println("draw a square");
}
}输出:
2
3
本文通过一个Java示例介绍了类方法重载的概念及其实现方式。子类继承父类并重载了父类的方法,通过不同的参数列表来实现方法的重载,展示了如何在子类中使用与父类相同名称但参数不同的方法。
446

被折叠的 条评论
为什么被折叠?



