Last active
December 16, 2015 17:59
-
-
Save blouerat/5474178 to your computer and use it in GitHub Desktop.
OOP example
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
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!"); | |
} | |
} |
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
I have a pair of dark blue pants! | |
Now my pants are vermilion, and it's much better! |
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
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