One of the most prevalent powerful programming languages.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
CS 211 Inheritance AAA.
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Abstract Classes and Interfaces
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Module 7: Object-Oriented Programming in Visual Basic .NET
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Programming in Java CSCI-2220 Object Oriented Programming.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
Object-Oriented Programming Chapter Chapter
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Coming up: Inheritance
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Overview of C++ Polymorphism
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
C# for C++ Programmers 1.
Inheritance and Polymorphism
Structs.
CS 302 Week 11 Jim Williams, PhD.
OOP’S Concepts in C#.Net
Interfaces.
Delegates & Events 1.
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Java Programming Language
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chengyu Sun California State University, Los Angeles
Presentation transcript:

One of the most prevalent powerful programming languages

Hi, C# class Program { static void Main() { System.Console.Write("Hi"); }

C# Reads C-Sharp Is a computer programming language  Server Side  Asp.net  Client Side  SilverLight Is an ECMA standard, spearheaded by MS

Run on.net framework Developed in, say, Visual Studio For all kinds of software Web,  Service, etc  silverlight Windows Form,

Typed Every value is typed C# is strong typed, different from ES Every value is introduced after type declared The type cannot be changed all the val’s life.

Type before Instance Every value is an instance of a type. Many instances are abstracted by type Each instance can have their own state  Stored in each instance Their behaviors are of the same model  Stored in type.  Vary due to data of each instance

Type definition and instantiation In C#, We define types, the model  Behavior/events  Data storage space, field Instantiate  Create a new thing from the model  Store different values in the data field.

Type definition and instantiation Note, Type can have its own behavior/data, which will not be copied/referenced by instances.  Such as constant/static ones Other members can be inherited

Run! Call a type/instance’method In which other methods may be called Data may be changed Then stop.

Coding So in c#, the coding is about typing and instantiating Types first.

Kinds of Types by declaration enum struct class interface delegate

By reference enum struct class interface delegate byValue byReference

By val vs by ref By val By ref Literal in var Pointer in var Literal in another place long i=1; object a=1; 1 1 1A3D136u i a 1 1

Types vs Types Subtype Any instance of a is also instance of b Association A’s property is b

SubType Object Number Student Integer Negative Person Male Negative Integer Student Male Function Color Object always top Directional Inherit from multiple Supertype chain can be 0,1,2, … steps long. The instance of a type is also instance of any type on the supertype chain Supertype chain can be 0,1,2, … steps long. The instance of a type is also instance of any type on the supertype chain

Example public enum Color{ Red, Blue, Green }

