super()如何知道输入的参数是什么?

时间:2014-07-15 17:06:26

标签: java parent children super

我目前正在查看此处的示例代码:http://www.java2s.com/Code/Java/Chart/JFreeChartStackedBarChartDemo1.htm

package org.jfree.chart.demo;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a stacked bar chart
 * using data from a {@link CategoryDataset}.
 *
 */
public class StackedBarChartDemo1 extends ApplicationFrame {

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */
    public StackedBarChartDemo1(final String title) {

        super(title);
        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

    }

    /**
     * Creates a sample dataset.
     * 
     * @return a sample dataset.
     */
    private CategoryDataset createDataset() {
        return DemoDatasetFactory.createCategoryDataset();
    }

    /**
     * Creates a sample chart.
     * 
     * @param dataset  the dataset for the chart.
     * 
     * @return a sample chart.
     */
    private JFreeChart createChart(final CategoryDataset dataset) {

        final JFreeChart chart = ChartFactory.createStackedBarChart(
            "Stacked Bar Chart Demo 1",  // chart title
            "Category",                  // domain axis label
            "Value",                     // range axis label
            dataset,                     // data
            PlotOrientation.VERTICAL,    // the plot orientation
            true,                        // legend
            true,                        // tooltips
            false                        // urls
        );
        return chart;

    }

    public static void main(final String[] args) {

        final StackedBarChartDemo1 demo = new StackedBarChartDemo1("Stacked Bar Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}

本部分

public StackedBarChartDemo1(final String title) {

        super(title);
        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

    }

我对超级(标题)有点困惑;确实。我知道它使用String参数“tittle”调用父构造函数,但我希望有人可以解释它对参数的作用。我看了很多教程,他们已经帮了解,但我对他的回答对我来说有点先进。我理解从父类调用方法时如何使用super()但我不明白为什么在此代码中需要它。我的猜测是它被传递给ApplicationFrame类,放在方法setTitle()中,但我不确定它是如何工作的。

1 个答案:

答案 0 :(得分:1)

调用super(title)将使用title参数运行ApplicationFrame中的构造函数。查看ApplicationFrame,看看它在做什么。

此外,只要您了解文档中发生的情况,它们实现代码的方式并不重要。