Java SE入门及基础(32)

方法重写(Override

**1.**概念

来自官方的说明

An instance method in a subclass with the same signature(name, plus the
number and the type of its parameters) and return type as an instance method
in the superclass overrides the superclass's method.
子类中的一个成员方法与父类中的成员方法有相同的签名(方法名加上参数数量和参数类型)和返回值类 型的实例方法重写了父类的方法

**2.**如何使用方法重写

The ability of a subclass to override a method allows a class to inherit from
a superclass whose behavior is "close enough" and then to modify behavior as
needed. The overriding method has the same name, number and type of
parameters, and return type as the method that it overrides. An overriding
method can also return a subtype of the type returned by the overridden
method. This subtype is called a covariant return type.
子类重写方法的能力使类可以从行为 " 足够近 " 的父类继承,然后根据需要修改行为。重写方法与被重写的方法具有相同的名称,数量和参数类型,并且返回类型相同。重写方法还可以返回重写方法返回的类型的子类型。 此子类型称为协变返回类型。
When overriding a method, you might want to use the @Override annotation that
instructs the compiler that you intend to override a method in the
superclass. If, for some reason, the compiler detects that the method does
not exist in one of the superclasses, then it will generate an error.
重写方法时,您可能需要使用 @Override 注解,该注释指示编译器您打算重写父类中的方法。 如果由于某种原因,编译器检测到该方法在父类中不存在,则它将生成错误。
byte short int long float double
Byte Short Integer Long Float Double => 都是 Number 的子类

案例

几何图形都有面积和周长,不同的几何图形,面积和周长的算法也不一样。矩形有长和宽,通过长和宽能够计算矩形的面积和周长;圆有半径,通过半径可以计算圆的面积和周长。请使用继承相关的知识完成程序设计。

分析

a. 几何图形包含了矩形和圆。几何图形都有面积和周长,因此几何图形可以定义为一个类,里面包含了面积和周长的计算方法
b. 矩形是一种几何图形。矩形有长和宽,可以根据长和宽来计算面积和周长
c. 圆是一种几何图形。圆有半径,可以根据半径来计算面积和周长

代码实现

package com .we . inheritance . shape ;
/**
* 几何图形
*/
public class Shape {
/**
* 计算周长
* @return
*/
public Number calculatePerimeter (){
return 0 ;
}
/**
* 计算面积
* @return
*/
public Number calculateArea (){
return 0 ;
}
}
package com .we . inheritance . shape ;
/**
* 矩形
*/
public class Rectangle extends Shape {
private int width ;
private int length ;
public Rectangle ( int width , int length ) {
this . width = width ;
this . length = length ;
}
@Override
public Integer calculatePerimeter () {
return ( width + length ) * 2 ;
}
@Override
public Integer calculateArea () {
return width * length ;
}
}
package com .we . inheritance . shape ;
/**
* 圆
*/
public class Circle extends Shape {
private int radius ;
public Circle ( int radius ) {
this . radius = radius ;
}
@Override
public Double calculateArea () {
return Math . PI * radius * radius ;
}
@Override
public Double calculatePerimeter () {
return 2 * Math . PI * radius ;
}
}
package com .we . inheritance . shape ;
public class ShapeTest {
public static void main ( String [] args ) {
Shape s1 = new Rectangle ( 10 , 9 );
System . out . println ( s1 . calculatePerimeter ());
System . out . println ( s1 . calculateArea ());
Shape s2 = new Circle ( 5 );
System . out . println ( s2 . calculatePerimeter ());
System . out . println ( s2 . calculateArea ());
}
}
重写方法时访问修饰符的级别不能降低。

Java SE文章参考:Java SE入门及基础知识合集-CSDN博客

相关推荐
七七软件开发2 分钟前
打车小程序 app 系统架构分析
java·python·小程序·系统架构·交友
_祝你今天愉快7 分钟前
Java-JVM探析
android·java·jvm
学编程的司马光18 分钟前
Idea集成Jenkins Control插件,在IDEA中触发Jenkins中项目的构建
java·jenkins·intellij-idea
凹凸曼说我是怪兽y20 分钟前
python后端之DRF框架(上篇)
开发语言·后端·python
孟君的编程札记24 分钟前
别只知道 Redis,真正用好缓存你得懂这些
java·后端
幻雨様28 分钟前
UE5多人MOBA+GAS 番外篇:同时造成多种类型伤害,以各种属性值的百分比来应用伤害(版本二)
java·前端·ue5
l1t29 分钟前
修改DeepSeek翻译得不对的V语言字符串文本排序程序
c语言·开发语言·python·v语言
z樾38 分钟前
Sum-rate计算
开发语言·python·深度学习
爱吃小土豆豆豆豆1 小时前
登录校验一
java·大数据·数据库
热河暖男1 小时前
Spring Boot AI 极速入门:解锁智能应用开发
java·人工智能·spring boot·ai编程