Sie sind auf Seite 1von 9

HaNoi University of Technology

Week 3: Object-based programing C# Mastering C# 2008

Week 3: Object-based Programing C#


Topics
Object & Class Members of Class Indexers

Concept review in Exercise 4


Object: - Objects are allocated on the heap (classes are reference types) - Objects must be created with new Visibility Modifiers (excerpt) public visible where the declaring namespace is known - Members of interfaces and enumerations are public by default. - Types in a namespace (classes, structs, interfaces, enums,delegates) - have default visibility internal (visible in the declaring assembly) private only visible in declaring class or struct - Members of classes and structs are private by default (fields, methods, properties, ..., nested types) Static Fields and Constants - Belong to a class, not to an object - Constants must not be declared static Method Overloading Methods of a class may have the same name - if they have different numbers of parameters, or - if they have different parameter types, or - if they have different parameter kinds (value, ref/out) Constructors - Constructors can be overloaded. - A constructor may call another constructor with this(specified in the constructor head, not in its body as in Java!). - Before a constructor is called, fields are possibly initialized. Properties - Allow read-only and write-only fields. - Can validate a field when it is accessed. - Interface and implementation of data can differ. - Substitute for fields in interfaces. Indexers - get or set method can be omitted (write-only / read-only) - Indexers can be overloaded with different index type - .NET library has indexers for string (s[i]), ArrayList (a[i]), etc.

1/9

HaNoi University of Technology

Week 3: Object-based programing C# Mastering C# 2008

Part I: Struct and Class


Exercise 1
The purpose of the first four exercises is to get used to the C# compiler and to get experience with properties, operator overloading and user-defined conversions. A Time value stores a time of day such as 10:05 or 00:45 as the number of minutes since midnight (that is, 605 and 45 in these examples). A struct type Time can be declared as follows: public struct Time { private readonly int minutes; public Time(int hh, int mm) { this.minutes = 60 * hh + mm; } public override String ToString() { return minutes.ToString(); } } Create a VS2008 C# project called TestTime. Modify the Main method to declare variables of type Time,assign values of type Time to them, and print the Time value using Console.WriteLine. Compile and run your program. The next few exercises use this type!

Exercise 2
In struct type Time, declare the following conversions: - an implicit conversion from int (minutes since midnight) to Time - an explicit conversion from Time to int (minutes since midnight) Use these facilities in your Main method. For instance, you should be able to do this: Time t1 = new Time(9,30); Time t2 = 120; // Two hours int m1 = (int)t1; Console.WriteLine("t1={0} and t2={1} and m1={2}", t1, t2, m1); Time t3 = t1 + 45; The next few exercises use this typeWhy is the addition in the initialization of t3 legal? What is the value of t3?

2/9

HaNoi University of Technology

Week 3: Object-based programing C# Mastering C# 2008

Exercise 3
Make the minutes field of struct type Time public (and not readonly) instead of private readonly. Then execute this code: Time t1 = new Time(9,30); Time t2 = t1; t1.minutes = 100; Console.WriteLine("t1={0} and t2={1}", t1, t2); What result do you get? Why? What result do you get if you change Time to be a class instead of a struct type?Why?

Exercise 4
Create a class, StrEncrypt with the following member functions: String EncryptForward (String input) - which will shift the input string by two characters forward - e.g. a becomes c - d becomes f - z becomes b String EncryptBackword(String input) - which will shift the input string by two characters backward - e.g c becomes a - d becomes b - b becomes z Create a windows form to test your class.

3/9

HaNoi University of Technology

Week 3: Object-based programing C# Mastering C# 2008

Part II: Members of Class


Exercise 5
(Deference between static method and instance method) Class A { static public void Static_Member() { Console.WriteLine("Calling from Static_Member"); } public void Instance_Member() { Console.WriteLine("Calling from Instance_Member"); } } Main: A myA = new A(); A.Static_Member(); // Call static methods with class name. myA.Instance_Member(); // Use instance to call instance method. Run project

Exercise 6
Add properties: read-only properties, write-only properties, and read/write properties Change Class B to add read-only property (X), write-only property (Y), and read/write property (Z). public int X { get { return m_x; } } public int Y { set { m_y = value; } } public int Z { get { return m_z; } set { m_z = value; } }

4/9

HaNoi University of Technology

Week 3: Object-based programing C# Mastering C# 2008

Coding to use these Propertier:

Exercise 7
Write a program to display the name and age of a person. Use a default constructor to assign values to the name and age variables. Use a parameterized constructor to pass the values of name and age. Use a single method to display the values from both the constructors. Output of program:

5/9

HaNoi University of Technology

Week 3: Object-based programing C# Mastering C# 2008

Exercise 8
Write a program that calculates the square of an integer, say 3 and a double, say 4.2. Use method overloading to calculate the square of the integer and double values. Output of program:

Exercise 9
Write a program that displays the values of two integers; intX and intY. Assign the values 10 and 20 to intX and intY respectively using a parameterized constructor. Include a default constructor that does nothing. Instantiate an object of this constructor and print the values of the variables intX and intY. Overload the operator to change the sign of the values (i.e. negative values). Display the values of intX and intY. The output of the program:

6/9

HaNoi University of Technology

Week 3: Object-based programing C# Mastering C# 2008

7/9

HaNoi University of Technology

Week 3: Object-based programing C# Mastering C# 2008

Part III: Indexers


Exercise 10
Create an indexer to store odd numbers in the range of 1 to 10. The output of the program:

8/9

HaNoi University of Technology

Week 3: Object-based programing C# Mastering C# 2008

Home work
Exercise 1
Write a program having a class Student. Create a field stud_no and property stud_name. Assign and display values for the property and field. Output example:

Exercise 2
Modify the class Student in exercise 1, to make stud_name as a readonly property Output example:

Exercise 3
Create an indexer to store the names of students and overload the same to store marks of three subjects of the students Output example:

9/9

Das könnte Ihnen auch gefallen