以下是我的代码。我没有得到什么是错误。任何人都可以指导。
class State {
static String country;
static String capital;
State() // Constructor
{
country = "America's";
capital = "Washington D.C";
}
static void display() {
System.out.println(capital + " " + "is" + " " + country + " " + "capital.");
}
}
class Place extends State // Method Overriding
{
static void display() {
System.out.println("Capital is Washington D.C.");
}
public static void main(String[] args) {
State st = new State();
Place pl = new Place();
st.display();
pl.display();
st = pl;
}
}
运行时显示为"错误:找不到或加载主类状态$"
由于产出的需要:"资本是华盛顿特区"而不是(资本 " " IS" " country " " Capital。")
class State
{
static String country;
static String capital;
State() //Constructor
{
country = "America's";
capital = "Washington D.C";
}
static void display()
{
System.out.println(capital + " " + "is" + " " + country + " " +"capital." );
}
}
主类
class Place extends State // Inheritance
static void display()
{
System.out.println("Capital is Washington D.C.");
}
public static void main(String[] args)
{
State st = new State();
st.display(); // to print sub class method
display(); // to print same class method
//st = pl; No idea of this point ..
}
}
您的Place
类需要定义为public
。
编辑:该文件还必须命名为 Place.java