我使用以下代码创建了源文件 .java:
// bank.java
// demonstrates basic OOP syntax
// to run this program: C>java BankApp
////////////////////////////////////////////////////////////////
class BankAccount
{
private double balance; // account balance
public BankAccount(double openingBalance) // constructor
{
balance = openingBalance;
}
public void deposit(double amount) // makes deposit
{
balance = balance + amount;
}
public void withdraw(double amount) // makes withdrawal
{
balance = balance - amount;
}
public void display() // displays balance
{
System.out.println(“balance=” + balance);
}
} // end class BankAccount
////////////////////////////////////////////////////////////////
class BankApp
{
public static void main(String[] args)
{
BankAccount ba1 = new BankAccount(100.00); // create acct
System.out.print(“Before transactions, “);
ba1.display(); // display balance
ba1.deposit(74.35); // make deposit
ba1.withdraw(20.00); // make withdrawal
System.out.print(“After transactions, “);
ba1.display(); // display balance
} // end main()
} // end class BankApp
我使用命令“javac”编译它并得到两个类文件——每个文件对应一个类。我尝试使用 cmd 运行它,但输入后出现错误“访问被拒绝”:
C:\new>java -cp <C:\new> BankApp.class
输入后:
C:\new>java BankApp.class
我得到:错误:找不到或加载主类 BankApp.class