简体   繁体   English

我可以在一个 java 包中定义多个公共类吗

[英]can i define more than one public class in a java package

i want to create a java package containing some classes .But non public classes cannot be accessed from outside a package so i write this code a saved it as Box.java我想创建一个包含一些类的 java 包。但是不能从包外部访问非公共类,所以我编写了这段代码并将其保存为 Box.java

package mypack;
public class Box
{
int length,breadth,height;
public Box(int l,int b,int h)
    {
    length=l;
    breadth=b;
    height=h;
    }
public  int vol()
    {
    return length*breadth*height;
    }
} 

public class circle
{
 int r;
 public circle(int rr)
    {
    r=rr;
    }
 public float area()
    {
    return (3.14f*r*r);
    }
 }  

is it possible to create a package like this .是否可以创建这样的包。 if possible what file name should i give it 'Box.java' or 'circle.java'如果可能的话,我应该给它什么文件名“Box.java”或“circle.java”

There can be more than one public class in package, but each public class needs to be declared in its own .java file.包中可以有多个public类,但每个公共类都需要在自己的.java文件中声明。 If you want these public classes that are in different .java files to be contained in the same package, simply put package mypack;如果你想让这些不同.java文件中的public类包含在同一个包中,只需将package mypack; at the very beginning of these .java files.在这些.java文件的最开始。 This is the example, how you should name these files:这是示例,您应该如何命名这些文件:

  1. Box.java : Box.java :

     package mypack; public class Box {...}
  2. Circle.java : Circle.java :

     package mypack; public class Circle {...}

Note that files should be named with name of public class contained in it (if public class is present inside).请注意,文件应以其中包含的public类的名称命名(如果其中存在public类)。

By the way, be aware of naming conventions in Java language - it might save your and others time when looking at code - class names should start with capital letter, so in your case it should be Circle class and Circle.java , not circle class and circle.java .顺便说一句,请注意 Java 语言中的命名约定 - 在查看代码时可能会节省您和其他人的时间 - 类名称应以大写字母开头,因此在您的情况下,它应该是Circle class 和Circle.java ,而不是circle class和circle.java

You can only create only one public type (like a class/interface, etc..) inside a source file as explained below from Java documentation (emphasis is mine).您只能在源文件中创建一种public类型(如类/接口等),如下面来自 Java 文档的解释(重点是我的)。

To create a package, you choose a name for the package (naming conventions are discussed in the next section) and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.要创建包,您需要为包选择一个名称(命名约定将在下一节讨论),并在每个包含类型(类、接口、枚举和注释)的源文件的顶部放置一个具有该名称的包语句类型)要包含在包中。 If you put multiple types in a single source file, only one can be public, and it must have the same name as the source file .如果将多种类型放在一个源文件中,则只能公开一种,并且必须与源文件同名

I suggest you read here on how to create and manage packages and source/class files in java.我建议您在此处阅读有关如何在 Java 中创建和管理包和源/类文件的信息。

Also, note that you are allowed to contain more than one non-public (default access) types inside a single source file as shown below:另请注意,您可以在单个源文件中包含多个非公共(默认访问)类型,如下所示:

Inside your Circle.java File:在 Circle.java 文件中:

package mypack;

class Box { //non-public (default) class
 //code for Box class
} 

public class Circle {//public class
  //code for circle class
} 

Also, inside a package, you can create any number of public classes (by following above rule).此外,在包内,您可以创建任意数量的public类(遵循上述规则)。 But, as a good practice, group your classes properly into various packages according to their relevance.但是,作为一个好的做法,根据它们的相关性将你的类正确地分组到不同的包中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM