Visual Basic An Object Oriented Approach 3 Making
Visual Basic: An Object Oriented Approach 3 – Making Objects Work
Object Structure n An Object n n has an internal structure that defines its state or identity has an interface through which other objects can work with it
Internal Structure and Operations n The state of an object is the set of values of its attributes n n these are stored in variables, whose value can change over time they can be altered or manipulated by operations, known as program statements
Operators n Operators work on values and variables to perform n n n n simple arithmetic (+, -, *, / …) string (text) manipulation date manipulation other forms of calculation assignment (=) comparison (=, >, <, >=, <=, Like) Values, variables and operators can be put together (using a form of grammar) to form expressions
Expressions n An expression n n is a combination of values, variables and operators has a value (found by calculating the expression) has a type of value (e. g. number, string, date…) can be a direct replacement for a value of the appropriate type x+2 “Joe” & “ Bloggs” Date + 7 (1 week from today) z * (x + 2) (=z * 3 if x is 1)
Conditions n n Conditions are expressions that evaluate to True or False Use conditions to control what happens next in a program n n select statements to execute repeat certain statements until a condition is True If age >= 18 Then buy beer Else buy soft drink End If Do sit driving test Loop Until Result=“Pass”
Abstraction n To create a computer program, we must create an abstraction n n Remove all unnecessary details Retain all info necessary to the calculations
Object Orientation n n In Object-Oriented programs, Objects are abstractions of real-world things We often refer to this a Modelling n n An object is a model of something we represent in a program The object Interface is how we manipulate it
Statements n A statement is the fundamental syntactical element of a program n n smallest piece of code that the language will accept in its own right A statement can be used to n n n set aside space for data storage (variables) assign a value to a variable perform a calculation and assign its result to a variable execute a previously-defined operation control other statements
Example statements Dim x As Integer, y As Single x=2 x=x+4 If x > 3 Then y = Sqr(x) Print y End If Declares variables Assigns a value to the variable Assigns the result of an expression Controls other statements Executes a predefined operation
Statements in Objects n Operations in objects are sequences of statements n n In general n n Can change the state of the object by changing the value of one or more variables Can retrieve data from the object Could do both A Sub is a sequence of statements that does something to the state of an object A Function is a sequence of statements that retrieves a value based on the values of the object’s member variables A Property is a set of statements that reflect an attribute of the object, allowing it to be read or changed Sometimes, these distinctions can become blurred
Inter-object communication n n One object (the (ATM) (Account) Deposit Client) makes use of (100. 00) another object (the Server) by sending it a message Account. Deposit 100. 00 The message is a statement within one of the Client’s methods This statement is an operation in the ATM (operations). object
- Slides: 12