我会尽可能清楚地解释这一点。我试图通过将三个使用的类转换为程序将通过import语句访问的包来缩小我的代码以进行赋值。这是我的原始代码:
import javax.swing.JOptionPane;
public class AssignmentTen
{
public static void main (String[] args)
{
System.out.println();
int num = Integer.parseInt(args[0]);
int eNumber;
String input2;
String input3;
String input4;
String input5;
String input6;
int input7;
int input8;
int input9;
int input10;
Employee[] employees = new Employee[num];
for (int i = 0; i < num; i++)
{
eNumber = getInt ("Enter Employee Number:");
input2 = getString ("Enter Employee First Name:");
input3 = getString ("Enter Employee Last Name:");
input4 = getString ("Enter Employee Street:");
input5 = getString ("Enter Employee City:");
input6 = getString ("Enter Employee State (Initials):");
input7 = getInt ("Enter Employee Zip Code (5 Digits):");
input8 = getInt ("Enter Employee Hire Month (MM):");
input9 = getInt ("Enter Employee Hire Day (DD):");
input10 = getInt ("Enter Employee Hire Year(YYYY):");
Name name = new Name(input2, input3);
Address address = new Address (input4, input5, input6, input7);
Date hireDate = new Date (input8, input9, input10);
employees[i] = new Employee (eNumber, name, address, hireDate);
System.out.println("#" + employees[i].empNumber + "\n" + employees[i].empName + "\n" + employees[i].empAddress + "\nHire Date: " + employees[i].empHireDate + "\n\n");
}
}
public static int getInt(String paramString)
{
String str = JOptionPane.showInputDialog(paramString);
return Integer.parseInt(str);
}
public static String getString(String paramString)
{
String str = JOptionPane.showInputDialog(paramString);
return str;
}
}
class Employee
{
Number empNumber;
Name empName;
Address empAddress;
Date empHireDate;
public Employee(Number empNumber, Name empName, Address empAddress, Date empHireDate)
{
this.empNumber = empNumber;
this.empName = empName;
this.empAddress = empAddress;
this.empHireDate = empHireDate;
}
}
class Name
{
String firstName;
String lastName;
Name(String first, String last)
{
firstName = first;
lastName = last;
}
public String toString()
{
return firstName + " " + lastName;
}
}
class Address
{
String eStreet;
String eCity;
String eState;
int eZipCode;
Address(String street, String city, String state, int zipCode)
{
eStreet = street;
eCity = city;
eState = state;
eZipCode = zipCode;
}
public String toString()
{
return eStreet + " " + eCity + " " + eState + " " + eZipCode;
}
}
class Date
{
int month;
int day;
int year;
Date(int eMonth, int eDay, int eYear)
{
month = eMonth;
day = eDay;
year = eYear;
}
public String toString()
{
return month + "/" + day + "/" + year;
}
}
我需要使用Name类,Address类和Date类,并将它们放入名为util的包中。首先,我按照我的指示将所有java程序组织在一个名为My Java Programs(C:\ MyJavaPrograms)的文件夹中。为了创建我认为应该是包的东西,我复制了每个类并将它们放入各自的文件中,我将语句'package.util'放在顶部,然后我将它们命名为Name.java,Address.java ,和Date.java,我把它们放在一个名为util的文件夹中,该文件夹位于C:\ MyJavaPrograms。
之后,我把语句'import util。*;'在我的原始代码的顶部,并将其放在C:\ MyJavaPrograms中。由于某种原因,该文件将无法编译。我已经为我的问题做了很多谷歌搜索,包括reading up on this,这对我没有帮助,至少在我理解错误的程度上。
我怀疑我的类路径有问题,虽然我不知道我能做些什么来修复它。如果它有助于解决问题,我知道我无法在命令提示符中使用javac命令而无需转到this thread并输入其中一个响应。
理想情况下,我想编译我的新任务并让它完全按照我在此问题顶部发布的代码执行的操作,除非它在使用带有额外类的pacakage时执行此操作。如果有人能把我推向正确的方向,我真的很感激。
答案 0 :(得分:0)
答案 1 :(得分:0)
1)将类分解为单独的文件,并将文件组织到包中是一件好事。
2)假设您想将类分成“Assignment10.java”和“Util.java”。他们都可以在“com.myclass”包中。你会做以下事情:
a)创建Assignment10.java和Util.java文件
b)放入“package com.myclass;”
的顶部c)创建一个新目录“com \”和子目录“myclass \”
d)将* .java文件复制到com \ myclass
e)从根目录编译并运行
3)如果您使用的是IDE(如Eclipse或Netbeans),它会自动为您创建目录
以下是一些很好的教程:
----------------------------- ADDENDUM ----------------- ------------
使用命令行中的包编译和运行Java程序:
1)创建包文件夹:
mkdir myclass\util
2)创建.java文件
cd myclass\util
notepad AssignmentTen.java =>
package myclass.util;
public class AssignmentTen {
public static void main (String[] args) {
System.out.println ("In main...");
Name name = new Name("Jack", "Torrance");
System.out.println ("Exiting main.");
}
}
notepad Name.java =>
package myclass.util;
public class Name {
public Name (String first, String last) {
System.out.println ("My name is " + first + " " + last + "...");
}
}
3)编译
javac *.java
dir =>
10/04/2013 10:38 AM 589 AssignmentTen.class
10/04/2013 10:36 AM 248 AssignmentTen.java
10/04/2013 10:38 AM 593 Name.class
10/04/2013 10:35 AM 177 Name.java
4)执行
cd ..\..
java myclass.util.AssignmentTen =>
In main...
My name is Jack Torrance...
Exiting main.