【问题标题】:Java attribute cannot be assigned in constructorJava 属性不能在构造函数中赋值
【发布时间】:2020-12-03 05:56:43
【问题描述】:

我正在尝试将值传递给我在 Java 中创建的类的属性,但它不起作用。

这是我的代码:

// Landscape Class

public class Landscape{

// Attribute

char [] [] landscape ;
    
// Constructor to create the landscape 

public void Landscape(){

landscape = {

    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
    {'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
    {'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
    {'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
    {'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
    {'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
    {'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'}
    
    };

}

// Function to display the landscape

public void display(){

    System.out.println("");

for(char [] element : landscape){


    System.out.print("              ");
    
    for(char character : element){
        System.out.print(character);
    }
    
    System.out.println("");
    
}

System.out.println("");

}

public static void main(String[] args){
    
    Landscape landscape = new Landscape();
    landscape.display();
}


}
 

我已经破解了我的数组语法,我知道这很好,但由于某些原因,Java 似乎无法识别我的类属性并给了我所有这些疯狂的错误:

Landscape.java:13: error: illegal start of expression
landscape = {
            ^
Landscape.java:15: error: not a statement
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
     ^
Landscape.java:15: error: ';' expected
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
        ^
Landscape.java:15: error: illegal start of expression
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
                                                                     ^
Landscape.java:16: error: not a statement
    {'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
     ^
Landscape.java:16: error: ';' expected
    {'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
        ^
Landscape.java:16: error: illegal start of expression
    {'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
                                                                     ^
Landscape.java:17: error: not a statement
    {'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
     ^
Landscape.java:17: error: ';' expected
    {'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
        ^
Landscape.java:17: error: illegal start of expression
    {'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
                                                                     ^
Landscape.java:18: error: not a statement
    {'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
     ^
Landscape.java:18: error: ';' expected
    {'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
        ^
Landscape.java:18: error: illegal start of expression
    {'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
                                                                     ^
Landscape.java:19: error: not a statement
    {'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
     ^
Landscape.java:19: error: ';' expected
    {'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
        ^
Landscape.java:19: error: illegal start of expression
    {'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
                                                                     ^
Landscape.java:20: error: not a statement
    {'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
     ^
Landscape.java:20: error: ';' expected
    {'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
        ^
Landscape.java:20: error: illegal start of expression
    {'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
                                                                     ^
Landscape.java:21: error: not a statement
    {'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
     ^
Landscape.java:21: error: ';' expected
    {'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
        ^
Landscape.java:21: error: illegal start of expression
    {'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
                                                                     ^
Landscape.java:22: error: not a statement
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'}
     ^
Landscape.java:22: error: ';' expected
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'}
        ^
24 errors 

我想了解我做错了什么以及如何解决它

【问题讨论】:

  • 那不是构造函数。您还试图在没有一个的情况下访问非静态函数。
  • 不知道为什么这个问题被否决了,但它看起来像是 Java 中数组初始化的一个有趣案例,最终不允许使用这样的数组初始化器。老实说,我不知道这些细节,有趣的是为什么会这样做,而且我看不出真正的原因,因为编译器似乎能够检测到变量类型本身。或许这个解释详细:stackoverflow.com/questions/12805535/…
  • @Abra 我尝试了您的解决方案,该解决方案确实允许我无错误地编译,但现在运行代码时出现 NullPointerException。我的猜测是您提供的解决方案创建了一个新的多数组字符但由于它在方法的范围内,我的方法 display() 无法访问它,这就是为什么我想创建一个属性“风景”并在我的方法中使用它但那不起作用
  • @Monstarules 为什么?

标签: java oop


【解决方案1】:

构造函数声明包含返回类型。

public class Landscape {
    char[][] landscape;

    // Constructor to create the landscape
    public Landscape() {
        landscape = new char[][]{
                {'^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^'},
                {'^', ' ', ' ', ' ', ' ', '^', '^', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
                {'^', ' ', '^', '^', ' ', ' ', ' ', '^', '^', ' ', '^', '^', '^', '^', '^', '^'},
                {'^', ' ', ' ', '^', ' ', ' ', ' ', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
                {'^', '^', ' ', '^', ' ', '^', ' ', '^', '^', '^', '^', '^', '^', '^', '^', '^'},
                {'^', ' ', ' ', ' ', ' ', ' ', ' ', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
                {'^', '^', ' ', ' ', '^', '^', ' ', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
                {'^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^'}
        };
    }

    // Function to display the landscape
    public void display() {
        System.out.println("");
        for (char[] element : landscape) {
            System.out.print("              ");
            for (char character : element) {
                System.out.print(character);
            }
            System.out.println("");
        }
        System.out.println("");
    }

    public static void main(String[] args) {
        Landscape landscape = new Landscape();
        landscape.display();
    }
}

【讨论】:

  • 感谢您提供的信息,尽管正如我所说,这对我的属性没有帮助,而且我的例外情况提到更高
  • 我忘了重新添加“new char[] []”它工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-25
  • 2017-11-11
  • 2013-06-18
相关资源
最近更新 更多