迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中导入自定义类

作者:迹忆客 最近更新:2023/10/10 浏览次数:

本文介绍了如何在 Java 中导入自定义类。

如果该类存在于同一个包中,则通过创建其对象来使用该类,如果该类存在于另一个包中,则应首先使用其方法和变量导入该包。让我们仔细看一下这些示例。


在 Java 中导入类的语法

以下是在 Java 中导入类和该类的静态成员的语法。

import package.myclass;
import static package.myclass; // static import

在 Java 中导入自定义类

让我们创建一个包含两个实例变量以及 gettersetter 方法的自定义类(Student)。之后,使用 import 语句将该类导入 SimpleTesting 类。请参见下面的示例。

  • Student.java
package myjavaproject;

class Student{
    String name;
    String email;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}
  • SimpleTesting.java
package xyz;
import myjavaproject.Student;

public class SimpleTesting{
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("Rohna");
        student.setEmail("rohna@xyz.com");
        System.out.println(student.getName());
        System.out.println(student.getEmail());
    }
}

输出:

Rohna
rohna@xyz.com

在 Java 中导入自定义类

如果该类位于同一包或同一文件中,则我们不需要导入该类,而只需通过创建该类的对象来使用它。请参见下面的示例。

package myjavaproject;

public class SimpleTesting extends Thread{
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("Rohna");
        student.setEmail("rohna@xyz.com");
        System.out.println(student.getName());
        System.out.println(student.getEmail());
    }
}

输出:

Rohna
rohna@xyz.com

Java 中的静态导入类

如果只想导入类的静态成员,则可以使用静态导入概念。我们需要在 import 语句中使用 static 关键字。请参见下面的示例。

import static java.lang.Math.*;
public class SimpleTesting{
    public static void main(String[] args) {
        int a = 10;
        double b = ceil(a);
        System.out.println(b);
    }
}

输出:

10.0

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便