简体   繁体   English

如何访问我的构造函数中定义的字段?

[英]How to I access the fields defined in my constructor?

I have a picker class that picks your outfit for you, depending on why you're going out and what kind of day it is.我有一个挑选课程,可以为你挑选你的衣服,这取决于你出去的原因和今天是什么样的一天。

I'm trying to finish the chooser method that decides on what pieces of clothing to assign at each ArrayList index.我正在尝试完成选择器方法,该方法决定在每个 ArrayList 索引处分配哪些衣服。 Clothing is its own class which has String brand, String color, and boolean formal fields. Clothing 是它自己的类,它具有字符串品牌、字符串颜色和布尔形式字段。

In the chooser method, I'm trying to do在选择器方法中,我正在尝试做

outfit.add(jeans);

but it gives me a nullpointer exception, which it shouldn't because I define jeans in the constructor, so I tried to do但它给了我一个空指针异常,它不应该是因为我在构造函数中定义了牛仔裤,所以我尝试这样做

outfit.add(picker.jeans);装束。添加(picker.jeans);

and it gives the error "non static field 'jeans' cannot be referenced from a static context"它给出了错误“不能从静态上下文中引用非静态字段'牛仔裤'”

The method used static boolean variables, so I tried fixing it by turning该方法使用静态布尔变量,所以我尝试通过转动来修复它

public class picker {
static boolean formalEvent;
static boolean niceWeather;
static boolean specialDay;
static boolean workingOut;

into进入

public class picker {
boolean formalEvent;
boolean niceWeather;
boolean specialDay;
boolean workingOut;

but it still doesn't work, and I maybe shouldn't have had those static in the first place, but it made my main method work, where I define them based on the users input into the scanner that I also made static.但它仍然不起作用,而且我可能一开始就不应该使用那些静态的,但它使我的主要方法起作用,我根据用户输入到扫描仪中的输入来定义它们,我也将其设为静态。 My teacher said use static where you don't need multiple of it.我的老师说在不需要多个静态的地方使用静态。 I only need one scanner.我只需要一台扫描仪。

The beginning of my code up to the constructor looks like this, I might later make the constructor ask what kind of pants, sweatpants, shirt, etc the user has so it isn't always one defined wardrobe.我的代码到构造函数的开头看起来像这样,稍后我可能会让构造函数询问用户拥有什么样的裤子、运动裤、衬衫等,所以它并不总是一个定义的衣柜。

import java.util.ArrayList;
import java.util.Scanner;

public class picker {
    boolean formalEvent;
    boolean niceWeather;
    boolean specialDay;
    boolean workingOut;
    ArrayList<clothing> outfit = new ArrayList<>();
    static Scanner scnr = new Scanner(System.in);
    clothing khakis;
    clothing jeans;
    clothing shorts;
    clothing sweatpants;
    clothing sweatshirt;
    clothing gymShirt;
    clothing workShirt;
    clothing designerShirt;

    /**
     * Picker constructor.
     */
    public picker() {
        clothing khakis = new clothing("Levi", "brown", true);
        clothing jeans = new clothing("Mike Amiri", "blue", false);
        clothing shorts = new clothing("Nike", "blue", false);
        clothing sweatpants = new clothing("Adidas", "black", false);
        clothing sweatshirt = new clothing("Old navy", "red", false);
        clothing gymShirt = new clothing("Nike", "grey", false);
        clothing graphicTee = new clothing("Mike's Paving Posse", "black", false);
        clothing workShirt = new clothing("Polo", "yellow", true);
        clothing designerShirt = new clothing("Supreme", "red", false);
    }

The start of my main method looks like this我的主要方法的开始看起来像这样

public static void main(String[] args) {
        picker todaysFit = new picker();
        String response = "";
        System.out.println("Are you going to a formal event?");
        response = scnr.next();
        if (response.equalsIgnoreCase("yes")) {
            formalEvent = true;

and now现在

formalEvent = true;

gets an error at formalEvent saying "Non-static field 'formalEvent' cannot be referenced from a static context."在formalEvent 出现错误,提示“不能从静态上下文中引用非静态字段‘formalEvent’。” I tried making it我试过了

picker.formalEvent = true;

to fix it, but it still gives the same error.修复它,但它仍然给出相同的错误。

You have declared and initialized local variables in the constructor that hide the data members.您已经在构造函数中声明并初始化了隐藏数据成员的局部变量 Drop the type declarations to actually initialize the members:删除类型声明以实际初始化成员:

public picker() {
    khakis = new clothing("Levi", "brown", true);
    jeans = new clothing("Mike Amiri", "blue", false);
    shorts = new clothing("Nike", "blue", false);
    sweatpants = new clothing("Adidas", "black", false);
    sweatshirt = new clothing("Old navy", "red", false);
    gymShirt = new clothing("Nike", "grey", false);
    graphicTee = new clothing("Mike's Paving Posse", "black", false);
    workShirt = new clothing("Polo", "yellow", true);
    designerShirt = new clothing("Supreme", "red", false);
}

You would need to declare the variables as static in your constructor.您需要在构造函数中将变量声明为静态变量。 But to follow good practice, you should create a method in your constructor to modify the value, for example:但是要遵循良好的做法,您应该在构造函数中创建一个方法来修改值,例如:

public changeShirt(clothing newShirt){
   this.shirt = newShirt
}

You would call this method on your todaysFit object你会在你的 todaysFit 对象上调用这个方法

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

相关问题 如何在我的类中使用复制构造函数和静态字段? - How can I use copy constructor and static fields in my class? 如何访问Android列表中定义的类字段? - how to access defined class fields in a list in android? 如何访问Java中自定义注释中定义的字段 - How to access fields defined in custom annotations in Java 如何在我的线程中访问Runnable对象的字段 - How can I access the fields of the Runnable object within my Thread 如何强制在抽象类的所有子类中定义构造函数 - How can I force a Constructor to be defined in all subclass of my abstract class 如何访问用户定义的树集内对象的子字段? - How do I access the child's fields of an object inside a user defined treeset? 如何让参数化构造函数链中的派生类访问使用派生构造函数初始化的基类的字段 - How to let a derived class in a parameterized constructor chain access fields of the base class that are initialized using the derived constructor 如何访问 class 的私有构造函数? - How can I access a private constructor of a class? 如何同时创建具有自动装配字段和非自动装配字段的构造函数? - How can i create constructor with autowired and non autowired fields simultaniosly? 如何使用枚举创建没有字段的构造函数? - How can i make constructor without fields with enum?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM