Skip to content

Instantly share code, notes, and snippets.

@blouerat
Last active December 16, 2015 17:59
Show Gist options
  • Save blouerat/5474178 to your computer and use it in GitHub Desktop.
Save blouerat/5474178 to your computer and use it in GitHub Desktop.
OOP example
package oop;
public class OOP {
public static void main(String[] args) {
Pants pants = new Pants("dark blue");
System.out.println("I have a pair of " + pants.getColour() + " pants!");
pants.setColour("vermilion");
System.out.println("Now my pants are " + pants.getColour() + ", and it's much better!");
}
}
I have a pair of dark blue pants!
Now my pants are vermilion, and it's much better!
package oop;
// A class modelizing pants
public class Pants {
// It has a colour
private String colour;
// initialColour being the colour we want when we create a new instance
public Pants(String initialColour) {
// i could also write colour = initialColour;
this.colour = initialColour;
}
public String getColour() {
return this.colour;
}
public void setColour(String colour) {
this.colour = colour;
}
@Override
public String toString() {
return "Pants{" + "colour=" + colour + '}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment