Skip to content

Instantly share code, notes, and snippets.

@rymate1234
Created May 17, 2014 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rymate1234/700b3c21ba98869b023a to your computer and use it in GitHub Desktop.
Save rymate1234/700b3c21ba98869b023a to your computer and use it in GitHub Desktop.
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);
}
}
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