Creating Uruk Hais
From Huben's Wiki
(Created page with "Saruman did not create single monsters to attack the lands of men. Each '''class''' is a race composed of many individuals ('''instances''' or '''objects'''.) The most horrible...") |
|||
Line 48: | Line 48: | ||
# Which instance variable of each object is set directly? | # Which instance variable of each object is set directly? | ||
# If you inserted 'one.sharedBattleCry = "Meep!";' before the declamations, what would change in the declamations? Why? | # If you inserted 'one.sharedBattleCry = "Meep!";' before the declamations, what would change in the declamations? Why? | ||
+ | |||
+ | == Static And Regular Methods == | ||
+ | |||
+ | The '''static''' reserved word distinguishes class variables from instance variables. It also has an important meaning for methods. A static (class) method may not use any instance variables, though it can use class variables. A static method may be called by its class or by one of the instances of the class with the dot notation. But a non-static, regular method cannot be called by its class because the class has no instance variables and the method might use them. | ||
+ | |||
+ | # Paste the line 'UrukHai.screamTheSharedBattleCry();' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. | ||
+ | # Paste the line 'one.screamTheSharedBattleCry();' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. | ||
+ | # Paste the line 'UrukHai.declaim();' at the end of main(). Look at UrukHai, and predict if it will work. Explain why. | ||
+ | |||
+ | == It Is Forbidden! == | ||
+ | |||
+ | At last the secret meanings of the keywords '''public''' and '''private''' may be revealed to you. | ||
+ | * Public means that a variable or method of a class may be used by any other class. | ||
+ | * Private means that a variable or method of a class may be used only within the same class. | ||
+ | This is the secret of '''encapsulation''', the principle of hiding the inner workings of a class. The public parts of a class are called the '''interface''', the private parts are concealed. |