Java 抽象类超类子类
我有一个名为Computer的抽象类,还有一个在抽象类中扩展的PC类,最后还有一个在抽象类中扩展的名为Server的类。我的问题是我想在文本文件中写一台新电脑和一台新服务器 我的Abstract代码是Java 抽象类超类子类,java,Java,我有一个名为Computer的抽象类,还有一个在抽象类中扩展的PC类,最后还有一个在抽象类中扩展的名为Server的类。我的问题是我想在文本文件中写一台新电脑和一台新服务器 我的Abstract代码是 public abstract class Computer { private final String ram; private final String hdd; private final String cpu; public Computer(
public abstract class Computer {
private final String ram;
private final String hdd;
private final String cpu;
public Computer(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
public String getRAM() {
return this.ram;
}
public String getHDD() {
return this.hdd;
}
public String getCPU() {
return this.cpu;
}
@Override
public String toString() {
return "RAM= " + this.getRAM() + ", HDD=" + this.getHDD() + ", CPU=" + this.getCPU();
}
public class PC extends Computer {
private final String rom;
public PC(String ram, String hdd, String cpu, String rom) {
super(ram, hdd, cpu);
this.rom = rom;
}
public String getROM() {
return rom;
}
@Override
public String toString() {
return super.toString() + ", ROM=" + this.getROM();
}
}
public class Server extends Computer {
public Server(String ram, String hdd, String cpu) {
super(ram, hdd, cpu);
}
}
}
添加新电脑的代码为
public class AddPCWindow extends JFrame implements ActionListener {
private final SelectComputerWindow select_computer_window;
ArrayList<Computer> computer_list;
File fileName;
private final JTextField ramText = new JTextField();
private final JTextField hddText = new JTextField();
private final JTextField cpuText = new JTextField();
private final JTextField romText = new JTextField();
private final JButton addBtn;
private final JButton cancelBtn;
FileWriter fileWriter;
public AddPCWindow(SelectComputerWindow select_computer_window) {
this.addBtn = new JButton("Add");
this.cancelBtn = new JButton("Cancel");
this.select_computer_window = select_computer_window;
computer_list = new ArrayList<>();
this.fileName = new File("test.txt");
initialize();
}
private void initialize() {
JFrame frame = new JFrame("Add PC");
frame.setVisible(true);
frame.setBounds(600, 200, 500, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6, 3));
panel.add(new JLabel("RAM : "));
panel.add(ramText);
panel.add(new JLabel("HDD : "));
panel.add(hddText);
panel.add(new JLabel("CPU : "));
panel.add(cpuText);
panel.add(new JLabel("ROM : "));
panel.add(romText);
panel.add(addBtn);
panel.add(cancelBtn);
frame.add(panel);
addBtn.addActionListener(this);
cancelBtn.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addBtn) {
try {
FileWriter fw = new FileWriter(fileName, true);
try (Writer output = new BufferedWriter(fw)) {
String ram = ramText.getText();
String hdd = hddText.getText();
String cpu = cpuText.getText();
String rom = romText.getText();
computer_list.add(new Computer(ram, hdd, cpu, rom));
output.write("PC" + "\n");
for (int i = 0; i < computer_list.size(); i++) {
output.write(computer_list.get(i) + "\n");
}
}
JOptionPane.showMessageDialog(null, "Correct");
} catch (IOException ex) {
}
}
if (e.getSource() == cancelBtn) {
System.exit(0);
}
}
}
公共类AddPCWindow扩展JFrame实现ActionListener{
私人最终选择计算机窗口选择计算机窗口;
ArrayList计算机列表;
文件名;
私有最终JTextField ramText=新JTextField();
私有最终JTextField hddText=新JTextField();
私有最终JTextField cpuText=新JTextField();
私有最终JTextField romText=新JTextField();
私有最终JButton addBtn;
专用最终按钮取消BTN;
文件编写器文件编写器;
公共添加PC窗口(选择计算机窗口选择计算机窗口){
this.addBtn=新的JButton(“添加”);
this.cancelBtn=新的JButton(“取消”);
this.select\u computer\u window=选择\u computer\u window;
计算机列表=新的ArrayList();
this.fileName=新文件(“test.txt”);
初始化();
}
私有void初始化(){
JFrame=新JFrame(“添加PC”);
frame.setVisible(true);
机架立根(600200500300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel面板=新的JPanel();
面板设置布局(新网格布局(6,3));
添加(新的JLabel(“RAM:”);
面板。添加(文本);
面板。添加(新的JLabel(“硬盘:”);
panel.add(hddText);
添加(新的JLabel(“CPU:”);
面板。添加(cpuText);
添加(新JLabel(“ROM:”);
面板。添加(文本);
面板。添加(addBtn);
面板。添加(取消BTN);
框架。添加(面板);
addBtn.addActionListener(此);
cancelBtn.addActionListener(此);
}
@凌驾
已执行的公共无效操作(操作事件e){
如果(例如getSource()==addBtn){
试一试{
FileWriter fw=新的FileWriter(文件名,true);
try(Writer输出=新的BufferedWriter(fw)){
字符串ram=ramText.getText();
字符串hdd=hddText.getText();
字符串cpu=cpuText.getText();
字符串rom=romText.getText();
计算机列表。添加(新计算机(ram、hdd、cpu、rom));
输出。写入(“PC”+“\n”);
对于(int i=0;i
我建议将所有公共字段移动到抽象类以避免重复。
之后,使PC和服务器类与Computer类一起继承,并将特定于PC的字段添加到PC类
请注意,抽象类也可以有字段和方法实现。尽量避免字段和方法实现重复
通过您的设置,您只需将字段添加到PC类,其他类的实例将无法访问此字段。我建议将所有公共字段移动到抽象类,以避免重复。 之后,使PC和服务器类与Computer类一起继承,并将特定于PC的字段添加到PC类 请注意,抽象类也可以有字段和方法实现。尽量避免字段和方法实现重复
通过您的设置,您只需将字段添加到PC类中,其他类的实例将无法访问此字段。如果您不想使用此字段,只需删除它,然后您可以将其余字段移动到抽象类中。要执行此操作,还必须重写toString方法:
public abstract class Computer {
private final String ram;
private final String hdd;
private final String cpu;
public Computer(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
public String getRAM() {
return this.ram;
}
public String getHDD() {
return this.hdd;
}
public String getCPU() {
return this.cpu;
}
@Override
public String toString() {
return "RAM= " + this.getRAM() + ", HDD=" + this.getHDD() + ", CPU=" + this.getCPU();
}
}
public class PC extends Computer {
private final String rom;
public PC(String ram, String hdd, String cpu, String rom) {
super(ram, hdd, cpu);
this.rom = rom;
}
public String getROM() {
return rom;
}
@Override
public String toString() {
return super.toString() + ", ROM=" + this.getROM();
}
}
public class Server extends Computer {
public Server(String ram, String hdd, String cpu) {
super(ram, hdd, cpu);
}
}
如果您不想拥有这个字段,只需删除它,就可以将其余字段移动到抽象类中。要执行此操作,还必须重写toString方法:
public abstract class Computer {
private final String ram;
private final String hdd;
private final String cpu;
public Computer(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
public String getRAM() {
return this.ram;
}
public String getHDD() {
return this.hdd;
}
public String getCPU() {
return this.cpu;
}
@Override
public String toString() {
return "RAM= " + this.getRAM() + ", HDD=" + this.getHDD() + ", CPU=" + this.getCPU();
}
}
public class PC extends Computer {
private final String rom;
public PC(String ram, String hdd, String cpu, String rom) {
super(ram, hdd, cpu);
this.rom = rom;
}
public String getROM() {
return rom;
}
@Override
public String toString() {
return super.toString() + ", ROM=" + this.getROM();
}
}
public class Server extends Computer {
public Server(String ram, String hdd, String cpu) {
super(ram, hdd, cpu);
}
}