本帖最后由 java2000_net 于 2008-08-05 16:06:09 编辑

解决方案 »

  1.   

    Overloading a method is what you have already learned: providing more than one method with the same name but with different signatures to distinguish them.Overriding a method means replacing the superclass's implementation of a method with one of your own. The signatures must be identicalbut the return type can vary in a particular way, as discussed below.Overloading an inherited method simply means that you have added a new method, with the same name as, but a different signature from, an inherited method. In ColorAttr we have gone from having one setValue method to having two overloaded forms of the method.public Object setValue(Object newValue) {
        // ...
    }
    public ScreenColor setValue(ScreenColor newValue) {
        // ...
    }This is no different from having overloaded forms of a method declared in the same class.Overriding a method means that you have replaced its implementation so that when the method is invoked on an object of the subclass, it is the subclass's version of the method that gets invoked. In the ColorAttr class, we overrode the Attr class's setValue(Object) by providing a new setValue(Object) method in the ColorAttr class that uses the super keyword to invoke the superclass's implementation and then invokes decodeColor. The super reference can be used in method invocations to access methods from the superclass that are overridden in this class. You'll learn about super in detail on page 89.When you're overriding methods, the signature must be the same as in the superclassif they differ then it is an overload, not an override. The return type of an overriding method is allowed to vary in a specific way: If the return type is a reference type then the overriding method can declare a return type that is a subtype of that declared by the superclass method. Because a reference to a superclass can always hold a reference to an instance of a subclass, this variation is perfectly safeit is referred to as being a covariant return type. We'll see a good example of this when we look at object cloning on page 101. If the return type is a primitive type, then the return type of the overriding method must be identical to that of the superclass method. It is an error if two methods differ only in return type and the compiler will reject your class.
      

  2.   

    方法重载是让类以统一的方式处理不同类型数据的一种手段。Java的方法重载,就是在类中可以创建多个方法,它们具有相同的名字,但具有不同的参数和不同的定义。调用方法时通过传递给它们的不同个数和类型的参数来决定具体使用哪个方法, 这就是多态性。
    方法重写
    在Java中,子类可继承父类中的方法,而不需要重新编写相同的方法。但有时子类并不想原封不动地继承父类的方法,而是想作一定的修改,这就需要采用方法的重写。方法重写又称方法覆盖。 若子类中的方法与父类中的某一方法具有相同的方法名、返回类型和参数表,则新方法将覆盖原有的方法。 如需父类中原有的方法,可使用super关键字,该关键字引用了当前类的父类。
      

  3.   

    看过你的内容,而且我发的内容你看明白也就没这么多问题了.
     1. public void method(int i, String s) { 
      2.    // do something 
      3. } 
      4. 
      5. public String method(int i, String s) { 
      6.    // do something 
      7. }  
    这两个方法如果定义在一个类中,肯定无法区分,因为方法的签名相同(方法名称,参数个数,类型,顺序),两个方法不能通过返回类型来区分.所以编译错误.
    override指的是子类重写了父类的方法,两个方法签名相同,返回类型也相同.但是允许子类方法的返回类型是父类中同一方法的返回类型的子类.
    仔细看我上一个回复的说明.第二个问题你对数据结构的理解存在问题.数据结构包括类型的定义以及定义在这行类型上的操作.构造方法可以认为是一个特殊的方法,执行一些初始化动作,对field进行初始化.跟方法一样,当然可以重载.