Escolar Documentos
Profissional Documentos
Cultura Documentos
Objectives
2
Class
3
Instance fields
class Stock
{
instance fields string name;
double price;
int shares;
...
}
4
Object creation
ibm
name
price
shares
objects sun
name
price
shares
5
Member access
6
Method
7
Method invocation
ibm.SetPrice(99.0);
ibm.Sell(50);
8
Method implementation
9
Required this
• Must use this when local name conflicts with member name
class Stock
{
string name;
double price;
int shares;
parameter and field
have same name
void Buy(int shares)
{
must use this this.shares += shares;
}
...
}
10
Omitting this
class Stock
{
string name;
double price;
int shares;
void Buy(int s)
{
omit this shares += s;
}
...
}
11
Member declaration order
class Stock
{
void Buy(int s)
{
use shares += s;
}
string name;
double price;
declare int shares;
...
}
12
Return value
ibm.SetPrice(56.0);
ibm.Buy(100);
double v = ibm.Value();
13
Method overloading
class Stock
{
void Buy(int shares)
{
this.shares += shares;
overload Stock ibm = new Stock();
}
14
Value parameter
int y = 9;
y unchanged F(y);
15
Ref parameter
ref parameter,
void H(ref int x)
initially 9
{
x = x * 2;
}
int y = 9;
16
Out parameter
int y;
17
Member default access
class Stock
{
string name;
equivalent private double price;
private int shares;
18
Meaning of private
class Test
{
static void Main()
{
Stock ibm = new Stock();
...
error to access in other class ibm.shares = 9;
}
}
19
Public member access
class Stock
{
private string name;
private double price;
private int shares;
20
Encapsulation
21
Naming
• Naming guidelines
– class: intercaps with initial capital
– method: intercaps with initial capital
– method parameter: intercaps with initial lower case
– public field: intercaps with initial capital
– private field: intercaps with initial lower case
22
Summary
23