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博客

相关推荐
科兴第一吴彦祖6 分钟前
基于Spring Boot + Vue 3的乡村振兴综合服务平台
java·vue.js·人工智能·spring boot·推荐算法
ajassi20007 分钟前
开源 java android app 开发(十八)最新编译器Android Studio 2025.1.3.7
android·java·开源
纤瘦的鲸鱼11 分钟前
Spring Gateway 全面解析:从入门到进阶实践
java·spring·gateway
用户32941900421625 分钟前
Java接入DeepSeek实现流式、联网、知识库以及多轮问答
java
Knight_AL29 分钟前
浅拷贝与深拷贝详解:概念、代码示例与后端应用场景
android·java·开发语言
DolphinScheduler社区31 分钟前
# 3.1.8<3.2.0<3.3.1,Apache DolphinScheduler集群升级避坑指南
java·大数据·开源·apache·任务调度·海豚调度
枫叶丹431 分钟前
【Qt开发】输入类控件(六)-> QDial
开发语言·qt
思考的笛卡尔1 小时前
Go语言实战:高并发服务器设计与实现
服务器·开发语言·golang
Le1Yu1 小时前
黑马商城微服务项目准备工作并了解什么是微服务、SpringCloud
java·微服务·架构
ZhengEnCi1 小时前
🚀创建第一个 SpringBoot 应用-零基础体验开箱即用的神奇魅力
java·spring boot