Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么';当你使用super()时,它发生在幕后_Java_Oop_Inheritance_Subclass_Superclass - Fatal编程技术网

Java 什么';当你使用super()时,它发生在幕后

Java 什么';当你使用super()时,它发生在幕后,java,oop,inheritance,subclass,superclass,Java,Oop,Inheritance,Subclass,Superclass,我很想知道当您使用super()调用超类的构造函数时,幕后到底发生了什么。当从子类实例化对象时,子类是否继承了超类对象?或者它是如何工作的 以下是我的代码供参考: public class Bicycle { //Declaring bicycles states public int movSpeed = 0; public int cadence = 0; public int curGear = 0; //Class constructor public Bicycle(){ } //

我很想知道当您使用
super()
调用超类的构造函数时,幕后到底发生了什么。当从子类实例化对象时,子类是否继承了超类对象?或者它是如何工作的

以下是我的代码供参考:

public class Bicycle {
//Declaring bicycles states
public int movSpeed = 0;
public int cadence = 0;
public int curGear = 0;

//Class constructor
public Bicycle(){
}

//Class constructor with params
public Bicycle(int movSpeed, int cadence, int curGear) {
    this.movSpeed = movSpeed;
    this.cadence = cadence;
    this.curGear = curGear;
}
子类:

public class mountainBike extends Bicycle {
//Declare mountainBikes states
public int frontTravel = 0;
public int rearTravel = 0;
public int gearMult = 0;

//Class constructor
public mountainBike(){
}

//Class constructor with params
public mountainBike(int movSpeed, int cadence, int curGear, int frontTravel, int rearTravel,int gearMult){
    super(movSpeed,cadence,curGear);
    this.frontTravel = frontTravel;
    this.rearTravel = rearTravel;
    this.gearMult = gearMult;
}

没有超级对象和子类对象。它只是一个对象,除了从父类继承的字段外,子类中还声明了字段

调用
super
时,JVM调用父类的构造函数来初始化从父类继承的字段。在幕后,构造函数转换为一条名为
的JVM指令,该指令为字段赋值

所以凭直觉你可以把它想象成:

public mountainBike(int movSpeed, int cadence, int curGear, int frontTravel, int rearTravel,int gearMult) {
    // an object is created here
    // call the super constructor special method <init> 
    // which initializes  the inherited fields movSpeed, cadence, and curGear
    // initialize the below fields
    this.frontTravel = frontTravel;
    this.rearTravel = rearTravel;
    this.gearMult = gearMult;
}
公共山地车(int movSpeed、int cadence、int curGear、int front travel、int reall travel、int gearmut){
//这里创建了一个对象
//调用超级构造函数的特殊方法
//它初始化继承的字段:速度、节奏和速度
//初始化以下字段
this.frontTravel=frontTravel;
this.rearTravel=rearTravel;
this.gearMult=gearMult;
}

谢谢您的回答!我只想留下一个我遇到的例子,它可以很好地解释这个问题

我们已经看到,一个对象是来自的类的数据类型 它被实例化了。例如,如果我们写

公共山地车myBike=新山地车();那我的自行车是我的 类型山地车

山地自行车是自行车和物体的后代。因此, 山地自行车是一种自行车,也是一种物体,可以使用 任何需要自行车或物体的地方

反过来不一定正确:自行车可能是山地自行车, 但这并不一定。类似地,物体可以是自行车或自行车 山地车,但不一定


从技术上讲,您的
mountainBike
(请用于类)是一辆
Bicycle
,调用
super(args…
会告诉您调用了特定的构造函数。如果不调用带有或不带参数的
super()
,将调用默认构造函数。我假设您对Java相当陌生,因此我还想指出,没有真正的理由将
frontTravel
rearTravel
等的值设置为
0
,因为这是自动完成的。请检查Head First Java以供参考(第9章)。