相关疑难解决方法(0)

实例化和声明分开

我的问题是关于Java中的声明和值分配规则。在编写字段时,我们可以一起声明和分配值,但是我们不能单独进行相同的操作。

例如:

class TestClass1 {
    private int a = 1;
    private int b ;
     b= 1;
    private int sum;

    public int getA() {
        return a;
    }

    public int getB() {
        return b;
    }

    public int getSum() {
        sum = a + b;
        return sum;
    }

}

public class TestClass {
    public static void main(String[] args) {
        TestClass1 testClass1 = new TestClass1();

        System.out.println("total =" + testClass1.getSum());
    }    

}    
Run Code Online (Sandbox Code Playgroud)

在这里:

private int a = 1; 
Run Code Online (Sandbox Code Playgroud)

我们可以将a声明为私有int并为其分配值1。但在以下情况下:

private int b ;
b= 1; …
Run Code Online (Sandbox Code Playgroud)

java declaration assign

1
推荐指数
1
解决办法
382
查看次数

对变量[Something]的赋值无效

this.name = name;
this.bind = bind;
this.category = category;
Run Code Online (Sandbox Code Playgroud)

它说

赋值给变量(3中的一个)没有效果.

我不知道为什么会出现这个错误,请帮忙.我正在使用Eclipse.

完整代码如下:

public class Module {

    private String name;
    private int bind;
    private Category category;

    public Module(String name,int bind,Category category) {
        this.name = name;
        this.bind = bind;
        this.category = category;
    }
Run Code Online (Sandbox Code Playgroud)

java variable-assignment

1
推荐指数
1
解决办法
3214
查看次数

如何从静态嵌套类修改外部类字段?

修改外类字段的正确方法是什么?当尝试从内部静态类更改时value,Artifact它会产生错误.需要迭代包含类型对象的数组列表Artifact; 并且能够为每个Artifact(无论是硬币还是高脚杯)显示该值.

 public class Artifact {
        public int value = 0;

        public static class  Goblet extends Artifact{
            value = 5; // Syntax error on token "value", VariableDeclaratorId expected after this token
        }
        public static class Coin extends Artifact{
            value = 10;
        }

    }
Run Code Online (Sandbox Code Playgroud)

java oop

0
推荐指数
1
解决办法
223
查看次数

我的ArrayList无法识别我添加到列表中的内容

这是代码本身

import java.util.ArrayList;

public class Student {
    private String name;
    private int age;

    public Student (String n, int a) {
        name = n;
        age = a;

    }

    public String toString() {
        return name + " is " + age + " years old";
    }

    ArrayList<Student> rayList = new ArrayList<Student>();
    rayList.add(new Student("Sam", 17));
    rayList.add(new Student("Sandra", 18));
    rayList.add(new Student("Billy", 16));
    rayList.add(new Student("Greg", 17));
    rayList.add(new Student("Jill", 18));

    public static void main(String[] args) {
        System.out.println(rayList.get(0));
    }

}
Run Code Online (Sandbox Code Playgroud)

main方法中缺少一些println命令.但是当我尝试将5个学生添加到我的ArrayList时,我收到错误"无法对非静态字段rayList进行静态引用"

java arraylist

0
推荐指数
1
解决办法
1257
查看次数

在对象初始化块内使用花括号

为什么bind()仅在将范围大括号内设置时该函数存在?

public void initialize() {

    inputsAreFull = new BooleanBinding() {
        {
            bind();
        }

        @Override
        protected boolean computeValue() {
            return false;
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

IntelliJ会自动建议bind()在花括号内使用,但是在花括号外不存在该功能吗?

这行不通:

public void initialize() {

    inputsAreFull = new BooleanBinding() {

        bind();

        @Override
        protected boolean computeValue() {
            return false;
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

java javafx

0
推荐指数
2
解决办法
52
查看次数