JAVA学习.方法的重写

来源:互联网 发布:java python 比较 编辑:程序博客网 时间:2024/05/16 09:13

内存图:


示例代码:

package ljy.inheritance;public class Animal extends Object {String name;Integer age;public Animal() {super();}public Animal(String name, Integer age) {super();this.name = name;this.age = age;}void run(){System.out.println("一只"+this.age+"岁的"+ this.name + "在奔跑!~~");}void sound(){System.out.println("一只"+this.age+"岁的"+ this.name + "在吼叫!~~");}}

package ljy.inheritance;public class Tiger extends Animal{String furcolor;public Tiger(String name,Integer age,String furcolor){super(name,age);this.furcolor = furcolor;}void hunt(){System.out.println("一只"+this.furcolor+"的"+ this.name + "在捕猎!~~");}void sound(){System.out.print("我是一只老虎~~~~哦~");super.sound();}//子类对父类的签名方法是认可的,但是对内容不认可//子类决定重新写过一次,这种语法现象叫做重写(override)//1.颠覆性重写:完全舍弃父类方法,自己写方法//2.改良性重写:对父类方法进行补充性的说明(以上属于该类)}

/** *  */package ljy.inheritance;/** * @author fshxxxyydys * */public class Tester {/** * @param args */public static void main(String[] args) {Tiger t = new Tiger("东北虎",1,"白色");t.sound();}}

==============================================

Result:

我是一只老虎~~~~哦~一只1岁的东北虎在吼叫!~~

==============================================

原创出处:http://blog.csdn.net/u012830807


原创粉丝点击