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

相关推荐
一只特立独行的猪6111 小时前
Java面试——集合篇
java·开发语言·面试
大得3692 小时前
go注册中心Eureka,注册到线上和线下,都可以访问
开发语言·eureka·golang
讓丄帝愛伱2 小时前
spring boot启动报错:so that it conforms to the canonical names requirements
java·spring boot·后端
weixin_586062022 小时前
Spring Boot 入门指南
java·spring boot·后端
小珑也要变强3 小时前
队列基础概念
c语言·开发语言·数据结构·物联网
Dola_Pan5 小时前
Linux文件IO(二)-文件操作使用详解
java·linux·服务器
wang_book5 小时前
Gitlab学习(007 gitlab项目操作)
java·运维·git·学习·spring·gitlab
AI原吾5 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
机器视觉知识推荐、就业指导5 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt
毕设木哥5 小时前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设