Java中的不同类 [英] different classes in Java

查看:86
本文介绍了Java中的不同类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建不同的类并在主类中创建实例,然后运行该程序就这么简单,但是出现此错误:

I'm trying to make different classes and make instances in the main class then run the program just that simple, but i get this error:

shka.java:4: error: cannot find symbol
        ahmed c = new ahmed("Shika");
        ^
  symbol:   class ahmed
  location: class shka
shka.java:4: error: cannot find symbol
        ahmed c = new ahmed("Shika");
                      ^
  symbol:   class ahmed
  location: class shka
2 errors
error: compilation failed

这是代码
shka.java:

And here is the code shka.java:

public class shka {
    public static void main(String[] args) {
        System.out.println("Starting.. ");
        ahmed c = new ahmed("Shika");
        // c.name = "Shika";
        System.out.println(c.name);
    }
}

ahmed.java:

ahmed.java:

public class ahmed {
    public String name;

    // Constructor
    // This = self in python
    public ahmed(String name) {
        this.name = name;
    }

    public void msg() {
        String h = "BATTA";
        System.out.println("HELLO, " + h + " This is the other class");
    }
}


推荐答案

问题在于您在类中没有 package 语句。

The problem is that you don't have package statements in the classes.

没有 package 语句在默认(匿名)包中隐式声明。但是默认包中的一个类不会被默认包中的另一个类隐式导入。并且您不能明确地从默认包中导入...因为它没有名称。

A class without a package statement is implicitly declared in the default (anonymous) package. But a class in the default package is not implicitly imported by another class in the default package. AND you can't explicitly import from the default package ... because it has no name.

解决方案:


  1. 了解软件包。这是最重要的步骤

  2. 添加包裹语句

  3. 如果这些类位于不同的包中(您可以选择!),请根据需要添加 import 语句。

  1. Read about Packages. This is the most important step.
  2. Add package statements
  3. If the classes are in different packages (your choice!) add import statements as required.

这篇关于Java中的不同类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