简体   繁体   English

如何引用在不同类的 main 方法中声明的对象的属性

[英]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.

相关问题 如何引用正确声明匿名类的对象? - How do I reference the object an anonymous class is declared in properly? Java的main()方法可以声明为final吗? - Can Java's main() method be declared final? 在声明引用时是否加载了类? - Is the class loaded when it's reference is declared? 为什么Scala从对象的main方法而不是类的static main方法开始? - Why does Scala starts from an object's main method instead of a class's static main method? 如何从静态类的main方法中引用变量? (java) - How to reference a variable in main method from a static class? (java) 如何使用对象的原始 class 投射引用? - How to cast a reference with an object's original class? 在另一个类中声明 Lock 对象并在另一个类中调用锁的方法 - declared Lock object in Another Class and invoke lock's method in another class 如何使用 class (java) 为在主要方法中声明的每个怪物设置范围攻击@伤害 - How to set in range attack@damage for each monsters that declared in the main method using class (java) 如何从静态main()方法调用内部类的方法 - how to call inner class's method from static main() method 没有实例化对象的方法中声明的类变量的赋值 - assignment to class variable declared in method with no object instantiated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM