|
1 | 1 | <?xml version="1.0"?> |
2 | 2 |
|
3 | 3 | <!-- Generated by Docutils 0.19 --> |
4 | | -<chapter xml:id="lets-look-at-a-java-program"> |
5 | | - <title>Lets look at a Java Program</title> |
| 4 | +<chapter xml:id="java-programs"> |
| 5 | + <title>Java Programs</title> |
| 6 | + |
| 7 | +<section xml:id="sec-classes-and-objects"> |
| 8 | + <title>Classes and Objects</title> |
| 9 | + |
| 10 | + <p> |
| 11 | + <idx>Object-Oriented Programming (OOP)</idx> |
| 12 | + Depending on how deep your knowledge of Python and programming in general is, you may or may not be familiar with classes and objects. These two important <term>Object-Oriented Programming (OOP)</term> concepts will briefly be discussed. If you already have a good understanding of classes and objects in Python, this section may be skipped. |
| 13 | + </p> |
| 14 | + |
| 15 | + <p> |
| 16 | + <idx>object</idx> |
| 17 | + <idx>attribute</idx> |
| 18 | + <idx>instance variable</idx> |
| 19 | + <idx>method</idx> |
| 20 | + <term>Objects</term> in the context of programming are instances of classes. Objects contain <term>attributes</term> (also referred to as <term>instance variables</term>), which are data that describe the object or are associated with the object, and <term>methods</term>, which are special functions used by the object. Methods are typically actions the object can perform, or can be used to make changes to the object's attributes. |
| 21 | + </p> |
| 22 | + |
| 23 | + <p> |
| 24 | + <idx>class</idx> |
| 25 | + <idx>constructor</idx> |
| 26 | + <term>Classes</term> can be thought of as being similar to blueprints or a recipe; they hold details of how to create an instance of an object. Classes contain a special method called a <term>constructor</term> that is used to create an instance of an object. Once the object is created, it will use the class definition to define its attributes and call methods. |
| 27 | + </p> |
| 28 | + |
| 29 | + <p> |
| 30 | + The best way to understand classes and objects is to see them in action. Let's define a <c>Dog</c> class in Python: |
| 31 | + </p> |
| 32 | + |
| 33 | + <program language="python" xml:id="python-class-example"> |
| 34 | + <code> |
| 35 | + class Dog: |
| 36 | + def __init__(self, name, breed, fur_color): |
| 37 | + self.name = name |
| 38 | + self.breed = breed |
| 39 | + self.fur_color = fur_color |
| 40 | + self.trained = False |
| 41 | + print("Dog named " + self.name + " created!") |
| 42 | + |
| 43 | + def bark(self): |
| 44 | + print(self.name + " says woof!") |
| 45 | + |
| 46 | + def sit(self): |
| 47 | + if self.trained: |
| 48 | + print(self.name + " sits.") |
| 49 | + else: |
| 50 | + print(self.name + " has not been trained.") |
| 51 | + |
| 52 | + def train(self): |
| 53 | + self.trained = True |
| 54 | + </code> |
| 55 | + </program> |
| 56 | + |
| 57 | + <p> |
| 58 | + Let's unpack what is going on in this code. The first line is where we declare the class definition and name it <c>Dog</c>. Next, we have a special method called <c>__init__</c>. This <c>__init__</c> method is the constructor and is required for every Python class definition. Within the <c>__init__</c> method, attributes are defined. As you can see, the attributes <c>name</c>, <c>breed</c>, and <c>fur_color</c> must be defined when creating a Dog object using this class definition, but the <c>trained</c> attribute is defined within the constructor and is initialized as <c>False</c>. We can also have the <c>__init__</c> method run any code, such as the print statement informing us that a Dog object was created. |
| 59 | + </p> |
| 60 | + |
| 61 | + <p> |
| 62 | + The next three blocks of code are the class's methods. These include <c>bark(self)</c>, <c>sit(self)</c>, and <c>train(self)</c>. As you can see, the class defines attributes (the variables in the <c>__init__</c> method) and methods for instances of the Dog class. |
| 63 | + </p> |
| 64 | + |
| 65 | + <p> |
| 66 | + <idx><c>self</c></idx> |
| 67 | + Within each method, and for each attribute, you will notice the use of <term><c>self</c></term>. This is required in Python. <c>self</c> simply indicates that an attribute or method is being used for a specific instance of an object created with a class. |
| 68 | + </p> |
| 69 | + |
| 70 | + <p> |
| 71 | + Next, we will use this class to create a new <c>Dog</c> object. We will call this new Dog object <c>my_dog</c>: |
| 72 | + </p> |
| 73 | + |
| 74 | + <program interactive="activecode" language="python" xml:id="python-class-create-object"> |
| 75 | + <code> |
| 76 | + class Dog: |
| 77 | + def __init__(self, name, breed, fur_color): |
| 78 | + self.name = name |
| 79 | + self.breed = breed |
| 80 | + self.fur_color = fur_color |
| 81 | + self.trained = False |
| 82 | + print("Dog named " + self.name + " created!") |
| 83 | + |
| 84 | + def bark(self): |
| 85 | + print(self.name + " says woof!") |
| 86 | + |
| 87 | + def sit(self): |
| 88 | + if self.trained: |
| 89 | + print(self.name + " sits.") |
| 90 | + else: |
| 91 | + print(self.name + " has not been trained.") |
| 92 | + |
| 93 | + def train(self): |
| 94 | + self.trained = True |
| 95 | + |
| 96 | + |
| 97 | + my_dog = Dog("Rex", "pug", "brown") |
| 98 | + </code> |
| 99 | + </program> |
| 100 | + |
| 101 | + <p> |
| 102 | + In the final line of code, we have created an object called <c>my_dog</c>. We have initialized its attributes, setting <c>name</c> to Rex, <c>breed</c> to pug, and <c>fur_color</c> to brown. |
| 103 | + </p> |
| 104 | + |
| 105 | + <p> |
| 106 | + Now that we have created a <c>Dog</c> object using the class we defined, we can utilize the class's methods: |
| 107 | + </p> |
| 108 | + |
| 109 | + <program interactive="activecode" language="python" xml:id="python-class-full"> |
| 110 | + <code> |
| 111 | + class Dog: |
| 112 | + def __init__(self, name, breed, fur_color): |
| 113 | + self.name = name |
| 114 | + self.breed = breed |
| 115 | + self.fur_color = fur_color |
| 116 | + self.trained = False |
| 117 | + print("Dog named " + self.name + " created!") |
| 118 | + |
| 119 | + def bark(self): |
| 120 | + print(self.name + " says woof!") |
| 121 | + |
| 122 | + def sit(self): |
| 123 | + if self.trained: |
| 124 | + print(self.name + " sits.") |
| 125 | + else: |
| 126 | + print(self.name + " has not been trained.") |
| 127 | + |
| 128 | + def train(self): |
| 129 | + self.trained = True |
| 130 | + |
| 131 | + |
| 132 | + my_dog = Dog("Rex", "pug", "brown") |
| 133 | + my_dog.bark() |
| 134 | + my_dog.sit() |
| 135 | + </code> |
| 136 | + </program> |
| 137 | + |
| 138 | + <note> |
| 139 | + <p> |
| 140 | + When running the code above, the line <c>Rex has not ben trained.</c> will appear in the output when calling the <c>sit()</c> method. Try adding a one or more lines of code so that <c>Rex sits.</c> appears in the output! |
| 141 | + </p> |
| 142 | + </note> |
| 143 | + |
| 144 | + <p> |
| 145 | + Now, we have a full class definition and have utilized its methods. Class definitions in Java will be covered thoroughly in chapter 6. For now, it is important to know that Python programs can be written without using classes at all. Java, on the other hand, requires all code to reside in a class. This will be discussed in the next section. |
| 146 | + </p> |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +</section> |
6 | 151 | <section xml:id="sec-lets-look-at-a-java-program"> |
7 | 152 | <title>Lets look at a Java Program</title> |
8 | 153 | <p> |
|
0 commit comments