当前位置: 技术问答>java相关
小弟初学java,今遇一难题,望众高手解答。
来源: 互联网 发布时间:2015-07-29
本文导语: 我初学package,试写一程序,但就是无法编译: Account.java package banking; public class Account { private double balance; public Account(double bal) { balance = bal; } ...
我初学package,试写一程序,但就是无法编译:
Account.java
package banking;
public class Account {
private double balance;
public Account(double bal) {
balance = bal;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
balance = balance - amount;
}
}
此类存于banking子目录中
TestBanking.java
/*
* This class creates the program to test the banking classes.
* It creates a new Bank, sets the Customer (with an initial balance),
* and performs a series of transactions with the Account object.
*/
import banking.*;
public class TestBanking {
public static void main(String[] args) {
Account account;
// Create an account that can has a 500.00 balance.
System.out.println("Creating an account with a 500.00 balance.");
account = new Account(500.00);
System.out.println("Withdraw 150.00");
account.withdraw(150.00);
System.out.println("Deposit 22.50");
account.deposit(22.50);
System.out.println("Withdraw 47.62");
account.withdraw(47.62);
// Print out the final account balance
System.out.println("The account has a balance of " + account.getBalance());
}
}
此文件在banking子目录或banking子目录所在的目录里都放过,都不行~~~~~~~
编译时总是报错,说是找不到package banking和class Account,我把classpath设为banking子目录,还是不行~~~~~~
小弟早起折腾了许久,终不解,望众高手指点迷津,救小弟于水火之中~~~~~~~~
Account.java
package banking;
public class Account {
private double balance;
public Account(double bal) {
balance = bal;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
balance = balance - amount;
}
}
此类存于banking子目录中
TestBanking.java
/*
* This class creates the program to test the banking classes.
* It creates a new Bank, sets the Customer (with an initial balance),
* and performs a series of transactions with the Account object.
*/
import banking.*;
public class TestBanking {
public static void main(String[] args) {
Account account;
// Create an account that can has a 500.00 balance.
System.out.println("Creating an account with a 500.00 balance.");
account = new Account(500.00);
System.out.println("Withdraw 150.00");
account.withdraw(150.00);
System.out.println("Deposit 22.50");
account.deposit(22.50);
System.out.println("Withdraw 47.62");
account.withdraw(47.62);
// Print out the final account balance
System.out.println("The account has a balance of " + account.getBalance());
}
}
此文件在banking子目录或banking子目录所在的目录里都放过,都不行~~~~~~~
编译时总是报错,说是找不到package banking和class Account,我把classpath设为banking子目录,还是不行~~~~~~
小弟早起折腾了许久,终不解,望众高手指点迷津,救小弟于水火之中~~~~~~~~
|
把TestBanking.java放banking外
退到banking外
javac banking/Account.java
javac TestBanking.java
退到banking外
javac banking/Account.java
javac TestBanking.java
|
在TestBanking.java中去掉import,加上package banking;。
呵呵,成与不成就看你自己了。
呵呵,成与不成就看你自己了。
|
你只要在TestBanking.java中的import banking.*;前面加上package banking;问题就可以解决了!