Syntax Case sensitive Type’s initial is conventionally capitalized Type def comprises signature and body {static} {public/…} {abstract/sealed} TypeKeyword typename:supertype[1],…{ …//the body }

Signature Modifiers static No instance can be created; not inheritable. access public: Visitable everywhere private: only here internal: in this project/package protected: in the subtypes protected internal: subtypes or within package abstract No instance; subtype can sealed No inheritace

members Define: Fields Constructor/destructor Methods/properties/indexer Events Inner types  ChessBoard  Square-used only here Different for declaration kinds of types

Field class Student{ public string id; private DateTime dob; } //Note the type and varName

ctor To init fields No return Can take args Same name as class Types with no fields have no ctor class Student{ Sex sex; public Student(Sex sex){ this.sex=sex; }

destructor Clear up, e.g., release held resources such as db. No return Class name preceded by ~ Not necessary Often explicitly done by other methods like close/Finalizer/destroyer, etc public class Student{ ~Student{ //close database, for example }

Method class Stduent{ public int age(){ //return currentTime-dob; }

Property class Stduent{ public bool performance{ protected get{ //return grades; } private set{ //forbid } //property is like a method without parameter

Indexer public class HbueStudent{ public Student this[int x]{ //return student one by one } HbueStudent hs=new HbueStudent(); hs[0];//a student //note the keyword this. //it’s like a method

Event Event is a field where a series of methods wrapped as delegates will be triggered.

Static member public class Circle { static public double Tau=6.28; double area(){ // } //Note if a member is static, it’s a member of the type itself. //It cannot be inherited. For exmaple Disc inherit Circle’s area, but Circle.Tau insteadof Disc.Tau

enum public enum Sex{ Male, Female } //Male can be regarded as the static property of Sex, so its initial is capital. //Object is implicitly the supertype. can not inherit any ther. //By value //stored as int //used like: Sex s=Sex.Male;

struct public struct Point{ double X; double Y; public void Translate(){ … } //note struct has only one implicit supertype object.

interface interface IShape { public double area(); //… } // no field; no implementation; //implementation will be in classes subtypes //it’s a placeholder for multiple inheritance, as is impossible for class

class public class Student{ //fields //methods //properties //indexer //event //subtypes }

delegate public delegete double BiOp(double x, double y); //inherit Delegate/MultiDelegate implicitly //can only store corresponding functions BiOp a=(double x, double y)=>return x+y; BiOp b=()=>return;//wrong!

Use your defined type to introduce a var Student s; Color c; BiOp o; Ishape shape; Point p;

new s=new Student();

Invoke members s.run();//method, in which other instances might be created and invoked s.sex;//data

Console application We can code different projects Console is a simple one

In VS An application is a project or many projects When creating projects, select Console, select c# as the lang. One or more files/folders will be added to the project. C# source code file’s extension is.cs

Coding One and only one class has a static Main method The system will invoke this type’s Main and start the application.

C# windows

solution project Source code opend subitem

Hi, C# class Program { static void Main() { System.Console.Write("Hi"); }

struct and enum is where to hold literal values class type declaration is where to hold functions/fields Var of such type is a reference Instances hold fields delegate/interface is references The reference chain will finally lead to literal values.

Class vs struct reference value In most cases, the two might both do.

Class vs interface Having instances Single inheritance No instance Multiple inheritance Reference type

Why interface Multiple inheritance Conceptual, type diagram You don’t have to care much about detail

Class class is the most used type static void Main method of a class is the entry point of a program.

Type diagram In visual studio Draw diagram to illustrate the subtype relations You can also add members to types on the diagram Visual studio will generate the source code for you.

Construct An instance has all the fields along the subtype chain. Its constructor will call supertype[1]’s constructor, which in turn will call upward, and so on, till object, where constructor init fields, then return, letting the lower chain to init subtype’s fields. This process ensures supertype’s fields initiated before subtype’s constructor reference those fields in its running.

construct An instance’s constructor can designate which of the supertype’s constructor to be called The default one is the one with no paras.

destruct Destruction from bottom up.

virtual override A member can be overridden in subtype A{ virtual m(){} } B:A{ override m(){} } A b=new B(); b.m();//call B.m()

Override vs overload Overload is within a same type where methods of the same name taking different type of paras Override is About the inheritance among types Method of the same name in subtype overrun that in supertype.

Polymorphism For instances of the same type, but of different subtypes, Same-named methods might be different due to overriding. E.g. All animals will eat, but the ways of eating differ

Func a=(int x)=>x+1;

C# Delegates and Events Defining and using Delegates three steps:  Declaration  Instantiation  Invocation

C# Delegates and Events Delegate Instantiation delegate void MyDelegate(int x, int y); class MyClass { private MyDelegate myDelegate = new MyDelegate( SomeFun ); public static void SomeFun(int dx, int dy) { } static public void Main(){ myDelegate(3,5); } Type declare instantiate

Event

public delegate void FireThisEvent(); class MyEventWrapper { private event FireThisEvent fireThisEvent; public void OnSomethingHappens() { if(fireThisEvent != null) fireThisEvent(); } public event FireThisEvent Handle FireThisEvent { add { fireThisEvent += value; } remove { fireThisEvent -= value; } } //somewhere else, someone will add to the events. And when something happens, onSomethingHappes will fire all the delegate in that events.

[…] class A{ […] void m(){} }

Attributes Querying Attributes [HelpUrl(" class Class1 {} [HelpUrl(" HelpUrl(" Tag=“ctor”)] class Class2 {} Type type = typeof(MyClass); foreach (object attr in type.GetCustomAttributes() ) { if ( attr is HelpUrlAttribute ) { HelpUrlAttribute ha = (HelpUrlAttribute) attr; myBrowser.Navigate( ha.Url ); }

The End Thanks