override和overload

来源:互联网 发布:mac 修复flash player 编辑:程序博客网 时间:2024/04/27 10:03

override,覆盖,子类覆盖父类的方法

overload,重载,同类同一个方法参数不同

/* overload,重载 * 方法setAge(String str)和方法setAge(int age)就叫做重载 * 也就是说一个类中同一个方法的参数不同 */@SuppressWarnings("unused")class Animal {private int age;public void setAge(String str){this.age = Integer.parseInt(str);}public void setAge(int age){this.age = age;}}/* override,覆盖 * 子类将父类的方法重写,也就是覆盖 */class Dog extends Animal{@Overridepublic void setAge(int age) {System.out.println(age);}}


原创粉丝点击