找不到 z 变量(3D 点 - Java 24 小时)

Cannot find z variable (3D Points - Java 24 hours)

我刚刚开始学习 Java 如果这是一个菜鸟问题,我很抱歉,但我已经检查了每一行代码,但我不明白哪里出了问题。代码来自书"Java in 24 hours".

代码的目的是获取一个起始的 2D 和 3D 点,然后移动它们并平移它们。首先它要求我制作一个 3D 点 class:

package com.java24hours;

import java.awt.*;

public class Point3D extends Point
{
    public int z;

    public Point3D(int x, int y, int z)
    {
        super(x,y);
        this.z = z;
    }
    public void move(int x, int y, int z)
    {
        this.z = z;
        super.move(x,y);
    }
    public void translate(int x, int y, int z)
    {
        this.z += z;
        super.translate(x,y);
    }
}

然后调用测试器 class,它使用 Points3D 移动和平移 3D 点:

package com.java24hours;

import java.awt.*;

class PointTester
{
    public static void main(String[] arguments)
    {
        Point location1 = new Point(11,22);
        Point location2 = new Point3D(7,6,64);       

        System.out.println("The 2D point is at ("+location1.x + "," + location1.y +")");

        System.out.println("It's being moved to (4,11)");
        location1.move(4,11);
        System.out.println("The 2D point is now at (" + location1.x + "," + location1.y + ")");

        System.out.println("It's now being moved -10 in both the x and y axis");
        location1.translate(-10,-10);
        System.out.println("The 2D point is now at (" + location1.x +"," + location1.y + ")\n");

        System.out.println("The 3D point is at (" + location2.x + "," + location2.y + "," + location.z + ")");

        System.out.println("It's being moved to (10,22,71)");
        location2.move(10,22,71);
        System.out.println("The 3D point is now at (" + location2.x + "," + location2.y + "," + location2.z + ")");

        System.out.println("It's now going to be moved -20 units in the x y and z axis");
        location2.translate(-20,-20,-20);
        System.out.println("It's now at (" + location2.x + "," + location2.y + "," + location2.z + ")");
    }
}

这会在包含 location2.z 的行上产生以下错误:

找不到符号 方法移动不能应用于给定类型 方法翻译不能应用于给定类型

来源在这里: https://www.informit.com/library/content.aspx?b=STY_Java2_24hours&seqNum=140

这大概是它应该给我的(我改变了一些措辞):

The 2D point is located at (11, 22) 
    It's being moved to (4, 13) 
The 2D point is now at (4, 13) 
    It's being moved -10 units on both the x and y axes 
The 2D point ends up at (-6, 3) 

The 3D point is located at (7, 6, 64) 
    It's being moved to (10, 22, 71) 
The 3D point is now at (10, 22, 71) 
    It's being moved -20 units on the x, y and z axes 
The 3D point ends up at (-10, 2, 51) 

我想我的困惑是我在 Point3D 中声明了 z 变量 class,我创建了接受三个变量的 newPoint3D,然后当我稍后尝试使用第三个变量时它不能找到它。

谢谢

您的代码中存在语法问题,因为您使用的是 location.z 而不是 location2.z

此外,您正在使用 Point class 作为参考来创建 Point3D 的对象。由于 多态性概念 (我建议您研究一下),您无法从 Point class 访问变量 z 作为 Point3D继承 Point class 等,变量 zPoint3D class 而不是 Point 中定义。

最后,您应该实例化您的 3D 点,如下所示:

Point3D location2 = new Point3D(7,6,64); 

发生这种情况是因为您已将 location2 变量声明为一个点。它的实际类型是 Point3D,但您将其引用为 Point 类型,因此 Point3D 变量 z 不可见。 (这是面向对象编程的基本原则,称为多态性,但不要担心,因为您才刚刚开始学习!)

您可以通过将 location2 变量声明为 Point3D 来解决此问题,如下所示:

Point location1 = new Point(11,22);

//here's the fixed line of code.
Point3D location2 = new Point3D(7,6,64);

//[...] rest of your code

我假设基础 Pointmove() 方法接受两个参数。但是,您使用三个调用该方法:location2.move(10,22,71);

当然,知道location2中包含的Point实际上是一个Point3D,但编译器认为它只是一个常规Point。将 location2 的类型更改为 Point3D,你应该没问题。

问题出在这一行:

    Point location2 = new Point3D(7,6,64);

您已经创建了一个 Point3D 实例并将其分配给 Point 变量。当前在 location2 中的对象此时有一个 z 字段,但是由于您将该变量声明为 Point ...编译器必须假定它是一个 Point 实例,它没有 z 字段。

因此编译器告诉你 location2.z 是一个编译错误。

修复是这样的:

    Point3D location2 = new Point3D(7,6,64);

好吧,为什么 Java 设计成不允许这样做?

考虑这样一个例子:

    Point location2 = test ? new Point3D(7,6,64) : new Point(7,6);
    System.out.println(location2.z);

如果编译器接受了,并且 testfalse,会发生什么?在动态类型语言中,这会给您一个运行时错误,因为您试图访问一个不存在的字段。但是 Java 是设计的静态类型,这是不可接受的。

事实上,Java认为当你声明一个变量的类型为Point时,下面的代码只能将它的值视为Point.

要么在Point中定义'x'和'move',要么通过Point3D

引用location1和location2