本文介绍了从另一个java文件中的一个文件扩展java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将一个java文件包含到另一个java文件中?
How can I include one java file into another java file?
例如:
如果我有2个java文件,则一个名为 Person.java
,其中一个是叫 Student.java
。如何将 Person.java
包含到 Student.java
中,以便我可以从 Person.java in Student.java
For example:If I have 2 java file one is called Person.java
and one is called Student.java
. How can I include Person.java
into Student.java
so that I can extend the class from Person.java
in Student.java
推荐答案
只需将两个文件放在同一目录中即可。这是一个例子:
Just put the two files in the same directory. Here's an example:
Person.java
Person.java
public class Person {
public String name;
public Person(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
Student.java
Student.java
public class Student extends Person {
public String somethingnew;
public Student(String name) {
super(name);
somethingnew = "surprise!";
}
public String toString() {
return super.toString() + "\t" + somethingnew;
}
public static void main(String[] args) {
Person you = new Person("foo");
Student me = new Student("boo");
System.out.println("Your name is " + you);
System.out.println("My name is " + me);
}
}
运行学生(因为它有主要功能)我们期望的结果:
Running Student (since it has the main function) yields us the desired outcome:
Your name is foo
My name is boo surprise!
这篇关于从另一个java文件中的一个文件扩展java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!