zoukankan      html  css  js  c++  java
  • 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
    ", st1.getInformation());
    		
    	}
    }
    
  • 相关阅读:
    RGB色彩模式
    淘宝console
    倒计时
    放大镜效果
    谢谢你乱码
    [Linux Deploy]镜像扩展容量
    [Linux Deploy]SD 卡挂载问题
    [Linux Deploy]安装PHP环境
    [Linux Deploy]安装Dotnet Core 环境
    [Linux Deploy]安装MYSQL 设置自启动
  • 原文地址:https://www.cnblogs.com/yzy-blogs/p/5767365.html
Copyright © 2011-2022 走看看