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

zhaozj2021-02-16  49

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

1.

Abstract class name {

PRIVATE STRING NAME;

Public Abstract Boolean isstupidName (String Name) {}

}

What is wrong with the heroes?

Answer: wrong. Abstract Method must end with a semicolon and do not bring a curly bracket.

2.

Public class something {

Void dosomething () {

Private string s = "";

INT L = S.LENGTH ();

}

}

Is it wrong?

Answer: wrong. Any access modifier (private, PUBLIC, and Protected) cannot be placed before the local variable. Final can be used to modify local variables (Final is like Abstract and Strictfp, all non-access modifiers, Strictfp can only modify Class and Method rather than variable).

3.

Abstract class something {

Private abstract string dosomething ();

}

It seems that there is nothing wrong?

Answer: wrong. Abstract Methods cannot be modified in private. Abstract Methods is the specific details of the subclass item, how can I use Private to block Abstract Method? (Similar to the confstract method, Final cannot be added).

4.

Public class something {

Public Int Addone (Final INT X) {

Return x;

}

}

This is more obvious.

Answer: wrong. INT X is modified to Final, meaning X cannot be modified in Addone Method.

5.

Public class something {

Public static void main (String [] args) {

Other o = new other ();

New Something (). Addone (O);

}

Public void addone (Final Other O) {

O.I ;

}

}

Class other {

Public INT I;

}

It is very similar to the above, it is about Final problem, is this wrong?

Answer: Correct. In Addone Method, the parameter O is modified into final. If we modified O's Reference in Addone Method (for example: o = new other ();), then this question is also wrong. But this is modified here that the MEMBER VAIRABLE (member variable) of O, while the REFERENCE does not change.

After 5 questions in the << Answer and Analysis 2 >>.

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

New Post(0)