View
428
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Programming with classes and objects in vb.net
Citation preview
Custom ClassesCustom ClassesCIS-166
ObjectsObjectsVB allows the creation of new object
types by creating a class.◦ Classes may have properties, methods,
and events and describe what’s possible.◦ Objects are particular things such as an
Add button, and describe what is.Button is a classexitButton is an instance of the class.A class type may be used to create
many objects (instances).
Cookie AnalogyCookie AnalogyClass = Cookie cutter (what cookie will
look like)Instantiate = Make a cookie using cookie
cutter (use the NEW keyword)Instance = Newly made cookie (object)Properties of different Instances may
have different values.◦ One cookie may have icing, another not◦ Each cookie can have a different flavor
Methods = Eat, Bake, or CrumbleEvents = Cookie crumbling
ReusabilityReusabilityReusability is a major advantage of
OOP over traditional programmingNew classes created can be used in
multiple projects.Each object created from the class can
have its own property values.
Object-Oriented Object-Oriented Terminology Terminology Encapsulation: Object is complete by
itself Inheritance: One class can serve as
the starting point for a second classPolymorphism: One word/name may
be used in different places, and operates correctly for its context
EncapsulationEncapsulationCombination of characteristics of an
object along with its behavior in "one package"
Cannot make object do anything it does not already "know" how to do.
Cannot make up new properties, methods, or events for an object.
Sometimes referred to as data hiding, an object can expose only those data elements and procedures that it wishes.
InheritanceInheritanceAbility to create a new class from an
existing classOriginal class is called Base Class,
Superclass, or Parent Class.Inherited class is called Subclass,
Derived Class, or Child Class.Example: each form created is
inherited from the Form class and customized to current needs
Purpose of inheritance is reusability.
Inheritance ExampleInheritance ExampleCan create a class that describes a
person◦Firstname, Lastname, Birthday properties
Can create a derived class that starts from person class and adds properties, methods of a student◦Major, Student ID
Common phrasing: “IS A”◦“A student IS A person” denotes student
as derived class from person
PolymorphismPolymorphismMethods having identical names, but
different implementationsRadio button, check boxes, and list
boxes all have a Select method—the Select method operates appropriately for its class.
Messagebox has multiple ways to Show, depending on arguments
Polymorphism in Polymorphism in PracticePracticeOverloading — Several procedures
have the same name with different argument lists◦ Argument list creates a signature
Overriding — Refers to a method that has the same name as its base class◦ Method in subclass takes precedence,
replaces the method from the parent class.
Instance VariablesInstance VariablesInstance variables or properties store
data specific to one objectSeparate memory locations for the
variables and procedures for each instance (copy) of the object
Shared Variables, Shared Variables, PropertiesPropertiesSingle memory location that is
available for ALL objects of a classCan be accessed without instantiating
an object of the classUse the Shared keyword to create
Constructors & Constructors & DestructorsDestructorsConstructor: Method that
automatically executes when an object is instantiated◦ Create by writing a Public Sub New
procedureDestructor: Method that
automatically executes when an object is destroyed◦ Create by writing a Finalize procedure