"Now it's high time that I tell you about constructors. This is really simple concept. Programmers have invented a shorthand way to create and initialize objects."
Without a constructor | With a constructor |
---|---|
|
|
|
|
|
|
"I just finished learning about the initialize method…"
"Look harder. With constructors, the code is more convenient and compact."
"So it is. Here's a question. I know how to write an initialize method inside a class, but how do I write a constructor?"
"First, look at this example:"
Without a constructor | With a constructor |
---|---|
|
|
"It's easy to declare a constructor inside a class. A constructor is similar to the initialize method, with just two differences:
1. The name of a constructor is the same as the class name (instead of initialize).
2. A constructor has no type (no type is indicated)."
"OK, so it's like initialize, but with a few differences. I think I get it."
GO TO FULL VERSION