Java 基础之方法的重写
方法重写的定义
在子类中可以根据需要对父类中继承来的方法进行改造,也称为方法的重写、覆盖。在执行程序时,子类方法经覆盖父类方法。
要求
1. 子类重写的方法必须和父类重写的方法具有相同的方法名称、参数列表
2.子类重写的方法返回值类型不能大于父类被重写的方法返回值类性
3.子类重写的方法访问权限不能小于父类被重写的方法的权限;子类不能重写父类中声明的private 权限的方法
4.子类方法抛出的异常不能大于父类被重写方法时异常
注意
子类与父类中同名同参的方法必须同时声明为非static的(即为重写),或者同时声明为static的(即为重写)。因为static属于类的,子方法无法覆盖父类的方法
示例父类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.chenxi.java; public class Person { String name ; int age; public Person(){ } public Person(String name, int age){ this .name= name; this .age = age; } public void eat(){ System.out.println( "吃饭" ); } public void wald( int distance){ System.out.println(name+ "走了" +distance+ "公里!" ); } public void show(){ System.out.println( "我是一个人" ); } public Object info(){ return null ; } } |
示例子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.chenxi.java; /* */ public class Student extends Person { String major; public Student (){ } public Student (String major){ this .major = major; } public void stuby(){ System.out.println( "学习 。专业" +major); } @Override //对父类方法的重写 public void eat() { System.out.println( "吃有营养的食物" ); } public String info(){ return null ; } public void wald( int distance){ System.out.println( "重写" ); } } |
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.chenxi.java; /* 方法的重写(override/overwrite) 1.重写:子类方法继承父类以后,可以对父类中同名同参的方法,进行覆盖操作 2.应用:重写以后,当创建子类对象以后。通过子类对象调用子父类中同名同参的方法时,实际执行的时子类重写父类后的方法 重写的规定 方法的声明 :权限修饰符 返回值类型 方法名(形参列表) throws 异常类型{ 方法体 } 约定俗称 :子类中的叫重写方法,父类中叫被重写的方法 子类重写的方法名和方法形参列表与被重写的方法名和形参列表形同 子类重写的方法的权限修饰符不小于父类重写的权限修饰符 子类不能重写父类中的private权限的方法 返回值类型: 父类被重写的方法的返回值类型是void,则子类重写的返回值类型也只能是void 父类被重写的方法返回值类型是A类性,则子类重写的方法返回值类型可以是A类或者A类的子类 父类被重写的方法返回值类型是基本数据类型,则子类冲写的方法返回值类型必须是型同的进本数据类型 子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型 */ public class PersonTest { public static void main(String[] args) { Student s1= new Student( "阅读" ); s1.stuby(); s1.eat(); s1.info(); } } 测试 学习 。专业阅读 吃有营养的食物 重写 Process finished with exit code 0 |
子类和父类中同名或者同参的方法要么都声明为非static的(考虑重写),要么都声明为static的(不是重写)
测试
父类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package com.chenxi.exer; public class Mankind { private int sex ; private int salary; public Mankind(){ } public Mankind( int sex, int salary){ this .sex=sex; this .salary=salary; } public void maOrWoman(){ if (sex== 1 ){ System.out.println( "man" ); } else if ( sex == 0 ){ System.out.println( "woman" ); } } public void employeed() { // if (salary == 1) { // System.out.println("man"); // } else if (salary == 0) { // System.out.println("woman"); // } else { // System.out.println("人妖"); // } // } if (salary == 0 ) { System.out.println( "no job" ); } else if (salary != 0 ) { System.out.println( "job" ); } } public void setSex( int sex) { this .sex = sex; } public void setSalary( int salary) { this .salary = salary; } public int getSalary() { return salary; } public int getSex() { return sex; } } |
子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.chenxi.exer; public class kids extends Mankind { private int yearsOld; public kids(){ } public kids( int yearsOld){ this .yearsOld=yearsOld; } public void setYearsOld( int yearsOld) { this .yearsOld = yearsOld; } public int getYearsOld() { return yearsOld; } public void employeed() { System.out.println( "kind should study and no job." ); } public void printAge(){ System.out.println( "I am" + yearsOld+ " years old" ); } } |
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.chenxi.exer; public class KidsTest { public static void main(String[] args) { kids somekid= new kids( 12 ); somekid.printAge(); somekid.setYearsOld( 4 ); somekid.setSex( 8 ); somekid.employeed(); } } 结果 I am12 years old kind should study and no job. |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