新声明前面的Java关键字

Ref*_*lon 1 java

我想学习一些Java.

当我运行以下代码时,它的工作原理:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

/**
 *
 * @author me
 */
public class Test {

    int myAge;

    public getAge( ){
        system.out.println(myAge);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Test Tester = new Test();
        Tester.myAge = 5;

        System.out.println(Tester.myAge);
    }

}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我的主要功能中有一个Test Tester.当我这样删除它:

Tester = new Test();
Run Code Online (Sandbox Code Playgroud)

它不起作用了.为什么我应该在此功能之前添加Test.记住,我在Java编程中仍然是非常棒的(这是我的第一个程序)所以要温柔.

Bat*_*eba 7

您需要告诉编译器什么类型 Tester.

您可能认为编译器可以在您创建的情况下解决这个问题Test(),但它不能.这是因为Tester可以有很多东西; 通常是实现的接口Test(),或基类.

你也可以写java.lang.Object Tester = new Test().