多态性:方法已更改,但参数值未更改。

huangapple go评论58阅读模式
英文:

Polymorphism: method is changed, but argument value not

问题

我运行了以下的代码:

public class Cycling {
    
    public static void main(String[] args) {
        
        Cycle unicycle = new Unicycle();
        System.out.println("Unicycle name: " + unicycle.name);
        unicycle.ride();
    }
}

class Cycle {
    public String name = "cycle";
    public void ride() {
        System.out.println("Ride cycle!");
    }
}

class Unicycle extends Cycle{
    public String name = "unicycle";
    public void ride() {
        System.out.println("Ride unicycle!");
    }
}

我期望的输出是:

Unicycle name: unicycle

Ride unicycle!

但是我得到了以下输出:

Unicycle name: cycle

Ride unicycle!

方法成功改变了,但参数的值没有。有人可以解释一下为什么吗?

英文:

I ran following code:

public class Cycling {
    
    public static void main(String[] args) {
        
        Cycle unicycle      = new Unicycle();
        System.out.println("Unicycle name: " + unicycle.name);
        unicycle.ride();
    }
}

class Cycle {
    public String name = "cycle";
    public void ride() {
        System.out.println("Ride cycle!");
    }
}

class Unicycle extends Cycle{
    public String name = "unicycle";
    public void ride() {
        System.out.println("Ride unicycle!");
    }
}

I expected this output:

Unicycle name: unicycle

Ride unicycle!

I got this output:

Unicycle name: cycle

Ride unicycle!

Method was successfully changed, but value of the argument was not. Can someone explain me why?

答案1

得分: 2

你的方法是多态的,但你的字段不是 - 因为字段不是多态的。

你目前有两个同名的字段,一个字段遮蔽了另一个。当你这样做时:

Cycle unicycle = new Unicycle();

Cycle 不知道 Unicycle 类中的 name。所以当你执行 unicycle.name 时,它指的是 Cycle 类中的那个。


建议:始终将你的字段设置为私有。


像 IntelliJ Idea 这样的集成开发环境会突出显示此错误。

英文:

Your method is polymorphic, your field is not - because fields are not polymorphic.

You currently have 2 fields with the same name, one field is shadowing another. When you do:

Cycle unicycle = new Unicycle();

Cycle has no idea about the name in the Unicycle class. So when you do unicycle.name, it refers to the one in Cycle class.


Advise: always keep your fields private.


An IDE like IntelliJ Idea would have highlighted this error.

答案2

得分: 2

这是编译时多态与运行时多态的经典案例。

编译器在编译时解析变量,但方法在运行时由JVM解析。

英文:

This is classic case of compile-time vs runtime polymorphism.

Compiler resolves the variables during compile time but methods are resolved by JVM during run time.

答案3

得分: 1

这是因为当你在子类中定义了同名的变量时,它将拥有仅共享名称的两个属性。这被称为隐藏。如果你想访问超类变量 "name" 的值,你可以通过 super.name 来调用它。

正如你所看到的,变量完全独立处理,它们也可以拥有完全不同的类型。

英文:

Thats because when you define a variable of the same name in the subclass, it will have both properties that only share their name. This is called hiding. If you want to access the value of the superclass variable "name", you can call it by super.name.

As you can see, that the variables are handled completely on their own, they can also have a completely different type.

huangapple
  • 本文由 发表于 2020年8月7日 15:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63297315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定