Created
May 17, 2014 22:05
-
-
Save rymate1234/700b3c21ba98869b023a to your computer and use it in GitHub Desktop.
This file contains 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 Human | |
{ | |
private String name = "unnamed"; // the name of this human | |
private Human friend = null; // the human's friend | |
/** | |
* This "creates" a new Human | |
*/ | |
public Human(String name, Human friend) { | |
this.name = name; | |
this.friend = friend; | |
} | |
public Human(String name) { | |
this.name = name; | |
this.friend = null; | |
} | |
public Human() { | |
this.name = "unnamed"; | |
this.friend = null; | |
} | |
public void sayName() { | |
System.out.println("My name is " + this.name); | |
} | |
public void sayGoodnight() { | |
if (friend == null) | |
System.out.println("Good night nobody."); | |
else | |
System.out.println("Good night " + friend.name); | |
} | |
} |
This file contains 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 Main | |
{ | |
public static void main(String[] args) { | |
//create a new human object named stephen | |
Human stephen = new Human("Stephen"); | |
//create a human object named joe with stephen as a friend | |
Human joe = new Human("Joe", stephen); | |
stephen.sayName(); //shows 'My name is Stephen' | |
stephen.sayGoodnight(); //shows 'Good night nobody.' | |
joe.sayName(); // shows 'My name is Joe' | |
joe.sayGoodnight(); //shows 'Good night Stephen' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment