Created
July 16, 2017 01:02
-
-
Save edgarsanchez/3bb607e6d8b2ffd64dd9006179fdb285 to your computer and use it in GitHub Desktop.
Some comparisons between Java O-O and F# O-O
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[<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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Color = Unknown | Red | White | Black | Silver | |
// A record: a simple immutable properties-only object | |
type Position = { longitude : float; latitude : double } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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