有人能解释一下这个小代码吗?

时间:2022-07-26 01:27:48
class Feline {
    public String type = "f";

    public Feline() {
        System.out.println("feline");
    }
}

public class Cougar extends Feline {

    public Cougar() {
        System.out.println("cougar");
    }

    void go() {
        type = "c";
        System.out.println(this.type + super.type);
    }

    public static void main(String[] args) {
        new Cougar().go();
    }
}

In the output of the console I get this:

在控制台的输出中我得到这个:

feline
cougar
cc

I am not sure why I get this output if I am creating a Cougar object and the constructor of the cougar class does not make a super call to the feline class constructor, also I don't understand why I get "C C" when calling the go() method. Sorry if it's a very basic question but I will appreciate your help.

我不知道为什么我得到这个输出如果我正在创建一个Cougar对象并且美洲狮类的构造函数没有对feline类构造函数进行超级调用,我也不明白为什么我在调用时会得到“CC” go()方法。对不起,如果这是一个非常基本的问题,但我将非常感谢您的帮助。

5 个解决方案

#1


3  

Java objects are constructed from the inside out: When you say new Cougar(), first space is allocated for the object, then a Feline object is constructed in that space by calling a Feline constructor, then finally your Cougar constructor is run.

Java对象是由内而外构建的:当你说新的Cougar()时,为对象分配第一个空间,然后通过调用Feline构造函数在该空间中构造一个Feline对象,最后运行你的Cougar构造函数。

You can specify which Cougar constructor is run by calling it explicitly with a super(..) call; that's particularly useful if there are several and you want to pick one by specifying arguments. But if you don't invoke one, the compiler will insert the super() call.

你可以通过super(..)调用显式调用它来指定运行哪个Cougar构造函数;如果有几个并且你想通过指定参数来选择一个,那么这个特别有用。但是如果你不调用一个,编译器将插入super()调用。

As to the 'cc', when type = "c"; was encountered, there was no local variable named "type" defined. That means it's a member variable, so the compiler interprets that as this.type = "c";. But there's only one member called "type", and that's in Cougar. So this.type and super.type are both the same thing, have been set to "c", and "c" is typed twice.

关于'cc',当type =“c”时;遇到过,没有定义名为“type”的局部变量。这意味着它是一个成员变量,因此编译器将其解释为this.type =“c”;.但是只有一个名为“类型”的成员,那就是美洲狮。所以this.type和super.type都是相同的东西,已经设置为“c”,而“c”是两次输入。

#2


0  

You have created two classes where Feline is a super class and Cougar is a child class. As Cougar extends Feline, Cougar inherits type variable from Feline.

你已经创建了两个类,其中Feline是一个超级类,而Cougar是一个子类。当Cougar延伸Feline时,Cougar继承了Feline的类型变量。

Now, Feline has a default constructor where feline string is printed. Cougar also has a default constructor. As per the line new Cougar().go(); in the main method, you are creating a new object of Cougar which calls the default constructor and implicitly, it calls super() that calls the constructor of Feline class.

现在,Feline有一个默认的构造函数,用于打印猫科动物的字符串。 Cougar还有一个默认构造函数。根据新线Cougar()。go();在main方法中,您正在创建一个Cougar的新对象,它调用默认构造函数,并且隐式地,它调用调用Feline类的构造函数的super()。

Now new Cougar().go() method is setting the type to "c"which means the value of the variable is changed as super.type and this.type are the same copy. So when you call this method, it prints this way:

现在新的Cougar()。go()方法将类型设置为“c”,这意味着变量的值更改为super.type,this.type是相同的副本。因此,当您调用此方法时,它会以这种方式打印:

feline

cougar

cc

#3


0  

When you are creating child object. It automatically call the parent constructor because parent need to initialized for child.

在创建子对象时。它会自动调用父构造函数,因为parent需要为child初始化。

So, when child class constructor is called it automatically call the parent default/non parameter constructor.i.e. o/p feline cougar

因此,当调用子类构造函数时,它会自动调用父默认/非参数构造函数.i.e。 o / p猫美洲狮

