Skip to content

Instantly share code, notes, and snippets.

@edgarsanchez
Created July 16, 2017 01:02
Show Gist options
  • Save edgarsanchez/3bb607e6d8b2ffd64dd9006179fdb285 to your computer and use it in GitHub Desktop.
Save edgarsanchez/3bb607e6d8b2ffd64dd9006179fdb285 to your computer and use it in GitHub Desktop.
Some comparisons between Java O-O and F# O-O
[<AbstractClass>]
type Vehicle() =
member val Color = Unknown with get, set
member val Position = {longitude = 0.0; latitude = 0.0} with get, set
member val Speed = 0.0 with get, set
member this.Accelerate increment =
// your logic goes here
this.Speed <- this.Speed + increment
this.Speed
public abstract class Vehicle {
private Color color;
private Position position;
private double speed;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public Vehicle()
{
this.setColor(Color.UNKNOWN);
this.setPosition(new Position(0, 0));
}
public double Accelerate(double increment) {
// your logic here
this.setSpeed(this.getSpeed() + increment);
return this.getSpeed();
}
}
public enum Color { UNKNOWN, RED, WHITE, BLACK, SILVER }
public final class Position {
public final double longitude;
public final double latitude;
public Position(double longitude, double latitude) {
this.longitude = longitude;
this.latitude = latitude;
}
}
type Color = Unknown | Red | White | Black | Silver
// A record: a simple immutable properties-only object
type Position = { longitude : float; latitude : double }
type Car(brand, color) =
inherit Vehicle()
do
base.Color <- color
member val Brand : string = brand with get
member this.PullHandBrake() =
// your logic goes here
this.Accelerate -this.Speed
public class Car extends Vehicle {
private final String brand;
public String getBrand() {
return brand;
}
public Car(String brand, Color color) {
super();
this.brand = brand;
this.setColor(color);
}
public double PullHandBrake() {
// your logic goes here
return this.Accelerate(-this.getSpeed());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment