Java之方法的重写

方法的重写指在子类中重新定义父类中已有的方法。

重写方法要注意下面的三点:

1、重写的方法和被重写的方法必须具有相同方法名称、参数列表和返回类型;

2、子类中不允许出现与父类同名同参但不同返回值的方法;

3、重写方法不能使用比被重写的方法更严格的访问权限。

class Human
{
	private int age;
	private String name;
	
	public Human()
	{
		
	}
	public Human(int age, String name)
	{
		this.age = age;
		this.name = name;
	}
	public String getInformation()
	{
		String infor = name + ":" + age;
		return infor;
	}
}
class Student extends Human
{
	private String school;
	
	public Student(int age, String name, String school)
	{
		super(age, name);
		this.school = school;
	}
	public String getInformation()
	{
		String infor = super.getInformation() + ":" + school;
		return infor;
	}
	
}
public class TestOverride
{
	public static void main(String[] args)
	{
		Student st1 = new Student(22, "李四", "重庆");
		System.out.printf("%s\n", st1.getInformation());
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值