从main方法调用扩展另一个类的类

时间:2016-03-10 14:47:35

标签: java inheritance bar-chart method-overriding

所以我有一个绘制条形图的课程。我从这里得到了代码:

https://docs.oracle.com/javafx/2/charts/bar-chart.htm

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class BarChartSample extends Application {
final static String austria = "Austria";
final static String brazil = "Brazil";
final static String france = "France";
final static String italy = "Italy";
final static String usa = "USA";

@Override public void start(Stage stage) {
    stage.setTitle("Bar Chart Sample");
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    final BarChart<String,Number> bc = 
        new BarChart<String,Number>(xAxis,yAxis);
    bc.setTitle("Country Summary");
    xAxis.setLabel("Country");       
    yAxis.setLabel("Value");

    XYChart.Series series1 = new XYChart.Series();
    series1.setName("2003");       
    series1.getData().add(new XYChart.Data(austria, 25601.34));
    series1.getData().add(new XYChart.Data(brazil, 20148.82));
    series1.getData().add(new XYChart.Data(france, 10000));
    series1.getData().add(new XYChart.Data(italy, 35407.15));
    series1.getData().add(new XYChart.Data(usa, 12000));      

    XYChart.Series series2 = new XYChart.Series();
    series2.setName("2004");
    series2.getData().add(new XYChart.Data(austria, 57401.85));
    series2.getData().add(new XYChart.Data(brazil, 41941.19));
    series2.getData().add(new XYChart.Data(france, 45263.37));
    series2.getData().add(new XYChart.Data(italy, 117320.16));
    series2.getData().add(new XYChart.Data(usa, 14845.27));  

    XYChart.Series series3 = new XYChart.Series();
    series3.setName("2005");
    series3.getData().add(new XYChart.Data(austria, 45000.65));
    series3.getData().add(new XYChart.Data(brazil, 44835.76));
    series3.getData().add(new XYChart.Data(france, 18722.18));
    series3.getData().add(new XYChart.Data(italy, 17557.31));
    series3.getData().add(new XYChart.Data(usa, 92633.68));  

    Scene scene  = new Scene(bc,800,600);
    bc.getData().addAll(series1, series2, series3);
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

但问题是我想从另一个类的main方法中调用这个BarChartSample类。 例如,我有一个名为BookRecommender的类,并且有一个main方法。我希望能够设置title,xlabel,ylabel等参数以及此BarChartSample类的值。但我不能从我的Recommender类中运行BarChartSample类的start方法。

2 个答案:

答案 0 :(得分:3)

这是一个JavaFX应用程序,因此您不会直接从另一个类调用此类。您需要在类中封装此类的功能,然后从JavaFX应用程序中调用它。

E.g。您可以创建特定的BarChartScene子类,然后在构造函数或方法setData([您希望传递的数据])中传递所需的数据。或者您可以创建一个返回Scene对象的BarChartSceneFactory类。

E.g。 SceneFactory.java

        [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical toQueue:[NSOperationQueue currentQueue]
                                                   withHandler: ^(CMDeviceMotion *motion, NSError *error){
                                                       [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
                                                   }];
    }
}

- (void)handleDeviceMotion:(CMDeviceMotion*)motion {
   CMAttitude *attitude = motion.attitude;
   double yaw = attitude.yaw * 180 / M_PI;
   double pitch = attitude.pitch * 180 / M_PI;
   double roll = attitude.roll * 180 / M_PI;
   self.xLabel.text = [NSString stringWithFormat:@"%7.4f", yaw];
   self.yLabel.text = [NSString stringWithFormat:@"%7.4f", pitch];
   self.zLabel.text = [NSString stringWithFormat:@"%7.4f", roll];
   [self sendDeviceAttitudeLogWithYaw:yaw pitch:pitch roll:roll];
}

MainApp.java

import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;

public abstract class SceneFactory {
    private static final String AUSTRIA = "Austria";
    private static final String BRAZIL = "Brazil";
    private static final String FRANCE = "France";
    private static final String ITALY = "Italy";
    private static final String USA = "USA";

    public static Scene getBarChartScene() {
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String,Number> bc = new BarChart<String,Number>(xAxis,yAxis);
        bc.setTitle("Country Summary");
        xAxis.setLabel("Country");     
        yAxis.setLabel("Value");

        XYChart.Series series1 = new XYChart.Series();
        series1.setName("2003");       
        series1.getData().add(new XYChart.Data(AUSTRIA, 25601.34));
        series1.getData().add(new XYChart.Data(BRAZIL, 20148.82));
        series1.getData().add(new XYChart.Data(FRANCE, 10000));
        series1.getData().add(new XYChart.Data(ITALY, 35407.15));
        series1.getData().add(new XYChart.Data(USA, 12000));      

        XYChart.Series series2 = new XYChart.Series();
        series2.setName("2004");
        series2.getData().add(new XYChart.Data(AUSTRIA, 57401.85));
        series2.getData().add(new XYChart.Data(BRAZIL, 41941.19));
        series2.getData().add(new XYChart.Data(FRANCE, 45263.37));
        series2.getData().add(new XYChart.Data(ITALY, 117320.16));
        series2.getData().add(new XYChart.Data(USA, 14845.27));  

        XYChart.Series series3 = new XYChart.Series();
        series3.setName("2005");
        series3.getData().add(new XYChart.Data(AUSTRIA, 45000.65));
        series3.getData().add(new XYChart.Data(BRAZIL, 44835.76));
        series3.getData().add(new XYChart.Data(FRANCE, 18722.18));
        series3.getData().add(new XYChart.Data(ITALY, 17557.31));
        series3.getData().add(new XYChart.Data(USA, 92633.68));  

        Scene scene  = new Scene(bc,800,600);
        bc.getData().addAll(series1, series2, series3);
        return scene;
    }
}

然后,您可以在主示例应用程序中简单地调用SceneFactory.getBarChartScene()以获取Scene对象并将其设置在舞台上。

如果您希望显示自己的自定义数据,请将其作为参数传递给getScene方法,例如import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; public class MainApp extends Application { @Override public void start(Stage stage) { Scene scene = SceneFactory.getBarChartScene(); stage.setScene(scene); stage.setTitle("Bar Chart Sample"); stage.show(); } } 第一个整数包含年份,映射到国家/地区名称到值的映射。

或者你可以将Scene子类化,并且这样做也是如此。

Map<Integer, Map<String, Double>> chartData

答案 1 :(得分:1)

您的主类是否也扩展了Application?如果是这样,我会修改您拥有的代码,以便它不会扩展应用程序。您可以添加方法来修改所需的任何数据。您甚至可以将所需的代码复制并粘贴到应用程序中。