1 Working with Objects. 2 Defining a Class in VB.NET A class is a user-defined data type You can declare it as part of a module, but usually in a separate.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Inheritance Chapter 14. What is inheritance?  The ability to create a class from another class, the “parent” class, extending the functionality and state.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Module 7: Object-Oriented Programming in Visual Basic .NET
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
JavaScript, Fourth Edition
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object-Oriented Programming: Classes and Objects.
Introduction to Object-oriented Programming
Classes (Part 1) Lecture 3
Object-Oriented Programming: Classes and Objects
Haidong Xue Summer 2011, at GSU
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
METHODS AND BEHAVIORS AKEEL AHMED.
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Interfaces.
Object-Oriented Programming: Classes and Objects
Tonga Institute of Higher Education
JAVA CLASSES.
Classes & Objects A deeper Look Chapter 10 & 11
Chapter 7 Objects and Classes
Chengyu Sun California State University, Los Angeles
Presentation transcript:

1 Working with Objects

2 Defining a Class in VB.NET A class is a user-defined data type You can declare it as part of a module, but usually in a separate file (also.vb) Ingredients –Data members (fields) –Constructors –Properties –Methods Access Modifiers –Public: accessible by all –Private: accessible by the class itself only –Protected: accessible by the class itself or its sub-classes –Friend: accessible by all in the current assembly

3 ClassExample1 (Horse.vb) Public Class Horse Public m_name As String‘ data members (fields) Public m_color As String Friend m_height As Single = 1.5 ' in meters Public Sub New()‘ default constructors MyClass.New("", "", 0)‘ MyClass (i.e. this in Java) End Sub Public Sub New(ByVal name As String, ByVal color As String) MyClass.New(name, color, 0) End Sub Private Sub New(ByVal name As String, ByVal color As String, ByVal height As Single) m_name = name m_color = color If 1 <= height And height <= 2.5 Then m_height = height End If End Sub End Class

4 ClassExample1 (Module1.vb) Module Module1 Sub Main() Dim h As Horse = New Horse("Lightning", "Brown") Console.Out.WriteLine("Name is {0}", h.m_name) Console.Out.WriteLine("Color is {0}", h.m_color) Console.Out.WriteLine("Height is {0}", h.m_height) End Sub End Module

5 Properties If data members are public, they can be altered directly (horse.m_name), violates Data Hiding principle (Encapsulation) If private, need accessor methods (set_ & get_), so Properties are introduced Fields and properties are similar, but properties are accessed through its accessor methods Get and Set Normally use private or protected fields to hold the data

6 Defining Properties Public Class Horse Public m_name As String Public m_color As String Private m_height As Single = 1.5 Public Property height() As Single Get‘ Get block Return m_height End Get Set‘ Set block, note the keyword Value If 1 <= Value And Value <= 2.5 Then m_height = Value End If End Set End Property Public Sub New(ByVal name As String, ByVal color As String, ByVal height As Single) m_name = name m_color = color MyClass.height = height‘ MyClass.height Is a Property!! End Sub End Class

