diff --git a/source/ch2_firstjavaprogram.ptx b/source/ch2_firstjavaprogram.ptx index 9b62f7d..9f0e546 100644 --- a/source/ch2_firstjavaprogram.ptx +++ b/source/ch2_firstjavaprogram.ptx @@ -30,32 +30,39 @@ The best way to understand classes and objects is to see them in action. Let's define a Dog class in Python:

- - + + + class Dog: + """ A simple Dog class definition. """ def __init__(self, name, breed, fur_color): + # constructor method to create a Dog object self.name = name self.breed = breed self.fur_color = fur_color - self.trained = False + self.trained = False # dogs are not trained by default print("Dog named " + self.name + " created!") def bark(self): + # method to make the dog bark print(self.name + " says woof!") def sit(self): - if self.trained: + # method to make the dog sit + if self.trained: # check if the dog has been trained otherwise it will not sit print(self.name + " sits.") else: print(self.name + " has not been trained.") def train(self): + # method to train the dog, which will set the trained attribute to True self.trained = True - - + + +

- Let's unpack what is going on in this code. The first line is where we declare the class definition and name it Dog. Next, we have a special method called __init__. This __init__ method is the constructor and is required for every Python class definition. Within the __init__ method, attributes are defined. As you can see, the attributes name, breed, and fur_color must be defined when creating a Dog object using this class definition, but the trained attribute is defined within the constructor and is initialized as False. We can also have the __init__ method run any code, such as the print statement informing us that a Dog object was created. + Let's unpack what is going on in . The first line is where we declare the class definition and name it Dog. Next, we have a special method called __init__. This __init__ method is the constructor and is required for every Python class definition. Within the __init__ method, attributes are defined. As you can see, the attributes name, breed, and fur_color must be defined when creating a Dog object using this class definition, but the trained attribute is defined within the constructor and is initialized as False. We can also have the __init__ method run any code, such as the print statement informing us that a Dog object was created.

@@ -70,74 +77,86 @@

Next, we will use this class to create a new Dog object. We will call this new Dog object my_dog:

- - + + class Dog: + """ A simple Dog class definition. """ def __init__(self, name, breed, fur_color): + # constructor method to create a Dog object self.name = name self.breed = breed self.fur_color = fur_color - self.trained = False + self.trained = False # dogs are not trained by default print("Dog named " + self.name + " created!") def bark(self): + # method to make the dog bark print(self.name + " says woof!") def sit(self): - if self.trained: + # method to make the dog sit + if self.trained: # check if the dog has been trained otherwise it will not sit print(self.name + " sits.") else: print(self.name + " has not been trained.") def train(self): + # method to train the dog, which will set the trained attribute to True self.trained = True - + # Create a Dog object called my_dog my_dog = Dog("Rex", "pug", "brown") - +

- In the final line of code, we have created an object called my_dog. We have initialized its attributes, setting name to Rex, breed to pug, and fur_color to brown. + In the final line of code in , we have created an object called my_dog. We have initialized its attributes, setting name to Rex, breed to pug, and fur_color to brown.

Now that we have created a Dog object using the class we defined, we can utilize the class's methods:

- + + class Dog: + """ A simple Dog class definition. """ def __init__(self, name, breed, fur_color): + # constructor method to create a Dog object self.name = name self.breed = breed self.fur_color = fur_color - self.trained = False + self.trained = False # dogs are not trained by default print("Dog named " + self.name + " created!") def bark(self): + # method to make the dog bark print(self.name + " says woof!") def sit(self): + # method to make the dog sit if self.trained: print(self.name + " sits.") else: print(self.name + " has not been trained.") def train(self): + # method to train the dog, which will set the trained attribute to True self.trained = True my_dog = Dog("Rex", "pug", "brown") - my_dog.bark() - my_dog.sit() + my_dog.bark() # call the bark method + my_dog.sit() # call the sit method +

- When running the code above, the line Rex has not ben trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output! + When running , the line Rex has not been trained. will appear in the output when calling the sit() method. Try adding a one or more lines of code so that Rex sits. appears in the output!