Java 學習記錄49 — Inner classes

import java.util.ArrayList;public class GearBox {    private ArrayList<Gear> gears;
private int maxGears;
private int currentGear = 0;
public GearBox(int maxGears) {
this.maxGears = maxGears;
this.gears = new ArrayList<>();
Gear neutral = new Gear(0, 0.0);
this.gears.add(neutral);
}
public class Gear {
private int gearNumber;
private double ratio;
public Gear(int gearNumber, double ratio) {
this.gearNumber = gearNumber;
this.ratio = ratio;
}
public double driveSpeed(int revs) {
return revs * this.ratio;
}
}
}
public class MainGearBox {
public static void main(String[] args) {
GearBox mcLaren = new GearBox(6);
GearBox.Gear first = mcLaren.new Gear(1, 12.3);
// GearBox.Gear second = new GearBox.Gear(2,2.34);
// GearBox.Gear third = new mcLaren.Gear(3, 3.45);
System.out.println(first.driveSpeed(1000));
}
}
import java.util.ArrayList;public class GearBox {    private ArrayList<Gear> gears;
private int maxGears;
private int currentGear = 0;
private boolean clutchIsIn;
public GearBox(int maxGears) {
this.maxGears = maxGears;
this.gears = new ArrayList<>();
Gear neutral = new Gear(0, 0.0);
this.gears.add(neutral);
}
public void operateClutch(boolean in) {
this.clutchIsIn = in;
}
public void addGear(int number, double ratio) {
if ((number > 0) && (number <= this.maxGears)) {
this.gears.add(new Gear(number, ratio));
}
}
public void changeGear(int newGear) {
if ((newGear >= 0) && (newGear < this.gears.size()) && this.clutchIsIn) {
this.currentGear = newGear;
System.out.println("Gear " + newGear + " selected.");
} else {
System.out.println("Grind ! (gee gee gee sound)");
this.currentGear = 0;
}
}
public double wheelSpeed(int revs){
if(clutchIsIn){
System.out.println("Scream!!");
return 0.0;
}
return revs * this.gears.get(this.currentGear).getRatio();
}
private class Gear {
private int gearNumber;
private double ratio;
public Gear(int gearNumber, double ratio) {
this.gearNumber = gearNumber;
this.ratio = ratio;
}
public double driveSpeed(int revs) {
return revs * this.ratio;
}
public double getRatio() {
return ratio;
}
}
}
GearBox mcLaren = new GearBox(6);
GearBox.Gear first = mcLaren.new Gear(1, 12.3);
public class MainGearBox {
public static void main(String[] args) {
GearBox mcLaren = new GearBox(6);
mcLaren.addGear(1,5.5);
mcLaren.addGear(2,10.10);
mcLaren.addGear(3,15.15);
mcLaren.operateClutch(true);
mcLaren.changeGear(1);
mcLaren.operateClutch(false);
System.out.println(mcLaren.wheelSpeed(1000));
mcLaren.changeGear(2);
System.out.println(mcLaren.wheelSpeed(3000));
mcLaren.operateClutch(true);
mcLaren.changeGear(3);
mcLaren.operateClutch(false);
System.out.println(mcLaren.wheelSpeed(6000));
}
}
public GearBox(int maxGears) {
this.maxGears = maxGears;
this.gears = new ArrayList<>();
Gear neutral = new Gear(0, 0.0);
this.gears.add(neutral);
for (int i = 0; i < maxGears; i++) {
addGear(i, i * 5.3);
}
}
public class MainGearBox {
public static void main(String[] args) {
GearBox mcLaren = new GearBox(6);
mcLaren.operateClutch(true);
mcLaren.changeGear(1);
mcLaren.operateClutch(false);
System.out.println(mcLaren.wheelSpeed(1000));
mcLaren.changeGear(2);
System.out.println(mcLaren.wheelSpeed(3000));
mcLaren.operateClutch(true);
mcLaren.changeGear(3);
mcLaren.operateClutch(false);
System.out.println(mcLaren.wheelSpeed(6000));
}
}
import javax.swing.text.View;public class Button {
private String title;
private OnClickListener onClickListener;
public Button(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public void onClick() {
this.onClickListener.onClick(this.title);
}
public interface OnClickListener {
public void onClick(String title);
}
}
import java.util.Scanner;public class MainButton {
private static Scanner scanner = new Scanner(System.in);
private static Button btn = new Button("Print");
public static void main(String[] args) {
class ClickListener implements Button.OnClickListener {
public ClickListener() {
System.out.println("I've been attached.");
}
@Override
public void onClick(String title) {
System.out.println(title + " was clicked.");
}
}
btn.setOnClickListener(new ClickListener());
listen();
}
private static void listen() {
boolean quit = false;
while (!quit) {
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 0:
quit = true;
break;
case 1:
btn.onClick();
break;
}
}
}
}
public static void main(String[] args) {
// class ClickListener implements Button.OnClickListener {
//
// public ClickListener() {
// System.out.println("I've been attached.");
// }
//
// @Override
// public void onClick(String title) {
// System.out.println(title + " was clicked.");
// }
// }
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(String title) {
System.out.println(title + " was clicked.");
}
});
listen();
}

How to Understand the Button Code Tutorial for Beginners

Joseph J

public class Button {
//attributes
private String title;
private IOnClickListener onClickListenerVariable;
//constructor
public Button(String title) {
this.title = title;
}

//getter
public String getTitle() {
return title;
}

//setter
public void setOnClickListenerVariable(IOnClickListener onClickListenerVariable){
this.onClickListenerVariable = onClickListenerVariable;
}
public void onClick(){
this.onClickListenerVariable.onClick(this.title);
}
public interface IOnClickListener {
public void onClick(String title);
}
}//close Button class
import java.util.Scanner;

public class Main {
private static Scanner scanner = new Scanner(System.in);
private static Button btnPrint = new Button("Print");
public static void main(String[] args) {

class ClickListener implements Button.IOnClickListener {
public ClickListener(){
System.out.println("New instance of ClickListener created");
}

@Override
public void onClick(String title) {
System.out.println(title + " was clicked");
}
}
btnPrint.setOnClickListenerVariable(new ClickListener());
btnPrint.onClick();
listen();
}

private static void listen(){
boolean quit = false;
while(!quit){
int choice = scanner.nextInt();
scanner.nextLine();
switch(choice){
case 0:
quit = true;
break;
case 1:
btnPrint.onClick();
}
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
張小雄

張小雄

記錄成為軟體工程師的過程