Você está na página 1de 22

Lesson 1

Class and Object

DAN Vournpheng
Computer Science Dept., Royal University of Phnom Penh

Objectives
In this lesson:
Object Members
Instance and Static Members
Fields
Methods
Properties
System.Object
2

Object Members
Objects should be self-contained with a
single purpose in mind.
All included members should be
compatible and interact effectively to
support that purpose.
Members accessibilities can be defined
as private, protected, or public.

Object Members (Contd)


Class skeleton is
Class Website
'constructors
'destructor
'fields
'methods
'properties
'events
'nested objects
End Class

Instance and Static Members


Members are classified in instance
members & static members.
Class members belong to instance, unless
modified as shared.
There is only a single copy of that static
members (with the keyword Shared) can
exist at any given time, regardless of how
many object instances there are.
5

Instance and Static Members (Contd)


An object exists as a sole entity, with its
own attributes and behaviors.
An object has a set of data separated from
others.

Class SavingAccount
Private CurrentBalance As Double
Public Shared CurrentInterestRate As Double = 0.05

'...

End Class
6

Fields
Fields comprise the primary data
portion of a class.
They define the objects state.
Fields can be initialized during declaration
or afterward, depending on style or the
nature of requirements

Fields (Contd)
Syntax
Class Website
Private URL As String = "http://www.google.com"

Private Description As String


Public Sub New()
Description = "Google Website"
End Sub
'...

End Class
8

Fields: Constant Fields


A constant field is known ahead of time,
its value wont change.
It is guaranteed not to change at runtime.
Its value is initialized during compilation.
It can be read as many times as needed.
Constants are defined by the keyword
Const, and by definition also static
members.
9

Fields: Constant Fields (Contd)


Syntax
Class Website
Private Const http As String = "http://"
'...
End Class

10

Fields: Read-Only Fields


Read only field is an instance member
(with the keyword ReadOnly).
Its value is initialized during runtime.
It cannot be modified after initialization
(during object instantiation).
It is initialized via declaration or
constructor.

11

Fields: Read-Only Fields (Contd)


Syntax
Class Fly
Private ReadOnly Dept As Date = Date.Now
Private ReadOnly Arrv As Date
Public Sub New()
Arrv = Date.Now.AddDays(3)
End Sub
'...

End Class
12

Methods
Methods: Instance methods and static
methods.
Instance method is the objects behavior.
Static method is the classs behavior.
Instance/class Method is the primary
mechanism whereby messages may be
passed between the object/class.

13

Methods (Contd)
Syntax
Class Website
Private Const http As String = "http://"
Public Sub Update()
'...
End Sub
Public Function IsLive(d As Date) As Boolean
'...
End Sub
'...

End Class

14

Methods: Constructors
A constructor is a sub method named
New()
A class might have several constructor
A class object is created with the
expression New ClassName()

15

Methods: Constructors (Contd)


Class Website
Public Sub New()
'...
End Sub
Public Sub New(url As String, ...)
'...
End Sub
'...

End Class
'Usage
Dim web As New Website()
'...
web = New Website("http://www.google.com", ...)
16

Properties
A property (readonly, writeonly, both) enables to
access to an objects state.
It is used like a field, but it operates much like
method.

17

Properties (Contd)
Class Website
'...
Public WriteOnly Property Owner() As Object
Set(ByVal value As Object) ... End Set
End Property
Public ReadOnly Property LiveDate() As Date
Get ... End Get
End Property
Public Property URL() As String
Get ... End Get
Set(ByVal value As String) ... End Set
End Property
'...

End Class

18

System.Object
System.Object is defined as the root of the
type hierarchy.
All other classes within the .NET
Framwork derive from System.Object.
Custom classes automatically inherit from
Object.
It provides the infrastructure for all
derived classes.
19

System.Object (Contd)
Here are some methods exposed by
System.Object.
Equals() Compare two objects fro equality.
ReferenceEquals() Returns true if both the
specified Object instances refer to the same
instance
GetType() Retrieves the qualified data type fro the
specified object.
MemberwiseClone() Creates a shallow copy of
the current Object instance.
ToString() Provides a string representation of the
Object.
20

21

Next topic
Designing Object-Oriented Programs
Inheritance
Encapsulate Object Internals
Polymorphisms

22

Você também pode gostar