« Return to Thread: constructors with mix of params and Map

constructors with mix of params and Map

by Alex Tkachman :: Rate this Message:

Reply to Author | View in Thread

Do we want to code like this where constructor params mixed with named
properties?

class Book {
  Book (String author, String coauthor) {
      this.author = author
      this.coauthor = coauthor
  }

  String title, author, coauthor
}

class SuperBook extends Book {
  SuperBook (Map map, String author, String coauthor) {
      super (author, coauthor)
      title = "SuperBook"
  }
}

class NewInstanceTest extends GroovyTestCase{
    void testNewInstance () {
       def book = Book.newInstance("Alex", title: "All about it", "Graeme" )
       assertEquals book.title, "All about it"
       assertEquals book.author, "Alex"
       assertEquals book.coauthor, "Graeme"
    }

    void testConstructor () {
       def book = new Book("Alex", title: "All about it", "Graeme" )
       assertEquals book.title, "All about it"
       assertEquals book.author, "Alex"
       assertEquals book.coauthor, "Graeme"
    }

    void testSuper () {
       def book = new SuperBook("Alex", title: "All about it", "Graeme" )
       assertEquals book.title, "SuperBook"
       assertEquals book.author, "Alex"
       assertEquals book.coauthor, "Graeme"
    }
}

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

 « Return to Thread: constructors with mix of params and Map