and .go() is changing the value of this.type and super.type and this is representing same value. because type variable is visibility is public, It also accessible in child class and there is no variable in child class with variable name of type.

和.go()正在改变this.type和super.type的值,这表示相同的值。因为类型变量是可见性是公共的,它也可以在子类中访问,并且子类中没有变量名称类型的变量。

So, The complete o/p is

所以,完整的o / p是

feline cougar cc

猫美洲狮cc

#4


0  

In the main(), when you are creating the object new Cougar() then the constructor public Cougar() is called which inturn calls parent constructor public Feline()

在main()中,当你创建对象new Cougar()然后调用构造函数public Cougar(),其中inturn调用父构造函数public Feline()

hence first sout of Feline() feline is displayed on console and then sout of Cougar() cougar is displayed on console.

因此,在控制台上显示第一个猫科动物猫科动物,然后在控制台上显示美洲狮()美洲狮。

This is popularly known as Instance Control flow in java.

这在java中通常称为实例控制流程。

Then cc is displayed in the output because of go() method, as "type" variable of both this operator and super operator points to "c".

然后由于go()方法在输出中显示cc,因为此运算符和超级运算符的“type”变量指向“c”。

Hence, the output is:
feline //because parent constructor is called first in Instance Control Flow.
cougar //because child constructor is called after parent constructor.
cc // since type variable of both this instance and super instance points to "c" object.

因此,输出是:feline //因为在实例控制流中首先调用父构造函数。 cougar //因为子构造函数是在父构造函数之后调用的。 cc //因为此实例和超级实例的类型变量都指向“c”对象。

#5


0  

According to your code: There is a parent class called "Feline" and child class called "Cougar". when it execute, It first go to the parent/super class(Feline) constructor and print : feline.

根据你的代码:有一个名为“Feline”的父类和名为“Cougar”的子类。当它执行时,它首先转到父/超类(Feline)构造函数并打印:feline。

Then it executes the child class (Cougar) constructor and print : cougar.

然后它执行子类(Cougar)构造函数并打印:cougar。

After that it executes the main method; In your main method you are referring to another method called "go", then executes the code inside "go" method:

之后它执行main方法;在main方法中,您指的是另一个名为“go”的方法,然后在“go”方法中执行代码:

Inside that it overriding the value of variable called "type" to : "C".

在它内部,它将名为“type”的变量的值覆盖为:“C”。

So when you are going to print the value of type,

所以当你要打印类型的值时,

this.type = C and super.type = C (because of the overriding)

this.type = C和super.type = C(因为覆盖)

#1


3  

Java objects are constructed from the inside out: When you say new Cougar(), first space is allocated for the object, then a Feline object is constructed in that space by calling a Feline constructor, then finally your Cougar constructor is run.

Java对象是由内而外构建的:当你说新的Cougar()时,为对象分配第一个空间,然后通过调用Feline构造函数在该空间中构造一个Feline对象,最后运行你的Cougar构造函数。

You can specify which Cougar constructor is run by calling it explicitly with a super(..) call; that's particularly useful if there are several and you want to pick one by specifying arguments. But if you don't invoke one, the compiler will insert the super() call.

你可以通过super(..)调用显式调用它来指定运行哪个Cougar构造函数;如果有几个并且你想通过指定参数来选择一个,那么这个特别有用。但是如果你不调用一个,编译器将插入super()调用。

As to the 'cc', when type = "c"; was encountered, there was no local variable named "type" defined. That means it's a member variable, so the compiler interprets that as this.type = "c";. But there's only one member called "type", and that's in Cougar. So this.type and super.type are both the same thing, have been set to "c", and "c" is typed twice.

关于'cc',当type =“c”时;遇到过,没有定义名为“type”的局部变量。这意味着它是一个成员变量,因此编译器将其解释为this.type =“c”;.但是只有一个名为“类型”的成员,那就是美洲狮。所以this.type和super.type都是相同的东西,已经设置为“c”,而“c”是两次输入。

#2


0  

