class B {
private String name;
private final List<A> list;
public B() {
list = new ArrayList<A>();
}
public void addA(A a) {
list.add(a);
}
public List<A> getListOfA {
return this.list;
}
}
// использование
B b = new B();
b.addA(new A(1, "First"));
b.addA(new A(2, "Second"));
for (A a : b.getListOfA()) {
System.out.println(a.getName());
} // Telescoping constructor pattern - does not scale well!
public class NutritionFacts {
private final int servingSize; // (mL) required
private final int servings; // (per container) required
private final int calories; // optional
private final int fat; // (g) optional
private final int sodium; // (mg) optional
private final int carbohydrate; // (g) optional
public NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories) {
this(servingSize, servings, calories, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat, int sodium) {
this(servingSize, servings, calories, fat, sodium, 0);
}
public NutritionFacts(int servingSize, int servings,
int calories, int fat, int sodium, int carbohydrate) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = sodium;
this.carbohydrate = carbohydrate;
}
}// Builder Pattern
public class NutritionFacts {
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public static class Builder {
// Required parameters
private final int servingSize;
private final int servings;
// Optional parameters - initialized to default values
private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;
public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}
public Builder calories(int val)
{ calories = val; return this; }
public Builder fat(int val)
{ fat = val; return this; }
public Builder carbohydrate(int val)
{ carbohydrate = val; return this; }
public Builder sodium(int val)
{ sodium = val; return this; }
public NutritionFacts build() {
return new NutritionFacts(this);
}
}
private NutritionFacts(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
}NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build(); public class OOP {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new GridBagLayout());
JTextField textf = new JTextField();
JTextField textf2 = new JTextField();
JButton Mybutton = new JButton("Start");
frame.add(textf, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.9,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2), 0, 0));
frame.add(textf2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.9,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2), 0, 0));
frame.add(Mybutton, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.9,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
new Insets(1, 2, 2, 2), 0, 0));
Mybutton.addActionListener(new ButtonActionListener()); // Ошибка
frame.setVisible(true);
frame.pack();
}
}
class ButtonActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Messege box");
}
} import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Party extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
} public static void deleteServer(int index) throws IOException {
BufferedReader reader = null;
PrintWriter writer = null;
try {
File file = new File(getFilePath());
String fileToWrite = "fileToWrite.txt";
reader = new BufferedReader(new FileReader(file));
writer = new PrintWriter(new FileWriter(fileToWrite));
int current = 0;
String line;
while ((line = reader.readLine()) != null) {
if (current != index) {
writer.println(line);
}
current++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
}
} Class.forName("org.postgresql.Driver"); если выдает class not found exception значит jarник не подключился.<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>VERSION</version>
</dependency><dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>VERSION</version>
</dependency><scope>provided</scope>если у вас Tomcat и jarник драйвера положить в папку lib Tomcata