Title: Outline
1Outline
- Review - Properties
- Properties allow to read and write class fields
- Implement a Property.
- Read-Only Property
- Write-Only Property.
- Define and use overloading methods
- Use Class variables and class methods
- i.e. static variables and methods
- Define and use arrays (text chapter 7)
2Properties
- class Point
-
- private double x
- private double y
- public Point(double x, double y)
-
- this.x x
- this.y y
-
- public double getY()
-
- return y
-
- public setY(double y)
-
- this.y y
-
class Point private double x private double
y public Point(double x, double y) this.x
x this.y y public double Y get return
y set y value
getter
property
setter
get accessor is used in read context
set accessor is used in write context
3Properties
- Point p1 new Point(1.0, 2.0)
- Point p2 new Point(-1.0, -2.0)
- p1.Y p2.Y
Read context, so get accessor of property is used
Write context, so set accessor of property is
used
4Exercise
The code below uses property Y of class Point.
Where is get accessor of the property is used
and where is set accessor is used?
- Console.WriteLine(The Y coordinate is p1.Y)
- p1.Y double.Parse(Console.ReadLine())
- if ( p2.Y lt p1.Y)
- Console.WriteLine(point p2 is below p1)
- double v -p2.Y
5Read-only and write-only properties
- Find out errors in the following code fragment
- class Student
-
- private int id
- private string passwd
- private bool registered
- public Student (int the_id)
-
- id the_id
- registered false
- passwd
-
- public string PassWord
-
- set passwd value
-
- public int ID
Student s1 new Student(000) Student s1 new
Student(111) s1.PassWord hello,world s2.Pas
sWord s1. Password s1.ID 222 int id
s2.ID Console.WriteLine(s1 state
s1.registered)
Write-only property
Read-only property
Read/write property
6Define and use overloading methods
- We can use method overloading to define more than
one method with the same name. - Overloaded methods must have different parameter
lists - Example 5.1
- Overloaded constructors - BankAccount.cs
- Exercise write a method overload the Deposit
method in BankAccount class. - This method takes no parameter. And itll deposit
100. - Test it using example 5.2 TestBankAccount.cs
7Class variables and class methods
- A class, we can define
- data fields
- constructor
- methods
- Non-static fields and methods must be used with
the object prefix. - Such fields or methods are called instance
variables or instance methods - Usage
- First declare and create an object of the class
- ObjectName.field_name
- ObjectName.method_name
- Static fields and methods must be used with the
class prefix. Because they belong to the class,
NOT objects. - Such fields or methods are called class variables
or class methods - Usage
- ClassName.Static_field_name
- ClassName.Static_method_name
8Class variables and class methods
- class Square
-
- private double length, width
- //Define a static variable recording the number
of resizing operations - private static int resize_times 0
- public Square (double l, double w)
-
- length l
- width w
-
- public Resize(double l1, double w1)
-
- length l
- width w1
- resize_times
-
class variable
Write a program which - creates two square
objects - resizes them several times - outputs
the total number of resizing operations.
class method
9Difference of class variables(static) and
instance variables(non-static)
- class Square
-
- private double length, width
- private static int resize_times 0
- ..
Square s1 new Square(1.0,2.0) Square s1 new
Square(3.0,4.0)
resize_times
length width
length width
object referenced by s2
object referenced by s1
s1
s2
reference s1
reference s2
10Arrays
- Define arrays
- int x 9,8,7 //initialize array elements in
definition - int y new int10 //define
- Array length
- int size1 x.Length
- int size2 y.Length
- Use array indices to access array elements
- index range from 0 to (array.Length-1)
- x0 x1 x2 0.0