2017-02-23 163 views
0

以下是我的代码。我没有得到什么错误。任何人都可以引导。覆盖构造函数类

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; 

    } 
} 

在运行它显示为“错误:无法找到或加载主类国家$地方”

As the output needs: "Capital is Washington D.C." instead of (capital + " " + "is" + " " + country + " " +"capital.")

+0

什么目的呢?? 'st = pl;' –

+0

它对我很好用。 –

+0

显示“Capital is Washington D.C.”而不是(大写+“”+“是”+“”+ country +“”+“capital。”) – Arvind

回答

-1

Place类需要被定义为public

编辑:该文件还必须命名为Place.java

+1

是什么让你这么想?你能详细解释一下吗? – Pshemo

+0

@TomK你认为主要方法必须在公开课上? – Pshemo

+0

'main'方法不需要在公共类中。同样,如果最高级别的课程不公开,它不需要使用同名的文件,因此答案的两个方面都是错误的。我不知道谁在没有测试的情况下提出了答案。 – Pshemo

0
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 .. 

    } 
} 
+0

“* st.display(); //打印子类方法*”您可能意思是sup(super)类,因为“Place extends State”和“st”是“State”类型。换句话说,它与调用'State.display()'相同,因为'display'是静态方法。 – Pshemo

+0

@Arvind有没有更新? –

0

Overriding取决于有一个类的实例。 polymorphism的要点是您可以继承一个类,并且实现这些subclasses的对象将具有与superclass(并在subclasses中重写)中定义的相同方法不同的行为。静态方法不与任何类的任何实例关联,因此这个概念不适用。

推动Java设计的两个因素影响了这一点。其中一个问题是性能问题:Smalltalk对它的速度太慢了(垃圾回收和多态调用是其中的一部分),Java的创建者决心避免这种情况。另一个决定是Java的目标受众是C++开发人员。使静态方法按照他们的方式工作的好处是程序员熟悉C++,并且速度也非常快,因为不需要等到运行时才能确定要调用哪种方法。

I give you a small example : // and it is not overriding method.May be this is you actually expect

public class HelloWorld { 

    public static void main(String args[]) { 

     Company st = new Company(); 
     eBay pl = new eBay(); 
     st=pl; 
     //st.address(); 
     pl.address(); 

    } 
} 

class Company { 
    static String country; 
    static String capital; 

    Company() // Constructor 
    { 
     country = "America's"; 
     capital = "Washington D.C"; 

    } 

    static void address() { 
     System.out.println(capital + " " + "is" + " " + country + " " + "capital."); 

    } 

} 

class eBay extends Company { 

    public static void address() { 
     System.out.println("Capital is Washington D.C"); 
    } 
} 
+0

并将该文件保存为HelloWorld.java –