java的重写(Override) (2013-10-11-163 写的日志迁移

/*
 *说明方法的重写(又称方法的覆盖)子类并不想原封不动地继承父类的方法,而是想作一定的修改 
 */
package czbk.jxy.study;
/**
 * @author Archon
 * @date 省略
 */
public class ChongXie {
	public static void main(String[] args) {
		Cat_1 cat=new Cat_1();
		cat.cry();
		Dog_1 dog=new Dog_1();
		dog.cry();
	}
}
/*先定义一个动物的父类*/
class Animal_1{
	int age;
	String name;
	//定义一个都会叫的方法
	public void cry(){
		 System.out.println("暂时未知此动物是怎样叫的!");
	}	
}
//定义猫类
class Cat_1 extends Animal_1{
	//覆盖父类的方法
	public void cry(){
		System.out.println("猫猫叫");
	}
}
//定义狗类
class Dog_1 extends Animal{
	//覆盖父类的方法
	public void cry(){
		System.out.println("汪汪叫");
	}
}

/*由此可见: 
 *重载重写都需要方法名相同; 
 *重载要求参数列表不同; 
 *重写要求参数列表、返回值都相同(只存在子类中)子类方法不能缩小父类方法的访问权限,反之则可以
 */

  

原文地址:https://www.cnblogs.com/jiang-xy/p/9340647.html