覆盖构造函数类 [英] Override a constructor class

查看:200
本文介绍了覆盖构造函数类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码。我没有得到什么是错误。任何人都可以指导。

Below is my code. I am not getting what is the mistake. Can anyone be able to guide.

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;

    }
}

运行时显示为错误:无法找到或加载主类State $ Place

While running it displays as "Error: Could not find or load main class State$Place"


由于输出需要:Capital is Washington DC而不是(大写+
+是++国家++大写。)

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


推荐答案

覆盖取决于拥有一个类的实例。 多态的要点是你可以对一个类进行子类化,实现那些子类的对象会有不同的行为超类中定义的方法(并在子类中重写)。静态方法不与类的任何实例相关联,因此该概念不适用。

Overriding depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviours for the same methods defined in the superclass (and overridden in the subclasses). A static method is not associated with any instance of a class so the concept is not applicable.

推动Java设计的两个因素影响了这一点。一个是对性能的担忧:有很多人批评Smalltalk关于它太慢(垃圾收集和多态调用是其中的一部分)而Java的创建者决心避免这种情况。另一个决定是Java的目标受众是 C ++ 开发人员。使静态方法按照他们的方式工作,对于 C ++ 程序员的熟悉程度也非常快,因为没有必要等到运行时才能找出要调用的方法。

There were two considerations driving Java's design that impacted this. One was a concern with performance: there had been a lot of criticism of Smalltalk about it being too slow (garbage collection and polymorphic calls being part of that) and Java's creators were determined to avoid that. Another was the decision that the target audience for Java was C++ developers. Making static methods work the way they do had the benefit of familiarity for C++ programmers and was also very fast, because there's no need to wait until runtime to figure out which method to call.


我给你一个小例子://它不是重写方法。可能是
这是你真正期望的

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

这篇关于覆盖构造函数类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