我试图找到一个在java中扩展抽象类的类的示例,该类旨在基于所选的单选按钮和按钮单击执行操作。
说明:
创建交易表单以管理帐户余额。
创建交易选项:
存款增加平衡 支票减去余额 如果余额>检查然后从余额中减去检查 否则显示资金不足的消息 并从余额中减去服务费(10美元) 服务费(10美元)减去余额
使用Abstract Class Transaction创建类:
类存款 class ServiceCharge 班级检查 覆盖每个类中的抽象函数calculateBalance()以执行适当的平衡计算 在类Check.calculateBalance()
我将使用以下抽象类:
public abstract class Transaction {
/**
The Transaction class simulates a bank account.
*/
static final double SERVICE_CHARGE = 10.0;
double transactionamount = 0.0;
static double balance = 0.0; // Account balance
static double totalDeposits = 0.0;
static double totalChecks = 0.0;
static double totalServiceCharges = 0.0;
static int numberDeposits = 0;
static int numberChecks = 0;
static int numberServiceCharges = 0;
public Transaction() {
transactionamount = 0.0;
}
public Transaction(double amount) {
this.transactionamount = amount;
}
public Transaction(String str) {
transactionamount = Double.parseDouble(str);
}
public void deposit(double amount) {
balance += amount;
}
public void deposit(String str) {
balance += Double.parseDouble(str);
}
static public double getBalance() {
return balance;
}
abstract void calculateBalance() throws Exception;
}

这就是我所拥有的,我想要去工作:
首先列出扩展Transaction类的类,我的例子是Check class。我试图设置我的计算按钮和交易菜单选择,以根据单选按钮选择和文本字段数据传递信息。
package Assignment5;
public class Check extends Transaction {
public double checksBalance(double check) // makes alterations to Transaction variables once information is passed from text field.
{
check = transactionamount;
totalChecks = check + totalChecks; // increases total amount in checks.
balance = check + totalDeposits - totalChecks - totalServiceCharges;
return check;
}
public int checks() {
return numberChecks++; // increases number of checks written.
}
@Override
void calculateBalance() throws Exception {
// TODO Auto-generated method stub
}
}
public class TypePanel extends JPanel
{
public String deposit, check, service;
JRadioButton depositButton;
JRadioButton checkButton;
JRadioButton serviceButton;
ButtonGroup radioButtonGroup;
public TypePanel() //
{
setLayout(new GridLayout(3, 1)); // Panel Layout
depositButton = new JRadioButton("Deposit", true); // Radiobuttons listed here.
checkButton = new JRadioButton("Check");
serviceButton = new JRadioButton("Service Charge");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(depositButton);
radioButtonGroup.add(checkButton);
radioButtonGroup.add(serviceButton);
add(depositButton);
add(checkButton);
add(serviceButton);
}
public void GetTotals()
{
if (checkButton.isSelected())
{
Transaction.transactionamount = AmountPanel.amountEntered;
}
}
public class AmountPanel extends JPanel {
public static Object amountEntered;
public String amountInput;
private JLabel amountLabel;
private String defaultText;
private JTextField amountTextField;
public AmountPanel()
{
setLayout(new GridLayout(2, 1)); // Panel Layout.
amountLabel = new JLabel("Enter Amount");
amountTextField = new JTextField(6);
defaultText = "0"; // Default value placed in textfields within this panel.
amountTextField.setText(defaultText);
add(amountLabel);
add(amountTextField);
}
public double GetAmount()
{
double amountEntered;
amountInput = amountTextField.getText();
amountEntered = Double.parseDouble(amountInput);
return amountEntered;
}
}

我没有包含我的所有代码,但是我试图将从文本字段输入的数量输入到Check类中的方法,这样它们就会反过来影响交易类。我不认为我是以最好的方式做到这一点。
答案 0 :(得分:0)
你编写的任何扩展Transaction的类应该是抽象本身,或者至少是实现void calculateBalance()抛出Exception; 。 “抽象”的要点是实现派生类中常见的方法,然后实现派生类中不同的方法。它与实际扩展任何其他类相同,只是如果您将抽象类扩展为非抽象类,则必须实现抽象类中的任何抽象方法。
如果需要,您可以通过简单地重新实现它们来替换其他一些函数,并且可以通过在方法中添加@Override注释来强制它们在调用时使用扩展类的实现。
好的读物:https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html