当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to call parent class objects within a subclass?

问题描述:

I'm not sure if I'm asking this right, as I'm attempting to teach myself Java. I have a class which contains my main method, and within this class are several subclasses that need access to my user settings using java.util.Properties. I have to create the properties object in every subclass in order to make it work, and I can't reference the object using configFilePath, it must be null. I'm wondering if I can create this public object within the parent class, so I don't need to create it in all of its subclasses? Here is my code, I'm really not sure I'm doing this right although it works.

public class Frame1 extends JFrame {

Settings config = new Settings(); //this is the object I want to reference within subclasses

class Update extends SwingWorker<Integer, Void> { //first subclass

@Override

protected Integer doInBackground() throws Exception {

Settings config = new Settings(configFilePath); //yet I have to create the object within every subclass, this time an argument is required.

String templateDir = config.getProperty("templatedir");

String writePath = config.getProperty("outputdir");

//do some logic code, not required for my question

}

@Override

protected void done() {

Update2 update2 = new Update2();

update2.execute(); //start the next subclass which also needs access to Settings(configFilePath)

}

}

}

public class Settings extends JFrame {

String configFilePath = "C:/path/to/settings.properties";

Properties properties = new Properties();

public Settings(String configFilePath) throws IOException {

this.configFilePath = configFilePath;

FileInputStream fis = null;

try {

fis = new FileInputStream(configFilePath);

properties.load(fis);

} catch (FileNotFoundException e) {

setDefaults();

} finally {

if (fis != null) {

fis.close();

}

}

}

}

I'm not sure if I'm doing this right or not, it seems to work but seems to be rather redundant having to create the config object every time I need to access my user settings. I hope this hasn't been asked before, and if it has please link me, as I could not find it.

网友答案:

You can create the Setting class as a Singleton pattern, here is one example:

public class Settings extends JFrame{
    String configFilePath = "C:/path/to/settings.properties";
    Properties properties = new Properties();

    private static Settings instance;

    public static Settings getInstance(){
       if(instance==null){
           instance = new Setting();
       }
       return instance;
    }

    private Settings() throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(configFilePath);
            properties.load(fis);

        } catch (FileNotFoundException e) {
            setDefaults();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }
}

Usage in any other class of your system:

Settings.getInstance().getProperty("...");
网友答案:

From Update you can use Frame1.this to access the this of Frame1 (because Update is an inner class of Frame1).

Then to access config you can use Frame1.this.config.

Here is a working example:

public class PrefixerFactory {
    private String prefix; // Used by Prefixer

    public PrefixerFactory(String prefix) {
        this.prefix = prefix;
    }

    public Prefixer createPrefixer() {
        return new Prefixer();
    }

    public class Prefixer { // Inner class
        public String addPrefix(String value) {
            // Using "prefix" from PrefixerFactory
            return PrefixerFactory.this.prefix + value;
        }
    }

    public static void main(String[] args) {
        Prefixer helloPrefixer = new PrefixerFactory("Hello ").createPrefixer();
        Prefixer goodbyePrefixer = new PrefixerFactory("Good bye ").createPrefixer();

        System.out.println(helloPrefixer.addPrefix("world")); // Hello world
        System.out.println(goodbyePrefixer.addPrefix("world")); // Good bye world
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: