如何在Java导入自定义类?
Java语言是所有编程语言中最流行的语言之一。使用Java编程语言有几个优点,无论是出于安全目的还是构建大型分发项目。使用Java的优势之一是它试图借助类、继承、多态等概念将语言中的每个概念与现实世界联系起来。 在本文中,我们将讨论如何从一个项目到另一个项目或在同一个项目中。
自定义类由用户为特定目的而创建。这些类不存在于项目的系统库中。考虑如下所示的项目结构:
结构格式:
ProjectName:Main1
|
|
First(package)[ GFG1.java , GFG2.java ] (classes)
|
Second(package)[GFG3.java] (class)
Note: In order to get through the concept, one must go through access modifiers in java to understand the concept of the scope of classes, members, and methods.
在这里,我们将按如下方式介绍两种结构类型,稍后将在下面进一步实现相同的类型:
- 同一包内的自定义类
- 来自另一个包的自定义类
实现:为同一个项目导入类
同一个项目中的类可以被导入到同一个项目中的任何其他类中,而无需在项目的特定类中使用任何导入语句。因为,默认情况下,所有类成员和方法都是 default 修饰符,并且根据 default 修饰符的范围,它们可以在同一个包/同一个包子类中访问,而无需导入该类。参考上表。
示例 1: GFG1。Java
Java
// Package of the class
package First;
public class GFG1 {
int a;
int b;
// Creating constructor
GFG1(int a, int b)
{
this.a = a;
this.b = b;
}
// Function to add the members of the class
int add() { return this.a + this.b; }
}
Java
// Package first
package First;
public class GFG2 {
public static void main(String[] args)
{
// Creating of instance of second class present
// in the same project
GFG1 ob = new GFG1(1, 2);
System.out.println("Addition " + ob.add());
}
}
Java
package Second;
class GFG3 {
int a;
int b;
// Creating constructor
public GFG3(int a, int b)
{
this.a = a;
this.b = b;
} // Function to substract the values
public int substract() { return this.a - this.b }
}
Java
package First;
// Importing class of second package
import Second.GFG3;
public class GFG2 {
public static void main(String[] args)
{
// Creating reference of the GFG1 class that is
// present in the same project
GFG1 ob = new GFG1(1, 2);
System.out.println("Addition " + ob.add());
// Creating the reference fo the GFG3 class that is
// present in the other project
GFG3 ob1 = new GFG3(2, 1);
System.out.println("Substract " + ob1.substract());
}
}
示例 2: GFG2。Java
Java
// Package first
package First;
public class GFG2 {
public static void main(String[] args)
{
// Creating of instance of second class present
// in the same project
GFG1 ob = new GFG1(1, 2);
System.out.println("Addition " + ob.add());
}
}
输出:
Addition 3
让我们继续讨论接下来我们将从另一个包导入自定义类的地方。
执行:
只有当需要导入的特定对象被标记为 public 并且它的成员和方法应该是 public 以便它们可以在它的包之外使用时,才在不同的包之间导入类。
示例 1:第二个包类
Java
package Second;
class GFG3 {
int a;
int b;
// Creating constructor
public GFG3(int a, int b)
{
this.a = a;
this.b = b;
} // Function to substract the values
public int substract() { return this.a - this.b }
}
Above second package class is imported to the class of first package
Structure:
Second:(Package)
|
GFG3
First:(Package)
|
GFG2
示例 2:第一个包类
Java
package First;
// Importing class of second package
import Second.GFG3;
public class GFG2 {
public static void main(String[] args)
{
// Creating reference of the GFG1 class that is
// present in the same project
GFG1 ob = new GFG1(1, 2);
System.out.println("Addition " + ob.add());
// Creating the reference fo the GFG3 class that is
// present in the other project
GFG3 ob1 = new GFG3(2, 1);
System.out.println("Substract " + ob1.substract());
}
}
输出:
Addition 3
Substract 1