Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
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"
End
Class
8
10
11
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
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