120 likes | 207 Views
Visual Basic: An Object Oriented Approach. 3 – Making Objects Work. Object Structure. An Object 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.
E N D
Visual Basic: An Object Oriented Approach 3 – Making Objects Work
Object Structure • An Object • 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 • The state of an object is the set of values of its attributes • these are stored in variables, whose value can change over time • they can be altered or manipulated by operations, known as program statements
Operators • Operators work on values and variables to perform • 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 • An expression • 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 • Conditions are expressions that evaluate to True or False • Use conditions to control what happens next in a program • 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 • To create a computer program, we must create an abstraction • Remove all unnecessary details • Retain all info necessary to the calculations
Object Orientation • In Object-Oriented programs, Objects are abstractions of real-world things • We often refer to this a Modelling • An object is a model of something we represent in a program • The object Interface is how we manipulate it
Statements • A statement is the fundamental syntactical element of a program • smallest piece of code that the language will accept in its own right • A statement can be used to • 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 Declares variables Dim x As Integer, y As Single x = 2 x = x + 4 If x > 3 Then y = Sqr(x) Print y End If Assigns a value to the variable Assigns the result of an expression Controls other statements Executes a pre-defined operation
Statements in Objects • Operations in objects are sequences of statements • 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 • In general • 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 • One object (the Client) makes use of another object (the Server) by sending it a message • The message is a statement within one of the Client’s methods (operations). (ATM) (Account) Deposit (100.00) Account.Deposit 100.00 This statement is an operation in the ATM object