You have created two classes where Feline is a super class and Cougar is a child class. As Cougar extends Feline, Cougar inherits type variable from Feline.

你已经创建了两个类,其中Feline是一个超级类,而Cougar是一个子类。当Cougar延伸Feline时,Cougar继承了Feline的类型变量。

Now, Feline has a default constructor where feline string is printed. Cougar also has a default constructor. As per the line new Cougar().go(); in the main method, you are creating a new object of Cougar which calls the default constructor and implicitly, it calls super() that calls the constructor of Feline class.

现在,Feline有一个默认的构造函数,用于打印猫科动物的字符串。 Cougar还有一个默认构造函数。根据新线Cougar()。go();在main方法中,您正在创建一个Cougar的新对象,它调用默认构造函数,并且隐式地,它调用调用Feline类的构造函数的super()。

Now new Cougar().go() method is setting the type to "c"which means the value of the variable is changed as super.type and this.type are the same copy. So when you call this method, it prints this way:

现在新的Cougar()。go()方法将类型设置为“c”,这意味着变量的值更改为super.type,this.type是相同的副本。因此,当您调用此方法时,它会以这种方式打印:

feline

cougar

cc

#3


0  

When you are creating child object. It automatically call the parent constructor because parent need to initialized for child.

在创建子对象时。它会自动调用父构造函数,因为parent需要为child初始化。

So, when child class constructor is called it automatically call the parent default/non parameter constructor.i.e. o/p feline cougar

因此,当调用子类构造函数时,它会自动调用父默认/非参数构造函数.i.e。 o / p猫美洲狮

and .go() is changing the value of this.type and super.type and this is representing same value. because type variable is visibility is public, It also accessible in child class and there is no variable in child class with variable name of type.

和.go()正在改变this.type和super.type的值,这表示相同的值。因为类型变量是可见性是公共的,它也可以在子类中访问,并且子类中没有变量名称类型的变量。

So, The complete o/p is

所以,完整的o / p是

feline cougar cc

猫美洲狮cc

#4


0  

In the main(), when you are creating the object new Cougar() then the constructor public Cougar() is called which inturn calls parent constructor public Feline()

在main()中,当你创建对象new Cougar()然后调用构造函数public Cougar(),其中inturn调用父构造函数public Feline()

hence first sout of Feline() feline is displayed on console and then sout of Cougar() cougar is displayed on console.

因此,在控制台上显示第一个猫科动物猫科动物,然后在控制台上显示美洲狮()美洲狮。

This is popularly known as Instance Control flow in java.

这在java中通常称为实例控制流程。

Then cc is displayed in the output because of go() method, as "type" variable of both this operator and super operator points to "c".

然后由于go()方法在输出中显示cc,因为此运算符和超级运算符的“type”变量指向“c”。

Hence, the output is:
feline //because parent constructor is called first in Instance Control Flow.
cougar //because child constructor is called after parent constructor.
cc // since type variable of both this instance and super instance points to "c" object.

因此,输出是:feline //因为在实例控制流中首先调用父构造函数。 cougar //因为子构造函数是在父构造函数之后调用的。 cc //因为此实例和超级实例的类型变量都指向“c”对象。

#5


0  

According to your code: There is a parent class called "Feline" and child class called "Cougar". when it execute, It first go to the parent/super class(Feline) constructor and print : feline.

根据你的代码:有一个名为“Feline”的父类和名为“Cougar”的子类。当它执行时,它首先转到父/超类(Feline)构造函数并打印:feline。

Then it executes the child class (Cougar) constructor and print : cougar.

然后它执行子类(Cougar)构造函数并打印:cougar。

After that it executes the main method; In your main method you are referring to another method called "go", then executes the code inside "go" method:

之后它执行main方法;在main方法中,您指的是另一个名为“go”的方法,然后在“go”方法中执行代码:

Inside that it overriding the value of variable called "type" to : "C".

在它内部,它将名为“type”的变量的值覆盖为:“C”。

So when you are going to print the value of type,

所以当你要打印类型的值时,

this.type = C and super.type = C (because of the overriding)

this.type = C和super.type = C(因为覆盖)