简体   繁体   中英

Abstract class-Super Class-SubClass

I have an abstract class named Computer, also i have a class PC which is extended in abstract class and finally i have a class named Server which is also extended in abstract class. My problem is that i want to write in text file one new PC and one new Server My abstarct code is

    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);
        }

    }
}

Code for add new PC is

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);

            }

        }

    }

I suggest moving all common fields to abstract class to avoid duplication. After that make PC and Server classes inherited with Computer class and add fields specific for PC to PC class.

Be aware that abstract class can have fields and method implementations too. Try to avoid fields and method implementations duplications.

With your setup you can just add field to PC class and this field won't be accessible for instances of another classes.

If you don't want to have this field, just delete it and you can move the rest of the fields to the abstract class. To do this, you must also override the toString method:

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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM