[英]How to reference an object's property that was declared in main method in a different class
I have 2 classes in 2 separate files.我在 2 个单独的文件中有 2 个类。 My first class in Person.java is shown below:
我在 Person.java 中的第一个类如下所示:
public class Person(){
String name;
public static void main(String[] args) {
Person paul = new Person();
paul.name = paul();
}
}
How would I reference that ^ specific person object's name property (paul) in my second class: Family.java's main method?我将如何在我的第二个类:Family.java 的 main 方法中引用 ^ 特定人员对象的名称属性(paul)? Inside of Family.java I've tried creating a new person object but i can't then reference the Person object, Paul,'s name property.
在Family.java 中,我尝试创建一个新的person 对象,但我无法引用Person 对象Paul 的name 属性。
Pretty much in Family.java几乎在 Family.java 中
public class Family(){
public static void main(String[] args) {
}
}
inside of the main method i want to get the paul object, which i created in Person.java's main method,'s name property在 main 方法中,我想获取我在 Person.java 的 main 方法中创建的 paul 对象的 name 属性
This cannot be done.这是无法做到的。 Upon
Person.main
returning, paul
falls out of scope and is no longer accessible, and becomes eligible for garbage collection.当
Person.main
返回时, paul
超出范围并且不再可访问,并且有资格进行垃圾收集。
You could instead do something like this:你可以做这样的事情:
public class Person{
String name;
static Person paul;
public static void main(String[] args) {
paul.name = "Paul"; //I assume you meant this?
}
}
public class Family{
public static void main(String[] args) {
Person paul = Person.paul;
}
}
But judging from what you're trying to do (and that you ahve two main methods) you may have a deeper issue.但是从您尝试做的事情(以及您有两种主要方法)来看,您可能有更深层次的问题。
You can have static member
in your Person
class then access in the Family
class.您可以在
Person
类中拥有static member
,然后在Family
类中访问。 You can define the getter
for the name
attribute for getting the name of the Person
.您可以为
name
属性定义getter
以获取Person
的名称。 Also you can keep both the class in one file, keeping only one of the class public, because file name should same as the public class.您也可以将两个类都保存在一个文件中,只保留一个类公共,因为文件名应该与公共类相同。 Also
()
are not required for declaring a class.此外
()
不是声明类所必需的。 As name
is String
type in the class, you can define an string literal for assigning to it.由于
name
是类中的String
类型,您可以定义一个字符串文字来分配给它。
public class Family {
public static void main(String[] args) {
Person person = Person.paul;
String name = person.getName();
}
}
class Person{
private String name;
static Person paul;
public static void main(String[] args) {
paul = new Person();
paul.name = "paul";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
PS : What are you trying to achieve with this code? PS:你想用这段代码实现什么? Because
two main method
are not required.因为不需要
two main method
。 You can define an constructor
in the Person
class with an String
argument.您可以使用
String
参数在Person
类中定义constructor
函数。 Create the Person
object in you Family
class as you need.根据需要在
Family
类中创建Person
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.