Java导入给定错误
再一次,我是一个JavaN00B,我试图从头开始学习,遇到了一些令人尴尬的问题 我得到了一个Account类,如下所示:Account.javaJava导入给定错误,java,Java,再一次,我是一个JavaN00B,我试图从头开始学习,遇到了一些令人尴尬的问题 我得到了一个Account类,如下所示:Account.java public class Account { protected double balance; // Constructor to initialize balance public Account( double amount ) { balance = amount; } // Overloade
public class Account
{
protected double balance;
// Constructor to initialize balance
public Account( double amount )
{
balance = amount;
}
// Overloaded constructor for empty balance
public Account()
{
balance = 0.0;
}
public void deposit( double amount )
{
balance += amount;
}
public double withdraw( double amount )
{
// See if amount can be withdrawn
if (balance >= amount)
{
balance -= amount;
return amount;
}
else
// Withdrawal not allowed
return 0.0;
}
public double getbalance()
{
return balance;
}
}
import Account;
class InterestBearingAccount extends Account
{
// Default interest rate of 7.95 percent (const)
private static double default_interest = 7.95;
// Current interest rate
private double interest_rate;
// Overloaded constructor accepting balance and an interest rate
public InterestBearingAccount( double amount, double interest)
{
balance = amount;
interest_rate = interest;
}
// Overloaded constructor accepting balance with a default interest rate
public InterestBearingAccount( double amount )
{
balance = amount;
interest_rate = default_interest;
}
// Overloaded constructor with empty balance and a default interest rate
public InterestBearingAccount()
{
balance = 0.0;
interest_rate = default_interest;
}
public void add_monthly_interest()
{
// Add interest to our account
balance = balance +
(balance * interest_rate / 100) / 12;
}
}
我试图使用extends来继承这个类中的方法和变量。所以,我使用了InterestBearingAccount.java
public class Account
{
protected double balance;
// Constructor to initialize balance
public Account( double amount )
{
balance = amount;
}
// Overloaded constructor for empty balance
public Account()
{
balance = 0.0;
}
public void deposit( double amount )
{
balance += amount;
}
public double withdraw( double amount )
{
// See if amount can be withdrawn
if (balance >= amount)
{
balance -= amount;
return amount;
}
else
// Withdrawal not allowed
return 0.0;
}
public double getbalance()
{
return balance;
}
}
import Account;
class InterestBearingAccount extends Account
{
// Default interest rate of 7.95 percent (const)
private static double default_interest = 7.95;
// Current interest rate
private double interest_rate;
// Overloaded constructor accepting balance and an interest rate
public InterestBearingAccount( double amount, double interest)
{
balance = amount;
interest_rate = interest;
}
// Overloaded constructor accepting balance with a default interest rate
public InterestBearingAccount( double amount )
{
balance = amount;
interest_rate = default_interest;
}
// Overloaded constructor with empty balance and a default interest rate
public InterestBearingAccount()
{
balance = 0.0;
interest_rate = default_interest;
}
public void add_monthly_interest()
{
// Add interest to our account
balance = balance +
(balance * interest_rate / 100) / 12;
}
}
我在尝试编译时收到一个错误,提示导入错误“.”。所有文件都在同一个文件夹中
我做了javac-cp。Interest BearingAccount如果所有文件都在同一文件夹/包中,则无需进行导入。如果您的类在同一个包中,则无需导入。否则,您应该导入包+类名。将InterestBearingAccount类公开,如
public class InterestBearingAccount {}
定义类时,可以选择在文件顶部包含
包
语句。这就要求类所属的包与其在文件系统中的位置相关联。例如,包com.foo
中的公共类Account
应在以下文件层次结构中定义:
com
|
|--foo
|
|--Account.java
由于您省略了
package
语句,所以您的两个类都属于匿名包。对于属于同一包的类,无需导入类来引用它们;这只是不同包中的类的一个要求。如果它们在同一个包中,则不需要导入。那么,包到底是什么?它是一个包含所有必需类的文件夹吗?你知道如何在互联网上搜索,对吗?package是文件夹的名称?文件夹的层次结构表示包名称。在上面的示例中,包是com.foo,因此您将在Account.java中添加“package com.foo'”作为第一行。