如何在类构造函数中为静态变量分配值?

huangapple go评论98阅读模式
英文:

How can I assign variables to my class constructor if they are static?

问题

我遇到了一个问题,我正在使用一种方法来接收用户数据,并且该方法应该返回一个对象。问题是,每当我尝试将输入的值分配给类构造函数时,都会出现错误消息,指出“无法从静态上下文引用非静态变量this”。我知道我的方法是使用public static [返回值]来声明的,这最终导致了这个问题。然而,我必须将这些方法保持为静态的,作为这个项目的一部分。这让我不得不以某种方式来操作类或类构造函数,但我不确定如何做到这一点。

这是类和类构造函数:

public class Project1 {

    public class Student {
        String name;
        String ID;
        float GPA;
        int creditHours;
        double tuitionCost;

        public static Student(String name, String ID, float GPA, int creditHours, double tuitionCost) {
            this.name = name;
            this.ID = ID;
            this.GPA = GPA;
            this.creditHours = creditHours;
            this.tuitionCost = tuitionCost;
        }
    }
}

这是我尝试将用户输入的数据分配给类构造函数的方式:

Student ret = new Student();

ret.name = name;
ret.ID = ID;
ret.GPA = GPA;
ret.creditHours = creditHours;
ret.tuitionCost = tuitionCost;

return ret;

如果读取输入的方法是静态的,我如何将从用户那里读取的值分配给类构造函数呢?

注意:我是Java的新手,所以我的术语可能有点不准确。

英文:

I'm running into a problem where I am using a method to take in user data and the method is to return an object. The problem is whenever I get to assigning the inputted values to the class constructor I am met with an error message stating, "non-static variable this cannot be referenced from a static context." I am aware that my methods are declared using public static [return value], which is ultimately leading to this issue. I have to keep the methods as static, though, as a part of this project. That leaves me somehow manipulating the class or class constructor, but I'm not certain as to how to do that.

This is the class and class constructor:

public class Project1 {

    public class Student {
        String name;
        String ID;
        float GPA;
        int creditHours;
        double tuitionCost;

        public static Student (String name, String ID, float GPA, int creditHours, double tuitionCost) {
            this.name = name;
            this.ID = ID;
            this.GPA = GPA;
            this.creditHours = creditHours;
            this.tuitionCost = tuitionCost;
        }
    }

This is how I am attempting to assign the user inputted data to the class constructor.

    Student ret = new Student();

    ret.name = name;
    ret.ID = ID;
    ret.GPA = GPA;
    ret.creditHours = creditHours;
    ret.tuitionCost = tuitionCost;

    return ret;

How can I assign the values I have read in from the user (using Scanner) to the class constructor if the method in which they are read in is static?

Note: I'm new to Java, so some of my jargon may be a bit off.

答案1

得分: 2

  • Java中不允许使用static构造函数。
  • 然后你的Student不是静态的,这意味着它只能通过父类Project1创建
public class Project1 {

    public class Student {

        public Student() {
        }
    }

    public static void main(String... args) {
        Project1 project1 = new Project1();
        Student student = project1.new Student();
    }
}
  • 如果你想在没有Project1的情况下使用Student类,你必须将Student类设为static
public class Project1 {

    public static class Student {

        public Student() {
        }
    }

    public static void main(String... args) {
        Student student = new Project1.Student();
    }
}
英文:
  • static constructors are not allowed in Java

  • Then your Student class is not static, it means that it could be created only with parent Project1.

    public class Project1 {

     public class Student {
    
         public Student() {
         }
     }
    
     public static void main(String... args) {
         Project1 project1 = new Project1();
         Student student = project1.new Student();
     }
    

    }

  • You have to make Student class statis if you want to use Student class without Project1.

    public class Project1 {

     public static class Student {
    
         public Student() {
         }
     }
    
     public static void main(String... args) {
         Student student = new Project1.Student();
     }
    

    }

答案2

得分: 0

这个问题的标题看起来很简单,但描述相当令人困惑:

> 如果变量是静态的,我如何将变量赋值给我的类构造函数?

根据我的理解,如果您想要在构造函数中分配一些静态变量,首先您的类中应该有静态变量,但似乎缺少这些静态变量。但是还有更多的东西您可能忽略了。

在Java中没有静态构造函数,您可能需要参考:

为什么构造函数不能是静态的 或者 我们能定义静态构造函数吗

我怀疑您发布的代码片段可能不会编译,因为编译器应该会抛出错误,因为您永远不能在类中有一个静态构造函数。

现在来谈谈您实际的问题:

> 如果从用户那里读取的值(使用Scanner),我如何将这些值分配给类构造函数,如果读取这些值的方法是静态的?

我稍微修改了您的代码来解决您的问题:

package com.sopra.banking.compliance.report.backend.common.bean;

import java.util.Scanner;

public class Test {
    public static class Student {

        String name;

        public Student() {

        }

        public Student(String name) {
            Student student = readValues();
            this.name = student.name;
        }

        static Student readValues() {
            Scanner sc = new Scanner(System.in);
            Student student = new Student();
            String name = sc.next();
            student.name = name;
            return student;
        }
    }
}

另外,作为一个附注,您有一个内部类,如果您想在内部类中定义一个静态方法,那么您需要将该类定义为静态的。

英文:

The title of the question seems very simple but the description is quite confusing:

> How can I assign variables to my class constructor if they are static?

As per my understanding, if you want to assign some static variables in your constructor then in the first place you should have static variables present in your class which seems to be missing.BUT there is something more which you are missing.

There are no static constructors in java, you might need to refer:

Why a constructor cannot be static or Can we define static constructors

I doubt the code snippet you posted would compile because the compiler should throw an error as you can never have a static constructor in a class.

Now coming to your actual issue:

> How can I assign the values I have read in from the user (using Scanner) to the class constructor if the method in which they are read in is static?

I modified your code little bit to solve your issue:

package com.sopra.banking.compliance.report.backend.common.bean;

import java.util.Scanner;

public class Test
{
    public static class Student
    {

    String name;

    public Student()
    {

    }

    public Student(String name)
    {
        Student student = readValues();
        this.name = student.name;
    }

    static Student readValues()
    {
        Scanner sc = new Scanner(System.in);
        Student student = new Student();
        String name = sc.next();
        student.name = name;
        return student;
        }
    }
}

As a side note, you were having an inner class and if you want to define a static method in an inner class then you need to make that class as static.

答案3

得分: -1

构造函数不能是静态的。方法可以。

如果你需要一个返回学生对象的静态方法,它会像这样:

public static Student buildStudent(String name, String ID, float GPA, int creditHours, double tuitionCost) {
    Student ret = new Student();

    ret.name = name;
    ret.ID = ID;
    ret.GPA = GPA;
    ret.creditHours = creditHours;
    ret.tuitionCost = tuitionCost;

    return ret;
}

然而,使用一个真正的构造函数可能更加直观来创建对象。

英文:

Constructors cannot be static. Methods can.

If you need a static method that returns a Student, it would look like

public static Student buildStudent(String name, String ID, float GPA, int creditHours, double tuitionCost) {
    Student ret = new Student();

    ret.name = name;
    ret.ID = ID;
    ret.GPA = GPA;
    ret.creditHours = creditHours;
    ret.tuitionCost = tuitionCost;

    return ret;
}

However, using an actual constructor is arguably more straightforward for object creation

huangapple
  • 本文由 发表于 2020年9月28日 02:34:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64091970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定