错误:<identifier> expected nm =“Sam”; </identifier>

时间:2014-04-18 06:47:31

标签: java

import java.util.*;

public abstract class Player {
    abstract String nm;
    public abstract void displayDetails();
}

class Booking extends Player {
nm = "Sam";           

    void displayDetails() {
        System.out.println("Name is:" + nm);
    }
}

//代码产生错误:标识符期望nm =&#34; sam&#34 ;;

1 个答案:

答案 0 :(得分:0)

在Java中,您应该仅将表达式保留在块内。你不能在那里保留nm = "Sam";,在实例初始化块中移动它

import java.util.*;

public abstract class Player {
    String nm; // instance fields cant be declared as abstract
    public abstract void displayDetails();
}

class Booking extends Player {
    { // instance initialization block
      nm = "Sam";
    }

    void displayDetails() {
        System.out.println("Name is:" + name);
    }
}