当前位置:网站首页>接口和抽象类/方法学习 demo
接口和抽象类/方法学习 demo
2022-07-27 04:39:00 【你若不离不弃,我必生死相依】
实现抽象方法
抽象类:抽象类就是不能使用new方法进行实例化的类,即没有具体实例对象的类,抽象类有点类似于“模板”的作用,目的是根据其格式来创建和修改新的类,对象不能由抽象类直接创建,只可以通过抽象类派生出新的子类,再由其子类来创建对象,当一个类被声明为抽象类时,要在这个类前面加上修饰符abstract,在抽象类中的成员方法可以包括一般方法和抽象方法
抽象方法:抽象方法就是以abstract修饰的方法,这种方法只声明返回的数据类型,方法名称和所需要的参数,没有方法体,也就是说抽象方法只需要声明而不需要事先,当一个方法为抽象方法时,意味着这个方法必须被子类的方法所重写,否则其子类的该方法仍然是abstract的,而这个子类也必须是抽象的,即声明为abstract。
抽象类
理解:
1,当一个类没有具体的信息来描述一个对象时,则定义为抽象类。
特点:
1,抽象类不能被实例化。
2,有构造器(类都有构造器)
3,抽象类中可以没有抽象方法,抽象方法所在的类一定是抽象类
应用:模型化那些父类无法确定的全部实现,而是由子类提供具体实现的对象的类
抽象方法
1,抽象方法所在的类一定是抽象类
2,格式:没有方法体{}
3, 若子类继承抽象类,并重写了所有的抽象方法,则此类是一个实体类。
4,若没有重写所有的抽象方法,则此类一定为抽象类
5,不同的对象有不同的处理方法,那么在父类中定义具体的便没有意义
限制:
1,abstract不能用来修饰属性,构造器,private,final,static
2,构造器不能被重写
3,子类不能重写private修饰的方法
4,final修饰的不能重写
5,static修饰直接调用,如果用abstract修饰为抽象没有任何意义
构造函数 ,是一种特殊的方法。主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中。特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类型的不同来区分它们 即构造函数的重载。
接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明。一个类通过继承接口的方式,从而来继承接口的抽象方法。
接口并不是类,编写接口的方式和类很相似,但是它们属于不同的概念。类描述对象的属性和方法。接口则包含类要实现的方法。
除非实现接口的类是抽象类,否则该类要定义接口中的所有方法。
接口无法被实例化,但是可以被实现。一个实现接口的类,必须实现接口内所描述的所有方法,否则就必须声明为抽象类。另外,在 Java 中,接口类型可用来声明一个变量,他们可以成为一个空指针,或是被绑定在一个以此接口实现的对象。
详情请查看菜鸟
描述
已知抽象类Base中定义了calculate方法,该方法的计算过程依赖于sum()和avg(),而后两个方法均为抽象方法。要求定义Base的子类Sub类,并实现父类的抽象方法,使得main函数中的运算逻辑得以正确执行。
输入描述:
两个整数
输出描述:
两个整数的和除以两个整数的平均值(平均值为int类型,不考虑小数问题)
public static void main(String[] args) {
// Sub是需要你定义的子类
Base base = new Sub();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
base.setX(x);
base.setY(y);
System.out.println(base.calculate());
}
}
}
abstract class Base {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int calculate() {
if (avg() == 0) {
return 0;
} else {
return sum() / avg();
}
}
/** * 返回x和y的和 */
public abstract int sum();
/** * 返回x和y的平均值 */
public abstract int avg();
}
class Sub extends Base {
//write your code here......
}
答案解析:
因为父类的成员变量是private类型的,因此子类中只能通过父类的函数来访问。父类calculate方法调用了sum和avg方法,但是这两个方法都是抽象方法,在父类中没有定义,我们需要将其在子类中补全。
于是我们在子类中重写sum和avg方法,调用父类的方法getX和getY获取变量值后计算求和与均值。
public static void main(String[] args) {
// Sub是需要你定义的子类
Base base = new Sub();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
base.setX(x);
base.setY(y);
System.out.println(base.calculate());
}
}
}
abstract class Base {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int calculate() {
if (avg() == 0) {
return 0;
} else {
return sum() / avg();
}
}
/** * 返回x和y的和 */
public abstract int sum();
/** * 返回x和y的平均值 */
public abstract int avg();
}
class Sub extends Base {
@Override
public int sum() {
System.out.println("和"+ (getX()+getY()));
return getX()+getY();
}
@Override
public int avg() {
System.out.println("平均值"+ (getX()+getY())/2);
// return (getX()+getY())/2;
return sum()/2;
}
结果为
5
6
平均值5
和11
和11
平均值5
和11
2
实现接口
描述
已知接口Comparator,内部定义了max函数,用于返回两个整数中的最大值。请定义该接口的实现类,使得main方法中的比较逻辑可以正确执行,要求实现类的名称为ComparatorImpl。
输入描述:
两个整数
输出描述:
两个整数中的最大值
public static void main(String[] args) {
Comparator comparator = new ComparatorImpl();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
System.out.println(comparator.max(x, y));
}
}
}
interface Comparator {
/** * 返回两个整数中的最大值 */
int max(int x, int y);
}
答案解析:
ComparatorImpl实现类重写max方法
public static void main(String[] args) {
Comparator comparator = new ComparatorImpl();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
System.out.println(comparator.max(x, y));
}
}
}
interface Comparator {
/** * 返回两个整数中的最大值 */
int max(int x, int y);
}
class ComparatorImpl implements Comparator{
@Override
public int max(int x, int y) {
//三目运算
return x>y?x:y;
}
运行结果是
5
6
6
边栏推荐
- CDH cluster integration external Flink (improved version - keep pace with the times)
- ps太卡怎么办?几步帮您解决问题
- [day02] Introduction to data type conversion, operators and methods
- Solution to the third game of 2022 Hangzhou Electric Multi school league
- The digital China Construction Summit ended with a list of massive pictures on site!
- Two way republication experiment
- 勤于奋聊聊现在还有哪些副业可以做
- Structural mode - decorator mode
- Photoshop提示暂存盘已满怎么办?ps暂存盘已满如何解决?
- Dynamic routing configuration
猜你喜欢

【报错】Cannot read property ‘parseComponent‘ of undefined

Prometheus node exporter common monitoring indicators

C language - two dimensional array, pointer

Solution to the third game of 2022 Hangzhou Electric Multi school league

Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing

Final Cut Pro中文教程 (1) 基础认识Final Cut Pro

日落红暖色调调色滤镜luts预设Sunset LUTs 1

有趣的C语言

STM32 Hal serial port (uart/usart) debugging experience (I) -- basic knowledge of serial port communication +hal library code understanding

TCP three handshakes and four disconnects
随机推荐
2022 T2i text generated image Chinese Journal Paper quick view-1 (ecagan: text generated image method based on channel attention mechanism +cae-gan: text generated image technology based on transforme
Simple static routing in ENSP
IP第十四天笔记
「Photoshop2021入门教程」调整图片到不同的长宽比
TCP's three handshakes and four waves
Easily download data in power Bi reports with power auto
Structural mode - bridging mode
TCP three handshakes and four disconnects
负数的右移
Dynamic routing configuration
报错:cannot read poperties of undefined(reading ‘then‘)
「Photoshop2021入门教程」“拉平”带有透视感的图像
详解左值、右值、左值引用以及右值引用
JS day 2 (variables, variable usage, naming rules, syntax extensions)
Structural mode - adapter mode
Redis interview question (2022)
Technology sharing | gtid that needs to be configured carefully_ mode
新手小白怎样开始学做自媒体呢?
Structural mode - facade mode
Grid layout