Skip to content

Instantly share code, notes, and snippets.

@OfTheDelmer
Last active December 27, 2015 12:09
Show Gist options
  • Save OfTheDelmer/7323674 to your computer and use it in GitHub Desktop.
Save OfTheDelmer/7323674 to your computer and use it in GitHub Desktop.

Animal Kingdom

A Classic Model For Inheritance

Objective
Utilize class inheritance in coffee-script for Animals

Outline

  • Using the class inheritance you've seen in class create an Animal Class. Keep count

    For example, Animal might have something like

     class Animal
     	constructor: (name, environment) ->
     		@name = name 
     		@environment = environment
    

    There is some descretion here, and you can feel free to try out default method arguments like we have in ruby, i.e.

     class Animal
     	constructor: (name, environment = "somewhere on earth") ->
     		@name = name 
     		@environment = environment
     		this.count ||= 0
     		this.count += 1
    

    Assume some default, creativity encouraged, functionality of your Animals, and give them a few methods. Try creating a private variable that keeps count of the number of animals.. maybe something like below

     class Animal
     	@_count: 0 # private variable
     	
     	constructor: (name, environment = "somewhere on earth") ->
     		@name = name 
     		@environment = environment
     		Animal._count++
    
     	@get_count: ->
     		 @_count
    

    but feel free to mix it up.

  • Using the Animal class define two different subclass classes each having a class that inherits from it. See below.

              Animal  -> Mammal -> Dolphin
    

    and

              Animal -> Fish -> Shark 
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment