gpt4 book ai didi

java - 错误: expected nm ="Sam";

转载 作者:太空宇宙 更新时间:2023-11-04 14:56:26 24 4
gpt4 key购买 nike

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="sam";

最佳答案

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

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);
}
}

关于java - 错误: <identifier> expected nm ="Sam";,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com