符号无法找到java

时间:2014-05-09 14:01:08

标签: java

我刚刚开始学习一些java guyz这是我创建的第一个类而且卡住了我在尝试编译类NameTest.java时仍然遇到错误,如符号未找到

classes.java

 public class classes
     {
     // defining classes 
     public class Name
     {
       //class members or variables or fields
        private String first;
        private String middle;
        private String last;    
        //constructor methods
        public Name(String f, String m, String l)
        {
            // assigning  letters
            first = f;
            middle = m;
            last = l;
        }
        //default constructor has no parameters
        public Name()
        {
            first = "";
            middle = "";
            last = "";
        }
        public String toString()
        {
            return first + " " + middle + " " + last;
        }
        /* class methods
        method name(public) method  return type string and method name displayName
        methods r like operations in a class */
        public String displayName()
        {
         return first + " " + middle + " " + last;
        }
     public String getInitials()
      {
          //substring for getting a sub sting in a string
          //(0,1) 0 is for the position for the character in a sting 
          // 1 is the number of characters to b  pulled from a substring
         return first.substring(0,1) + middle.substring(0,1) + last.substring(0,1);

      }
     }
        public static void main(String[] args)
      {

      }
    }

NameTest.java

public class NameTest
{
 public static void main(String[] args)
  {
     //instantiation
      Name myName = new Name("smart", "daniel", "zulu");
      Name yourName = new Name("daniel", "Zulu");
      Name aName = new Name("Zulu");
      System.out.println("myName: " + myName.toString());
  }
}

我需要一些如何解决错误的帮助

2 个答案:

答案 0 :(得分:3)

问题是Nameclasses的内部类,因此除非您有Name的对象引用,否则无法直接访问classes

要简单修复当前代码,请将NameTest中的代码重写为:

classes c = new classes();
classes.Name myName = c.new Name("smart", "daniel", "zulu");
classes.Name yourName = c.new Name("daniel", "Zulu");
classes.Name aName = c.new Name("Zulu");
System.out.println("myName: " + myName.toString());

更好的选择是将Name类作为顶级类移动,然后代码将按预期编译。

//rename classes.java file to Name.java as well
//public class classes
//     {
     // defining classes 
     public class Name
     {
     }
//}

答案 1 :(得分:1)

最佳解决方案: 放弃班级"班级"在代码之上,创建一个名为" Name.java"的文件。只包含类"名称"。您也可以删除"静态main()"类中的方法"名称"。

或者您可以更改代码:

new Name()

new classes.new Name()

自"姓名"是类的内部类"类"

编辑:根据Luiggi Mendoza的评论更正了代码。谢谢你的纠正