当前位置:网站首页>接口和抽象类/方法学习 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
边栏推荐
- Technology sharing | gtid that needs to be configured carefully_ mode
- How does novice Xiaobai learn to be we media?
- [C language] dynamic memory management
- Horizon sunrise X3 PI (IV) running on board (unfinished)
- .htaccess学习
- Photoshop裁剪工具隐藏技巧
- Summary of fire safety training materials
- IP第十四天笔记
- Dry goods | how can independent station operation improve online chat customer service?
- 【AtCoder Beginner Contest 260 (A·B·C)】
猜你喜欢

「Photoshop2021入门教程」“拉平”带有透视感的图像
深度学习领域图像分割FCN(Fully Convolutional Networks for Semantic Segmentation)

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

Digital integrated circuit: MOS tube device chapter (II)

iPhone13再降价,其实只是做做样子,消费者都在等iPhone14

JS tips

HCIA dynamic routing rip basic experiment

Digital integrated circuit: MOS tube device chapter (I)

Safety fourth after class exercise

【搜索】Flood Fill 和 最短路模型
随机推荐
How to do smooth data migration: Double write scheme
IP 14th day notes
IP第十四天笔记
HCIA static routing basic simulation experiment
Structural mode - bridging mode
Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
报错:cannot read poperties of undefined(reading ‘then‘)
Find a specific number in an ordered array
【搜索】Flood Fill 和 最短路模型
Replication of df-gan experiment -- detailed steps of replication of dfgan and forwarding from remote port to local port using mobaxtem
负数的右移
CEPH operation
Scala immutable map, variable map, map conversion to other data types
Chapter 6: cloud database
小程序项目如何创建
ps太卡怎么办?几步帮您解决问题
0 dynamic programming medium leetcode467. The only substring in the surrounding string
新手小白怎样开始学做自媒体呢?
Chapter 4 scope and life cycle of bean object
Use unity to build a WordArt system