从另一个java文件中的一个文件扩展java类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/880662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extend a java class from one file in another java file
提问by Jin Yong
How can I include one java file into another java file?
如何将一个 java 文件包含到另一个 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
例如:如果我有 2 个 java 文件,一个被调用Person.java
,一个被调用Student.java
。我怎么能包括Person.java
成Student.java
这样我可以从扩展类Person.java
的Student.java
采纳答案by Chris Bunch
Just put the two files in the same directory. Here's an example:
只需将两个文件放在同一目录中即可。下面是一个例子:
Person.java
人.java
public class Person {
public String name;
public Person(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
Student.java
学生.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:
运行 Student(因为它有 main 函数)会产生我们想要的结果:
Your name is foo
My name is boo surprise!
回答by Pablo Santa Cruz
You don't.
你没有。
If you want to extend Person with Student, just do:
如果你想用学生扩展 Person ,只需执行以下操作:
public class Student extends Person
{
}
And make sure, when you compile both classes, one can find the other one.
并确保当您编译这两个类时,一个可以找到另一个。
What IDE are you using?
你用的是什么IDE?
回答by Jherico
Java doesn't use includes the way C does. Instead java uses a concept called the classpath, a list of resources containing java classes. The JVM can access any class on the classpath by name so if you can extend classes and refer to types simply by declaring them. The closes thing to an include statement java has is 'import'. Since classes are broken up into namespaces like foo.bar.Baz, if you're in the qux package and you want to use the Baz class without having to use its full name of foo.bar.Baz, then you need to use an import statement at the beginning of your java file like so:
import foo.bar.Baz
Java 不像 C 那样使用包含。相反,java 使用了一个称为类路径的概念,这是一个包含 java 类的资源列表。JVM 可以通过名称访问类路径上的任何类,因此如果您可以扩展类并通过声明它们来引用类型。最接近 java 的包含语句是“导入”。由于类被分解为 foo.bar.Baz 之类的命名空间,如果您在 qux 包中并且想要使用 Baz 类而不必使用其全名 foo.bar.Baz,那么您需要使用java 文件开头的 import 语句,如下所示:
import foo.bar.Baz
回答by FractalSpace
What's missing from all the explanations is the fact that Java has a strict rule of class name = file name. Meaning if you have a class "Person", is must be in a file named "Person.java". Therefore, if one class tries to access "Person" the filename is not necessary, because it has got to be "Person.java".
所有解释中都遗漏了一个事实,即 Java 有一个严格的class name = file name规则。意思是如果你有一个“Person”类,它必须在一个名为“Person.java”的文件中。因此,如果一个类尝试访问“Person”,则不需要文件名,因为它必须是“Person.java”。
Coming for C/C++, I have exact same issue. The answer is to create a new class (in a new file matching class name) and create a public string. This will be your "header" file. Then use that in your main file by using "extends" keyword.
来到 C/C++,我有完全相同的问题。答案是创建一个新类(在匹配类名的新文件中)并创建一个公共字符串。这将是您的“头”文件。然后通过使用“extends”关键字在主文件中使用它。
Here is your answer:
这是你的答案:
Create a file called Include.java. In this file, add this:
public class Include { public static String MyLongString= "abcdef"; }
Create another file, say, User.java. In this file, put:
import java.io.*; public class User extends Include { System.out.println(Include.MyLongString); }
创建一个名为 Include.java 的文件。在此文件中,添加以下内容:
public class Include { public static String MyLongString= "abcdef"; }
创建另一个文件,例如 User.java。在这个文件中,输入:
import java.io.*; public class User extends Include { System.out.println(Include.MyLongString); }