Showing posts with label classes. Show all posts
Showing posts with label classes. Show all posts

11 Sept 2012

Interfaces, nested classes and inheritance

In one of my previous posts I said that interfaces in CoffeeScript can be written as usual classes and extended by other classes (if only you want to write Java-style interface, of course). I was wrong.
There are two main problems with Java-style interfaces in CoffeeScript:
  1. Every class in Java can have only one parent but implement as many interfaces as you like. CoffeeScript classes can extend only one class. That's it. If CoffeeScript allowed multiple inheritance everything would be as easy as I said before. However, it doesn't.
  2. Every class in Java is instance of all the interfaces it implements. In CoffeeScript instanceof operator return true only for classes in objects' prototype chain. No exclusions, no possibility to override.
So, I've tried to solve both of this problems.

5 Sept 2012

Nested classes

As I've already told in previous post, I need to use nested classes. The main problem is that nested classes in CoffeeScript behave not like nested classes in Java. In Java any nested class can access fields and methods of the outer class instance in that the nested class instance was created. It is widely used, e.g. for iterators. In CoffeeScript you can define a nested class and create its' instance from the outer one but this nested class can't access outer's fields and methods.
So, you need somehow tell the nested class instance who created it. I've found two ways of solution.
The first one is C# way. You pass outer class instance reference to nested class constructor. This works fine. I do not like this way because you need to modify every constructor in every nested class (CoffeeScript allows you to determine only one constructor for each class, but you can use static methods as constructor; I will tell about this later).

Anonymous classes

I must apologise for the long silence. Every time I make a plan of what I want to tell about in some blog, I miss lots of things I could tell but didn't because they are not planned, and do not tell about planned things because do not have an appropriate mood for that. Anyway, I've decided to post here what I want to in every moment, though it will be a mess.
I had to work much with classes in CoffeeScript for last few days. The main problem of my work is that I convert Java code to CoffeeScript. Thus I need to have all the Java-like constructions in my disposal: anonymous classes, interfaces, nested classes, enums...
To be honest, interfaces are absolutely useless in CoffeeScript (as you can never declare a typed variable) but in Java they sometimes contain constants that you may need in other parts of code. So, making interface in CoffeeScript is pretty simple - you just declare the class with all the necessary fields (and empty method - if you wish to make your code more readable and self-documented).
The interesting thing I found out several days ago is that CoffeeScript allows making anonymous classes quite easily, though I failed to find this option in its' official docs (http://coffeescript.org/).

22 Mar 2012

Classes: introduction

First of all, I want to say several words about classes in CoffeeScript. In fact, any class here is just a map, where keys are field names and values are anything you want - numbers, strings, functions... Here is very simple example:
class Animal
  name: undefined
  @numberOfRegisteredAnimals: 0
  
  constructor: (@name) ->
    Animal.numberOfRegisteredAnimals += 1

  @printNumberOfRegistedAnimals: () ->
    console.log "The number of registered animals is equal to", 
            Animal.numberOfRegisteredAnimals 
  
  sayName: () ->
    console.log "My name is " + @name + ", " + @voice() 

class Cat extends Animal
  voice: () ->
    return "meow!!!"

class Dog extends Animal
  voice: () ->
    return "woof!!!"

Animal.printNumberOfRegistedAnimals()
cat = new Cat "Kuzya"
cat.sayName()
Animal.printNumberOfRegistedAnimals()
dog = new Dog "Sharik"
dog.sayName()
Animal.printNumberOfRegistedAnimals()