java中为按钮添加图片
No, it is not possible to define private and protected modifiers for the members in interfaces in Java.
不可以,无法为Java接口中的成员定义私有修饰符和受保护的修饰符。
As we know that, the members defined in interfaces are implicitly public or in other words, we can say the member defined in an interface is by default public.
众所周知,接口中定义的成员是隐式公共的,换句话说,我们可以说接口中定义的成员默认是公共的。
It is possible to define public modifiers for the member in interfaces.
可以在接口中为该成员定义公共修饰符。
In the case of public modifiers, it is not mandatory to prefix "public" with members in interfaces because all the members of the interface are by default public.
对于public修饰符,由于接口的所有成员默认情况下都是public,因此在接口中的成员之前不必强制给“ public”加上前缀。
We will discuss three cases in terms of modifiers for the members in interfaces.
我们将针对接口中成员的修饰符讨论三种情况。
- What will happen, if we define private modifiers for the members in an interface?
- What will happen, if we define protected modifiers for the members in the interface?
- What will happen, if we define public modifiers for the members in the interface?
- What will happen, if we define no modifiers for the members in the interface?
We will see each of the above cases one by one with the help of an example...
在示例的帮助下,我们将逐一看到上述每种情况。
1)在Java接口中为成员定义私有修饰符 (1) Defining private modifiers for the member in interface in Java)
// Java program to demonstrate the example of
// defining private members for the interface
interface PrivateMemberInterface {
private String str = "There will be an error.";
void display();
}
public class Main implements PrivateMemberInterface {
// override display() of PrivateMemberInterface
public void display() {
System.out.print("Private modifiers not allowed");
System.out.print("for Data Members");
}
public static void main(String[] args) {
// class instantiation
Main m = new Main();
// calling display() of Main class
m.display();
// Accessing private member of an interface
System.out.println(str);
}
}
Output
输出量
/Main.java:5: error: modifier private not allowed here
private String str = "There will be an error.";
^
1 error
Conclusion: We cannot define private modifiers for the members in interface.
结论:我们不能为接口中的成员定义私有修饰符。
2)在Java接口中为成员定义受保护的修饰符 (2) Defining protected modifiers for the member in interface in Java)
// Java program to demonstrate the example of
// defining protected members for the interface
interface ProtectedMemberInterface {
protected String str = "There will be an error.";
void display();
}
public class Main implements ProtectedMemberInterface {
// override display() of ProtectedMemberInterface
public void display() {
System.out.print("Protected modifiers not allowed");
System.out.print("for Data Members");
}
public static void main(String[] args) {
// class instantiation
Main m = new Main();
// calling display() of Main class
m.display();
// Accessing protected member of an interface
System.out.println(str);
}
}
Output
输出量
/Main.java:5: error: modifier protected not allowed here
protected String str = "There will be an error.";
^
1 error
Conclusion: We cannot define protected modifiers also for the members in interface.
结论:我们也不能为接口中的成员定义受保护的修饰符。
3)在Java接口中为成员定义公共修饰符 (3) Defining public modifiers for the member in interface in Java)
// Java program to demonstrate the example of
// defining public members for the interface
interface PublicMemberInterface {
public String str = "No error here...";
void display();
}
public class Main implements PublicMemberInterface {
// override display() of PublicMemberInterface
public void display() {
System.out.print("Public modifiers are allowed" + " ");
System.out.print("for Data Members");
}
public static void main(String[] args) {
// class instantiation
Main m = new Main();
// calling display() of Main class
m.display();
System.out.println();
// Accessing public member of an interface
System.out.println(str);
}
}
Output
输出量
Public modifiers are allowed for Data Members
No error here...
Conclusion: We can define public modifiers for the members in an interface and it is valid in java.
结论:我们可以为接口中的成员定义公共修饰符,并且在Java中有效。
4)在Java接口中未为成员定义任何修饰符 (4) Not defining any modifiers for the member in interface in Java)
// Java program to demonstrate the example of
// not defining any modifier for the members in
// interface
interface NoModifierMemberInterface {
String str = "No error here...";
void display();
}
public class Main implements NoModifierMemberInterface {
// override display() of NoModifierMemberInterface
public void display() {
System.out.print("No modifiers are allowed" + " ");
System.out.print("for Data Members");
}
public static void main(String[] args) {
// class instantiation
Main m = new Main();
// calling display() of Main class
m.display();
System.out.println();
// Accessing no modifiers member of an interface
System.out.println(str);
}
}
Output
输出量
No modifiers are allowed for Data Members
No error here...
Conclusion: We can define no modifiers for the members in the interface and it is valid in java because by default modifier for the member is public.
结论:我们无法在接口中为成员定义修饰符,并且在Java中是有效的,因为默认情况下,该成员的修饰符是公共的。
java中为按钮添加图片