Java trap, a basket: the first answer and analysis 2

zhaozj2021-02-16  42

Java trap, a basket: the first answer and analysis 2

The top 5 is in the << Answer and Analysis 2 >>.

6.

Class Something {

INT I;

Public void dosomething () {

System.out.println ("i =" i);

}

}

Is there anything wrong? I can't see it.

Answer: Correct. The output is "i = 0". INT i belongs to Instant Variable (instance variable, or a member variable). Instant variable has default value. INT's default value is 0.

7.

Class Something {

Final INT I;

Public void dosomething () {

System.out.println ("i =" i);

}

}

There is only one place with one of the above questions, that is, there is a final. Is this wrong?

Answer: wrong. Final INT i is a Final's Instant Variable (instance variable, or a member variable). Final Instant Variable No default value must be given a clear value before the constructor ends. Can be modified to "Final INT I = 0;".

8.

Public class something {

Public static void main (String [] args) {

Something s = new something ();

System.out.println ("s.dosomething () returns" DOSMETHING ());

}

Public string dosomething () {

Return "Do Something ...";

}

}

It looks perfect.

Answer: wrong. It seems that there is no problem in main dosomething in main, after all, two Methods are in the same class. But look carefully, main is static. Static Method can't directly call Non-Static Methods. It can be changed to "S.DOSMETHING () Returns" S.DOSomething ()); ". Similarly, Static Method cannot access Non-Static Instant Variable.

9.

Here, the file name of the Something class is called Othershing.java

Class Something {

Private static void main (string [] Something_to_do) {

System.out.println ("Do Something ...");

}

}

This seems to be obvious.

Answer: Correct. No one has said that the Java's Class name must be the same as its file name. But the name of the public class must be the same as the file name.

10.

The hardest question today:

Interface playable {

Void Play ();

}

Interface bounceable {

Void Play ();

}

Interface Rollable Extends Playable, Bounceable {

Ball ball = New Ball ("pingpang");

}

Class ball imports rollable {

PRIVATE STRING NAME;

Public string getname () {

Return Name;

}

Public ball (String name) {

THIS.NAME = Name;

}

Public void play () {

Ball = New Ball ("Football");

System.out.println (ball.getname ());

}

}

This error is not easy to find.

Answer: wrong. "Interface Rollable Extends Play, Bounceable" is no problem. Interface can inherit multiple Interfaces, so it is right. The problem is in the Interface Rollable "Ball Ball = New Ball (" pingpang ");". Any Interface Variable (interface variable, or member variable) declared in Interface, the default is public static final. That is, "Ball Ball = New Ball (" pingpang ");" I am "public static final ball ball = new ball (" pingpang ");". In the Play () method of the Ball class, "football"); "Change the Ball Reference, and the Ball from Rollable Interface, the ball in the Rollable Interface is public static final, Final Object It is not possible to change Reference. Therefore, the compiler will be "Ball = New Ball (" football ");" it is wrong here.

转载请注明原文地址:https://www.9cbs.com/read-25001.html

New Post(0)