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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java类名与文件名_为什么Java文件名必须与公共类名相同?

發布時間:2023/12/1 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java类名与文件名_为什么Java文件名必须与公共类名相同? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java類名與文件名

The question is that "Can we keep different names for java class name and java file name?"

問題是“我們可以為Java類名和Java文件名保留不同的名稱嗎?”

Yes, we can keep the different name for the java filename and java class name if and only if the class is not public.

是的,當且僅當該類不是公共類時,才能為java文件名和Java類名保留不同的名稱。

There are two cases, that we will discuss here...

有兩種情況,我們將在這里討論...

  • Case 1: We are taking different name if the class is not public

    情況1:如果該課程不是公開的,我們將使用其他名稱

  • Case 2: We are taking different name if the class is public

    情況2:如果該課程是公開的,我們將使用其他名稱

  • Case 1: We are taking different name if the class is not public

    情況1:如果該課程不是公開的,我們將使用其他名稱

    With the help of an example, we will see what will happen if the class is not public and in that case, we can take a different name for the java class and java file that means it is not mandatory to have the same name for the java class name and java filename in that case.

    借助示例,我們將看到如果該類不是公共類,會發生什么情況,在這種情況下,我們可以為java類和java文件取一個不同的名稱,這意味著不必強制為該類使用相同的名稱。在這種情況下,java類名稱和java文件名。

    Example:

    例:

    class ClassIsNotPublic{public static void main(String[] args){String str1 = "This class is not public so we can take different name for the java filename and java classname";String str2 = "This class is not prefixed with public keyword that's why it is not public";System.out.println("What will happen if we take non public class " +str1);System.out.println("Why it is not public class "+ str2);} }

    Output

    輸出量

    E:\Programs>javac abc.java [abc is java filename]E:\Programs>java ClassIsNotPublic [ClassIsNotPublic is java classname] What will happen if we take non public class This class is not public so we can take different name for the java filename and java classname Why it is not public class This class is not prefixed with public keyword that's why it is not public

    Case 2: We are taking different name if the class is public

    情況2:如果該課程是公開的,我們將使用其他名稱

    If we declared a class as "public" then, in that case, java filename and java class name must be same or in other words, we can’t take any other name for the java class name and java filename in the case of a public class.

    如果我們將某個類聲明為“ public”,則在這種情況下,java文件名和Java類名必須相同或換句話說,對于Java類名和java文件名,我們不能使用任何其他名稱。公共課。

    Example:

    例:

    public class ClassIsPublic{public static void main(String[] args){String str1 = "This class is public so we can't take different name for the java filename and java classname";String str2 = "This class is prefixed with public keyword that's why it is public class";System.out.println("What will happen if we take public class"+" " +str1);System.out.println("Why it is public class "+ str2);} }

    Output

    輸出量

    E:\Programs>javac xyz.java xyz.java:1: error: class IfClassIsPublic is public, should be declared in a file named IfClassIsPublic.java public class IfClassIsPublic{^ 1 error

    Now we will see why it is required to take the same name for the java filename and java class name in the case of a public class?

    現在,我們將看到為什么在公共類的情況下,要求Java文件名和Java類名使用相同的名稱?

    There are few points to understand why it is required to take the same name?

    有幾點要理解為什么需要使用相同的名稱?

    • Let suppose we have a java file named "java1000" in that java file we have 1000 classes and in that case if we want to find any single class in 1000 classes so it will be more difficult to find and it will create a lot of confusion.

      假設我們有一個名為“ java1000”的java文件,該java文件中有1000個類,在這種情況下,如果我們想在1000個類中找到任何一個類,那么將很難找到它,并且會造成很多混亂。

    • In that java file we have 1000 classes we know that to find any class in 1000 classes will difficult so, in that case, almost one is class will be public and that public class will contain main() method so all the classes objects will be called from main() method class(i.e. public class) so if we take java filename and public class name will be same then it will be easy to find any class in the public class.

      在該java文件中,我們有1000個類,我們知道很難在1000個類中找到任何一個類,因此,在那種情況下,幾乎是class是public,而public class將包含main()方法,因此所有class對象都是從main()方法類(即公共類)調用,因此,如果我們使用java文件名,并且公共類名稱相同,則可以很容易在公共類中找到任何類。

    • If our java filename and public class name will be same so by using java filename we can easily reach to public class(main() method class) and if we reach to public class then from that class we can reach to any other class also from the 1000 classes and we know all classes object will be called from the main() method class(i.e. public class).

      如果我們的java文件名和公共類名相同,那么通過使用java文件名,我們可以輕松地訪問public class(main()方法類);如果我們訪問public類,那么從該類中我們也可以訪問任何其他類。 1000個類,我們知道所有類對象都將從main()方法類(即公共類)中調用。

    翻譯自: https://www.includehelp.com/java/why-does-java-file-name-must-be-same-as-public-class-name.aspx

    java類名與文件名

    總結

    以上是生活随笔為你收集整理的java类名与文件名_为什么Java文件名必须与公共类名相同?的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。