Вы находитесь на странице: 1из 2

Chapter 9 Homework Solutions:

Chapter 9 - OOP Creating Object Oriented Programs ANSWERS TO REVIEW QUESTIONS 1. What is an object? property? method? An object is a thing such as a command button. Visual Basic allows you to create your own new object types by creating a class module. Properties are characteristics, and methods are actions that can be performed by a class of object. 2. What is the purpose of a class module? When a new class is created with a class module, you can use that class in multiple projects. A big advantage of object-oriented programming over traditional programming is the ability to reuse objects. Each object that you create from the class has its own set of properties. This works just like the built-in VB controls. 3. Why should properties of a class be declared as private? The rules of encapsulation require that each object be in charge of its own data. Encapsulation refers to the combination of characteristics of an object along with its behaviors. You have one "package" that holds the definition of all properties, methods, and events. Encapsulation is also called data hiding. To accomplish encapsulation, all variables in a class module are declared as Private. As a private variable the value is available only to the procedures within the class module, the same way that module-level variables are available only to procedures within the form module. 4. What are property procedures and what is their purpose? The way that your class allows its properties to be set is through a Property Let procedure. To retrieve the value of a property from a class you must use a Property Get procedure. The Property Let and Property Get procedures serve as a gateway to assign and retrieve property values to the private members of the class module. 5. Explain how to create a new object. After a new class is created it can be used to create a new object. You must create an instance of the class using the New keyword and then specify the class. This step is referred to as instantiating an object. The New keyword creates a new instance of an object class. The object class can be a class that you create or a standard Visual Basic object such as a form or a control. 6. What steps are needed to assign property values to an object? When your program creates objects from your class, you will need to assign values to the properties. The way that your class allows its properties to be set is through a Property Let procedure. To retrieve the value of a property from a class you must use a Property Get procedure. The Property Let and Property Get procedures serve as a gateway to assign and retrieve property values to the private members of the class module. 7. What actions trigger the Initialize event and the Terminate event of an object? The Class_Initialize event is triggered when an object is created and the Class_Terminate event occurs when an object goes out of scope or is terminated with a Set ObjectName =

Nothing. These event procedures are useful for doing any setup work or for making sure that the memory allocated for an object is released. 8. How can you write methods for a new class? You can create methods by adding sub procedures and functions for the behaviors that the class needs. These methods belong with the collection. 9. What is a collection? Name one collection that is automatically built into VB. A collection class holds references for a series of objects created from the same class or from different classes. Although in concept the collection contains the objects, actually the collection holds a reference to each of the objects, called members of the collection. There are many collections built into VB. Each project has a Forms collection; each form has a Controls collection. 10. What properties and methods are provided by the Collection object?

A collection object has an Add method, a Remove method, an Item method, and a Count property.

Вам также может понравиться