java中为按钮添加图片_我们可以在Java接口中为成员定义私有和受保护的修饰符吗?...
java中為按鈕添加圖片
No, it is not possible to define private and protected modifiers for the members in interfaces in Java.
不可以,無法為Java接口中的成員定義私有修飾符和受保護(hù)的修飾符。
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.
眾所周知,接口中定義的成員是隱式公共的,換句話說,我們可以說接口中定義的成員默認(rèn)是公共的。
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修飾符,由于接口的所有成員默認(rèn)情況下都是public,因此在接口中的成員之前不必強(qiáng)制給“ 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 PrivateMemberInterfacepublic void display() {System.out.print("Private modifiers not allowed");System.out.print("for Data Members");}public static void main(String[] args) {// class instantiationMain m = new Main();// calling display() of Main classm.display();// Accessing private member of an interfaceSystem.out.println(str);} }Output
輸出量
/Main.java:5: error: modifier private not allowed hereprivate String str = "There will be an error.";^ 1 errorConclusion: We cannot define private modifiers for the members in interface.
結(jié)論:我們不能為接口中的成員定義私有修飾符。
2)在Java接口中為成員定義受保護(hù)的修飾符 (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 ProtectedMemberInterfacepublic void display() {System.out.print("Protected modifiers not allowed");System.out.print("for Data Members");}public static void main(String[] args) {// class instantiationMain m = new Main();// calling display() of Main classm.display();// Accessing protected member of an interfaceSystem.out.println(str);} }Output
輸出量
/Main.java:5: error: modifier protected not allowed hereprotected String str = "There will be an error.";^ 1 errorConclusion: We cannot define protected modifiers also for the members in interface.
結(jié)論:我們也不能為接口中的成員定義受保護(hù)的修飾符。
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 PublicMemberInterfacepublic void display() {System.out.print("Public modifiers are allowed" + " ");System.out.print("for Data Members");}public static void main(String[] args) {// class instantiationMain m = new Main();// calling display() of Main classm.display();System.out.println();// Accessing public member of an interfaceSystem.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.
結(jié)論:我們可以為接口中的成員定義公共修飾符,并且在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 NoModifierMemberInterfacepublic void display() {System.out.print("No modifiers are allowed" + " ");System.out.print("for Data Members");}public static void main(String[] args) {// class instantiationMain m = new Main();// calling display() of Main classm.display();System.out.println();// Accessing no modifiers member of an interfaceSystem.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.
結(jié)論:我們無法在接口中為成員定義修飾符,并且在Java中是有效的,因?yàn)槟J(rèn)情況下,該成員的修飾符是公共的。
翻譯自: https://www.includehelp.com/java/can-we-define-private-and-protected-modifiers-for-the-members-in-interfaces-in-java.aspx
java中為按鈕添加圖片
總結(jié)
以上是生活随笔為你收集整理的java中为按钮添加图片_我们可以在Java接口中为成员定义私有和受保护的修饰符吗?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: electron 菜单栏_如何在Elec
- 下一篇: java美元兑换,(Java实现) 美元