简体   繁体   English

在java中将属性从一个类传递到另一个+ instaiation

[英]Passing attributes from one class to another + instaiation in java

I am tying to wrap my head around using a method of an object in one class (in this case the Category class) and having it pass attributes/fields into an object of another class (in this case subCategory class). 我想在一个类中使用一个对象的方法(在本例中为Category类),并将属性/字段传递给另一个类的对象(在本例中为subCategory类)。 You can see in my main method below the actual results I am getting from the compiler versus the results I am trying to obtain. 你可以在我的main方法下面看到我从编译器获得的实际结果与我想要获得的结果。

I think the biggest challenge for me is trying the following: 我认为对我来说最大的挑战是尝试以下方法:

  1. Passing a class as an argument in a method(I could not find online references that talk about it.) 将一个类作为参数传递给一个方法(我找不到关于它的在线参考文献。)
  2. Instantiating an object of an external class inside a method of a local class. 在本地类的方法中实例化外部类的对象。

Thanks for the help. 谢谢您的帮助。

Category class 分类

public class Category {

private String name;
private SubCategory subCategory = new SubCategory();

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public void createSubcategory(SubCategory subCategory, String name){
    SubCategory m = new SubCategory();
    m.setParent(Category.this);
    m.setName(name);

}

SubCategory class SubCategory类

public class SubCategory {


private String name;
private Category parent;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Category getParent() {
    return parent;
}

public void setParent(Category parent) {
    this.parent = parent;
}    }

Main (method) 主要(方法)

public static void main(String[] args) {


        Category mainCat = new Category();
        SubCategory m = new SubCategory(); // I want to instantiate this inside the method of class Category called createSubCategory()

        mainCat.setName("TITLE");
        System.out.println(mainCat.getName()); // Returns --> "TITLE" THIS IS OK!


        mainCat.createSubcategory(m, "SUBTITLE1");
        System.out.println(m.getName()); // Returns "null", I'm trying to make it return --> "SUBTITLE1"
        System.out.println(m.getParent());// Returns "null", I'm trying to make it return --> "mainCat (object)"

    }

right now your createSubCategory method receives a SubCategory as a parameter, but it does nothing with it (creates a new instance inside). 现在你的createSubCategory方法接收一个SubCategory作为参数,但它没有做任何事情(在里面创建一个新的实例)。 and on the other hand- it creates an instance of SubCategory, but does nothing with it as the method returns void. 另一方面,它创建了一个SubCategory实例,但由于该方法返回void,因此不执行任何操作。

Two ways you can go here: 你可以在这里找到两种方式:

  1. make createSubCategory method use the provided subCategory, and work on it (in this case, I'd call it setSubCategory , as it doesn't create it): make createSubCategory方法使用提供的subCategory,并对其进行处理(在这种情况下,我将其称为setSubCategory ,因为它不会创建它):

     public void setSubcategory(SubCategory subCategory, String name){ subCategory.setParent(Category.this); subCategory.setName(name); } 

and in main: 在主要:

    mainCat.setSubcategory(m, "SUBTITLE1");
    System.out.println(m.getName()); //you will get 'SUBTITLE1' here
    System.out.println(m.getParent()); //you will get the parent category
  1. make createSubCategory create it, and in this case you don't need to pass it to it, but instead return it: make createSubCategory创建它,在这种情况下你不需要将它传递给它,而是返回它:

     public SubCategory createSubcategory(String name){ SubCategory m = new SubCategory(); m.setParent(Category.this); m.setName(name); return m; } 

and in main: 在主要:

public static void main(String[] args) {
    Category mainCat = new Category();
    mainCat.setName("TITLE");
    System.out.println(mainCat.getName()); 

    Subcategory m = mainCat.createSubcategory("SUBTITLE1");
    System.out.println(m.getName());
    System.out.println(m.getParent());
}

I would even go little bit further and modify example 2 given by @Nir Levy: 我甚至会更进一步修改@Nir Levy给出的例子2:

public SubCategory createSubcategory(String name){
    return new SubCategory(this, name);
}

If both attributes are required they should be injected via constructor ( Constructor or Setter ) 如果两个属性都是必需的,则应通过构造函数( Constructor或Setter )注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM