为什么向上转换 Class 不改变覆盖的方法?

Why is upcasting a Class not changing overridden methods?

我有一个继承自 class BritishPerson.

的子class ScottishPerson
class BritishPerson {
    public String name = "A british name";

    public void salute() {
        System.out.println("Good Morning!");
    }
}

class ScottishPerson extends BritishPerson {
    public String name = "A scottish name "; //Variable overriding
    public String clanName = "MacDonald";

    public void salute() //Method overriding
    {
        System.out.println("Madainn Mhath!");
    }

    public void warcry() {
        System.out.println("Alba Gu Brath!");
    }
}

class Driver {

    public static void main(String[] args) {
        ScottishPerson scottishPerson = new ScottishPerson(); //Created as a subclass, can always be upcasted.
        BritishPerson britishPerson = new BritishPerson(); //Created as the superclass, throws an error when downcasted.
        BritishPerson britishPersonUpcasted =
                new ScottishPerson(); //Created as the subclass but automatically upcasted, can be downcasted again.

        //Checking the methods and parameters of scottishPerson
        scottishPerson.salute();
        scottishPerson.warcry();
        System.out.println(scottishPerson.name);
        System.out.println(scottishPerson.clanName);

        //Checking the methods and parameters of britishPerson
        britishPerson.salute();
        System.out.println(britishPerson.name);

        //Checking the methods and parameters of britishPersonUpcasted
        britishPersonUpcasted.salute();
        System.out.println(britishPersonUpcasted.name);
    }
}

运行代码,这是输出。

Madainn Mhath!
Alba Gu Brath!
A scottish name 
MacDonald
Good Morning!
A british name
Madainn Mhath!
A british name

这就是困惑所在。将 ScottishPerson 向上转换为 BritishPerson 会将变量名称更改为 superclass 定义的名称。 warcry()clanName等仅存在于子class中的方法和变量将被丢弃。但是,在向上转换的 class 上调用方法 salute() 仍然是 returns 基于 subclass 实现的字符串。

是因为当我创建对象 britishPerson 时,我只初始化了 BritishPerson class,而当我创建对象 britishPersonUpcasted 时,我同时创建了 BritishPerson class 和 ScottishPerson class 导致永久覆盖 salute() 方法?

你实际调用方法的对象属于 ScottishPerson 所以在编译时它检查引用变量但运行时它总是执行属于对象的方法而不是引用持有它的对象.运行时多态性其实就是这个概念的背后。

  • 访问静态字段、实例字段和静态方法取决于引用变量class并且不是实际对象 变量指向的。
  • 请记住,成员变量是隐藏的,而不是被覆盖的。
  • 这与实例方法的情况相反。
    在实例方法的情况下,对象的实际class的方法被调用。

考虑以下示例。

    class ABCD {
        int x = 10;
        static int y = 20;

        public String getName() {
            return "ABCD";
        }
    }

    class MNOP extends ABCD {
        int x = 30;
        static int y = 40;

        public String getName() {
            return "MNOP";
        }
    }

    public static void main(String[] args) {

      System.out.println(new MNOP().x + ", " + new MNOP().y);

      ABCD a = new MNOP();
      System.out.println(a.x); // 10
      System.out.println(a.y); // 20
      System.out.println(a.getName()); // MNOP
    }

在您的场景中,britishPersonUpcasted 对象的 name 属性 被 BritishPerson 隐藏。

希望对您有所帮助。

Java 中的变量不是多态的。在 subclass 中声明的相同变量不会覆盖 super class 中的值。

要反映与 super class 关联的值,您需要将其传递给构造函数并使用 super 关键字设置 super class 变量。这样:

  public ScottishPerson(String name) {
    super.name = name;
    this.name = name;
  }

这是您的代码,我已对其进行修改以表明这一点。

class BritishPerson
{
  public String name = "A british name";

    public void salute()
    {
        System.out.println("Good Morning!");
    }

}


class ScottishPerson extends BritishPerson
{
  public String name = "A scottish name "; //Variable overriding
  public String clanName = "MacDonald";

  public ScottishPerson() {
    // TODO Auto-generated constructor stub
  }

  public ScottishPerson(String name) {
    super.name = name;
    this.name = name;
  }

    @Override
    public void salute() //Method overriding
    {
        System.out.println("Madainn Mhath!");
    }

    public void warcry()
    {
        System.out.println("Alba Gu Brath!");
    }

}

public class Driver {



  public static void main(String[] args) {
    // TODO Auto-generated method stub


    ScottishPerson scottishPerson = new ScottishPerson(); //Created as a subclass, can always be upcasted.
    BritishPerson britishPerson = new BritishPerson(); //Created as the superclass, throws an error when downcasted.
    BritishPerson britishPersonUpcasted = new ScottishPerson("Another scottish name"); //Created as the subclass but automatically upcasted, can be downcasted again.

    //Checking the methods and parameters of scottishPerson
    scottishPerson.salute();
    scottishPerson.warcry();
    System.out.println(scottishPerson.name);
    System.out.println(scottishPerson.clanName);

    //Checking the methods and parameters of britishPerson
    britishPerson.salute();
    System.out.println(britishPerson.name);

    //Checking the methods and parameters of britishPersonUpcasted
    britishPersonUpcasted.salute();
    System.out.println(britishPersonUpcasted.name);
  }

}

您可以在子类中声明一个与超类中同名的字段,从而隐藏它(不推荐)。

即使您想要它,也可以通过 super 关键字

访问它

请看一下这个问题,以便更好地理解向上转换和向下转换:

What is the difference between up-casting and down-casting with respect to class variable

我还做了一个查看向上转换行为的例子:

abstract class Animal 
{ 
    public void saySomething()
    {
        System.out.println("Some Animal sound");
    }

    public abstract void getTheBall();
}

class Horse extends Animal
{ 
    public void saySomething()
    {
        System.out.println("Neigh Neigh");
    }

    public void getTheBall()
    {
        System.out.println("I won't, Try a dog, I am a Horse!");
    }
}

class Dog extends Animal 
{ 
    public void saySomething()
    {
        System.out.println("woof woof, waon waon");
    }

    public void getTheBall()
    {
        System.out.println("huf huf, here it is!");
    }
}

public class Main 
{
    public static void main (String [] args) 
    {
        Dog dog = new Dog(); 
        Horse horse = new Horse();
        Animal animal = dog;
        Animal horseAnimal = new Horse();

        //upcasting
        Dog upcastedAnimal = upcastToDog(animal);
        dog.saySomething();
        dog.getTheBall();

        upcastedAnimal.saySomething();
        upcastedAnimal.getTheBall();

        horse.saySomething();
        horse.getTheBall();

        try {
            Dog upcastedDog = upcastToDog(horseAnimal);
        } catch (Exception ex){
            System.out.println(ex.getClass().getSimpleName() + ": Obviously a horse is not a dog!");
        }
    }

    public static Dog upcastToDog(Animal animal){
        return (Dog) animal;
    }
}

输出:

woof woof, waon waon
huf huf, here it is!
woof woof, waon waon
huf huf, here it is!
Neigh Neigh
I won't, Try a dog, I am a Horse!
ClassCastException: Obviously a horse is not a dog!

首先,java 如果尝试转换不兼容的类型,将抛出异常。

在可以转换的情况下,将始终从实际实例调用重写的方法。在你的例子中,实例是 ScottishPerson,所以方法将在 ScottishPerson 上调用,即使你在 BritishPerson 中持有它的引用。

你可以运行这里的例子https://repl.it/B83f/3

在 JLS 中,它被覆盖在这里“Narrowing Reference Conversion”,正如他的名字所暗示的那样,只有引用被缩小或扩大(或向上转换或向下转换)而不是实例。