Encapsulating functionality in object classes
Object-oriented programming (OOP) is a programming paradigm common to most software development. At its core is the object
class. Objects allow us to encapsulate data and functionality, which can then be stored and passed around.
In this recipe, we will build some class objects, to break down their components, and understand how they are defined and used.
Getting ready
In this recipe, we can use the playground from the previous recipe. Don’t worry if you didn’t work through the previous recipe, as this one will contain all the code you need.
How to do it…
Let’s write some code to create and use class objects, and then we will walk through what the code is doing:
- First, let’s create a
Person
class object:class Person {
}
- Within the curly brackets,
{ }
, add three constants representing the person’s name, and one variable representing their country of residence:let givenName...