Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AutoErsteller {
- public static void main(String[] args) {
- Auto fiat500 = new Auto("Fiat500");
- fiat500.fahren();
- fiat500.fahren();
- fiat500.fahren();
- fiat500.tankAuffuellen();
- fiat500.tankAuffuellen();
- fiat500.getBoardComputer().logBoardComputer();
- }
- }
- public class Auto {
- private String carType;
- public String getCarType() {
- return carType;
- }
- public void setCarType(String carType) {
- this.carType = carType;
- }
- private BoardComputer _BoardComputer = new BoardComputer();
- public BoardComputer getBoardComputer(){
- return _BoardComputer;
- }
- public void fahren(){
- _BoardComputer.setKiloMeterStand(_BoardComputer.getKiloMeterStand() + 10);
- }
- public void tankAuffuellen(){
- _BoardComputer.setTankFuellung(_BoardComputer.getTankFuellung() + 10);
- }
- public Auto(String nType){
- carType = nType;
- }
- }
- import java.lang.reflect.*;
- public class BoardComputer {
- private Double kiloMeterStand = 0.0;
- public Double getKiloMeterStand() {
- return kiloMeterStand;
- }
- public void setKiloMeterStand(Double kiloMeterStand) {
- this.kiloMeterStand = kiloMeterStand;
- }
- private Double tankFuellung = 0.0;
- public Double getTankFuellung() {
- return tankFuellung;
- }
- public void setTankFuellung(Double tankFuellung) {
- this.tankFuellung = tankFuellung;
- }
- public void logBoardComputer(){
- Field[] fields = this.getClass().getDeclaredFields();
- for(int i = 0; i< fields.length;i++){
- try{
- System.out.println("[BoardComputer]: " + fields[i].getName() + " = " + String.valueOf(fields[i].get(this)));
- }
- catch(Exception e){
- System.out.println("[BoardComputer]: Could not get the value of." + fields[i].getName() + ". Exception thrown: " + e.toString());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement