6 class and methods

31
Class and Method [email protected] Programming in C#

Upload: tuan-ngo

Post on 10-May-2015

482 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 6    class and methods

Class and Method

[email protected]

Programming in C#

Page 2: 6    class and methods

Class and Objects

Page 3: 6    class and methods

Object-oriented programming

Programming languages designed based on data and ways to

manipulate data.

Procedural Languages (Pascal, C) focus on ways

Object-oriented Languages (C#, Java) focus on data

Page 4: 6    class and methods

Features

Abstraction Encapsulation

Inheritance

Polymorphism

Focus only the required information from objects

Page 5: 6    class and methods

Features

Abstraction

Encapsulation Inheritance

Polymorphism

Hide details of what a class contains

Page 6: 6    class and methods

Features

Abstraction

Encapsulation

Inheritance Polymorphism

Create a new class based on the attributes and methods of an existing class

Page 7: 6    class and methods

Features

Abstraction

Encapsulation

Inheritance

Polymorphism

Behave differently in different situations

Page 8: 6    class and methods

Class

Page 9: 6    class and methods

Class – should & cannot

Should be a noun

first letter capitalized

simple, descriptive, meaningful

Cannot

in mixed case

C# keyword

begin with a digit except @, _

Page 10: 6    class and methods

Methods

Page 11: 6    class and methods

Method – can & cannot

Can

begin with a letter, _ or @

Cannot C# keyword

contain space

begin with digit

Page 12: 6    class and methods

Invoking methods

https://gist.github.com/2349623

Page 13: 6    class and methods

Static methods

called without creating any objects of the class

refer only to static variables and other static methods of class.

https://gist.github.com/2349658

Page 14: 6    class and methods

Static methods

Only one copy of static variable is shared by all the objects of the class.

Page 15: 6    class and methods

Question?

https://gist.github.com/2349708

Page 16: 6    class and methods

Access Modifiers

Page 17: 6    class and methods

Access Modifiers

Page 18: 6    class and methods

Ref

causes arguments to be passed in a method by reference

https://gist.github.com/2344456

Page 19: 6    class and methods

Out

similar to ref but no required the variables that are passed by

reference to be initialized https://gist.github.com/2344483

Page 20: 6    class and methods

Method overloading

Page 21: 6    class and methods

Method overloading

every method has a signature which comprises

the number of params

the data types of params

the order of params

Page 22: 6    class and methods

Should and avoid

Methods to be overloaded should perform the same task

The signatures of the overloaded must be unique

Return type is not a part of the signature

The ref and out parameters can be included as a part of the signature

Page 23: 6    class and methods

This keyword

refer to the current object of the class.

cannot use this keyword with static variables and method

https://gist.github.com/2344534

Page 24: 6    class and methods

Question? https://gist.github.com/2349708

Page 25: 6    class and methods

Constructors and Destructors

Page 26: 6    class and methods

Constructors

initialization

no return type

possible to have overloaded constructors

https://gist.github.com/2344570

Page 27: 6    class and methods

Default Constructors

C# creates a default constructors for a class if no constructor is

specified within the class. It automatically initializes all the numeric data type instance variables to zero.

If you define a constructor in the class, the default constructor is no longer used.

Page 28: 6    class and methods

Static Constructors

initialize static variables of the class and to perform a particular

action only once.

is invoked before any static member of the class is accessed.

does not take any parameters and does not use any access modifiers because it is invoked directly by the CLR instead of the object.

Certainly, cannot access non-static data member. https://gist.github.com/2344643

Page 29: 6    class and methods

Constructor Overloading

https://gist.github.com/2344673

Page 30: 6    class and methods

Destructors

invoked automatically when the objects are not in use

only one destructor in a class

cannot be overloaded or inherited

cannot be explicitly invoked

cannot specify access modifiers and cannot take parameters

https://gist.github.com/2344708

Page 31: 6    class and methods

Garbage Collector