the topic is as follows: (Multiple choice) Please select the following interface to define the correct method () A: Public Static void Main (string[] args); B:privatevoid test (); C:publicvoid test2 ();D:staticvoid test3 () {};
The correct answer is: C D.
The reasons are:
A does not provide a default implementation;
B chooses to decorate with private (the private modifier is not allowed in the interface to decorate the method ).
In doing this question, oneself basically is in the state of being in the head, because about the interface oneself only know: interface definition method must be abstract method, do not allow the default implementation.
Today I wrote a demo experiment and found that there are several rules for defining methods in interfaces:
1) in an interface where static methods can be defined, static methods must be implemented . And this static method can only be modified with public or not write.
For example: public static void Test () {} or static void Test () {}.//public omitted does not write by default is also decorated with public.
The method name of a static method can be arbitrary, that is, you can define the main method, for example: public static void Main (string[] args) {}.
2) for normal methods, modifiers are: public, abstract, default, and combined public abstract
Finally, a little description of the properties in the interface
First you need to understand what the interface means. An interface is a uniform ' protocol ', and the attributes in the interface are members of the ' protocol '. They are public, static, and final constants. Equivalent to a global constant.
The variables inside the interface are public static final. So you can write this:
public static final int i=10;
Dolly Mix
int i=10; (Can omit part)
Note that the variable is given an initial value at the time of declaration and cannot be the default.
By comparison, you can see that in the interface is a constant, only the final keyword in the class becomes a constant
Based on the various features of the interface, therefore, what you think is going to change is put in your own implementation, not in the interface, interface is just a kind of things of the properties and behavior of a higher level of abstraction. For the modification to close, the extension (different implementation implements) Open, interface is a manifestation of the principle of closure.
52536233
Methods in interfaces in Java