gpt4 book ai didi

java - java程序中的抽象

转载 作者:搜寻专家 更新时间:2023-11-01 01:24:40 27 4
gpt4 key购买 nike

为什么输出如下?

bike is created

running safely..

gear changed

因为我们没有在任何地方调用 Bike() 方法。

abstract class Bike {
Bike() {
System.out.println("bike is created");
}

abstract void run();

void changeGear() {
System.out.println("gear changed");
}
}

//Creating a Child class which inherits Abstract class
class Honda extends Bike {
void run() {
System.out.println("running safely..");
}
}

//Creating a Test class which calls abstract and non-abstract methods
class TestAbstraction2 {
public static void main(String args[]) {
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}

最佳答案

本田类是用 Default Constructor 创建的

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

 public class Point {
int x, y;
}

is equivalent to the declaration:

public class Point {
int x, y;
public Point() { super(); }
}

因此每次调用 new Honda();

时都会调用 Bike()

关于java - java程序中的抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52686018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com