日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java中为按钮添加图片_我们可以在Java接口中为成员定义私有和受保护的修饰符吗?...

發(fā)布時(shí)間:2025/3/11 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中为按钮添加图片_我们可以在Java接口中为成员定义私有和受保护的修饰符吗?... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 error

Conclusion: 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 error

Conclusion: 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。