7 Using Properties Module Module1 Sub Main() Dim h As Horse = New Horse("Lightning", "Brown“, 2) h.height = 1.9 Console.Out.WriteLine("Name is {0}", h.m_name) Console.Out.WriteLine("Color is {0}", h.m_color) Console.Out.WriteLine("Height is {0}", h.height) End Sub End Module

8 ReadOnly Properties With Get block only Does not allow changes Public Class Person Private m_name As String Private m_dob As Date Private m_balances() As Decimal Public ReadOnly Property name() As String Get Return m_name End Get End Property Public ReadOnly Property dob() As Date Get Return m_dob End Get End Property … End Class

9 WriteOnly Properties With Set block only, rarely used Public Class OutTextStream Public WriteOnly Property outText() As String Set (ByVal Value As String) Console.Out.WriteLine(Value) End Set End Property End Class Module Module1 Sub Main() Dim o As OutTextStream = New OutTextStream() o.outText = “This goes to the console!” End Sub End Module

10 Parameterized Properties Property “Array”, can be multi-dimensional too Public Class Person Private m_name As String Private m_dob As Date Private m_balances() As Decimal … Public Property balances(ByVal index As Integer) As Decimal Get Return m_balances(index) End Get Set m_balances(index) = Value End Set End Property Public ReadOnly Property noOfBalances() As Integer Get Return m_balances.Length End Get End Property Public Sub New(ByVal name As String, ByVal dob As Date) m_name = name m_dob = dob m_balances = New Decimal(5) {} End Sub End Class ‘ Using it Dim p As Person = New person("John G.", #7/4/1960#) p.balances(0) = 2100 p.balances(1) = 10000

11 ClassExample2 Lab 7

12 Methods (1) Behavior, operations that an object can do A function or subroutine defined inside the class body that can be called on an object Can have modifiers Public, Private, Protected, or Friend Public Class Horse Private m_name As String Private m_color As String Private m_height As Single … ' We assume that a horse can jump 75% of its height Public Function Jump() As Single Return CSng(0.75 * m_height) End Function End Class

13 Methods (2) ‘ Calling Method Sub Main() Dim h As Horse = New Horse("Bucefalus", "black", 2) Console.Out.WriteLine("The horse (0) jumps {1} meters!", _ h.name, h.Jump()) End Sub Example ClassExample3

14 Method Overloading (1) To achieve the same task but with different arguments E.g. a Add Function that can be applied to Short, Integer, and Long Must have different argument signatures Public Class Calculator Public Overloads Function Add(ByVal x As Short, ByVal y As Short) As Long Return x + y End Function Public Overloads Function Add(ByVal x As Integer, ByVal y As Integer) As Long Return x + y End Function Public Overloads Function Add(ByVal x As Long, ByVal y As Long) As Long Return x + y End Function End Class

15 Method Overloading (2) Module Module1 Sub Main() Dim result As Long Dim calc As New Calculator() Dim s1 As Short = 3, s2 As Short = 5 result = calc.Add(s1, s2)‘ using Add(Short, Short) Dim i1 As Integer = 3, i2 As Integer = 5 result = calc.Add(i1, i2) ‘ using Add(Integer, Integer) Dim j1 As Long = 3, j2 As Long = 5 result = calc.Add(j1, j2) ‘ using Add(Long, Long) End Sub End Module Example ClassExample4

16 ClassExample5 Account id: String balance: Decimal New(id:String, balance:Decimal) Person name: String accounts: Account() balance: Decimal New(name:String) Deposit(ac:String, amt:Decimal) Withdraw(ac:String, amt:Decimal): Boolean Transfer(acFrom:String, acTo:String, amt:Decimal) GetAccountById(ac:String): Account

17 Shared Class Members Similar concepts of “static” in other OO languages Shared data member Shared Methods

18 Shared Data Members (1) Can be fields or properties Belong to the class, shared by all instances Also known as class variable All instances get the same value Shared property CANNOT access non-shared (instance) fields or properties Accessed through class name, e.g. Product.taxRate Save memory Define constants (no instantiation of class need)

19 Shared Data Members (2) Public Class Product Private m_name As String ' product name (instance variable) Private m_listPrice As Decimal ' the list price (instance variable) Private Shared s_taxRate As Double‘ shared variable Public Shared Property taxRate() As Double‘ shared property Get Return s_taxRate End Get Set s_taxRate = Value End Set End Property Public Sub New(ByVal name As String, ByVal listPrice As Decimal) m_name = name m_listPrice = listPrice End Sub... End Class

20 Shared Methods (1) Actions performed on class level, not object level Do not require the existence of an object (no instantiation need) E.g. some math functions CANNOT access non-shared (instance) members Accessed through class name, e.g. Product.GetUnknownPrice(10)

21 Shared Methods (2) Public Class Product Private m_name As String ' product name Private m_listPrice As Decimal ' the list price Private Shared s_taxRate As Double... Public Sub New(ByVal name As String, ByVal listPrice As Decimal) m_name = name m_listPrice = listPrice End Sub Public Function GetPrice(ByVal discount As Double) As Decimal Dim price As Double price = m_listPrice + m_listPrice * s_taxRate - m_listPrice * discount Return CDec(price) End Function Public Shared Function GetPriceUnknown(ByVal price As Decimal) As Decimal Return CDec(price + price * s_taxRate) End Function End Class

22 ClassExample6