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

相关推荐
胡乱儿起个名34 分钟前
《高阶函数:把函数当玩具传来传去》
开发语言·c++·算法
七七知享35 分钟前
开启 Python 编程之旅:基础入门实战班全解析
开发语言·python·程序人生·程序员·零基础·实战
repetitiononeoneday37 分钟前
java基础课程-springmvc课程
java·开发语言
Mr.每天进步一小步1 小时前
每天记录一道Java面试题---day39
java·jvm·面试
工业互联网专业1 小时前
基于springboot+vue的数码产品抢购系统
java·vue.js·spring boot·毕业设计·源码·课程设计·数码产品抢购系统
敖云岚1 小时前
【AI】SpringAI 第二弹:接入 DeepSeek 官方服务
java·人工智能·spring boot·后端·spring
purrrew1 小时前
【数据结构_6】双向链表的实现
java·数据结构·链表
古月居GYH1 小时前
嵌入式C语言高级编程:OOP封装、TDD测试与防御性编程实践
c语言·开发语言·tdd
nangonghen1 小时前
JAVA程序实现mysql读写分离并在kubernetes中演示
java·mysql·mybatis·读写分离
ghost1431 小时前
Python自学第1天:变量,打印,类型转化
开发语言·python·学习