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

相关推荐
dhxhsgrx39 分钟前
PYTHON训练营DAY25
java·开发语言·python
不知几秋2 小时前
数字取证-内存取证(volatility)
java·linux·前端
风逸hhh3 小时前
python打卡day25@浙大疏锦行
开发语言·python
刚入门的大一新生3 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
chxii5 小时前
5java集合框架
java·开发语言
老衲有点帅5 小时前
C#多线程Thread
开发语言·c#
C++ 老炮儿的技术栈5 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
IsPrisoner5 小时前
Go语言安装proto并且使用gRPC服务(2025最新WINDOWS系统)
开发语言·后端·golang
Python私教5 小时前
征服Rust:从零到独立开发的实战进阶
服务器·开发语言·rust