在職教師教育網路應用系統程式 分享與實作研習 ---C# Tutorial(Code C) 行政網路組 傅志雄.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 211 Inheritance AAA.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
物件導向程式 授課教師 : 王耀德 研究室 : 靜宜大學 二研 105 電話 : (04) # Web site:
Inheritance and Polymorphism
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance using Java
物件導向實習 極高的忘記答題率 … AB 卷都有的題目 : 4(1). Define method overloading 明明寫出了方法, 卻不回答老師的題目 (1)( 只要 寫出定義就好了 ) 另外, 讀題一定要仔細 : Two overloaded methods average.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Class & Object 蔡柏灃.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Object Oriented Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming Pillars Introduction to Object- Oriented Programming.
What is inheritance? It is the ability to create a new class from an existing class.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Classes, Interfaces and Packages
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes (Part 1) Lecture 3
Static data members Constructors and Destructors
2.7 Inheritance Types of inheritance
Inheritance and Polymorphism
Module 5: Common Type System
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
Learning Objectives Classes Constructors Principles of OOP
Computer Programming with JAVA
CS2011 Introduction to Programming I Objects and Classes
CIS 199 Final Review.
Class.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chapter 7 Inheritance.
CS 240 – Advanced Programming Concepts
Presentation transcript:

在職教師教育網路應用系統程式 分享與實作研習 ---C# Tutorial(Code C) 行政網路組 傅志雄

Object Oriented Programming with C#

Classes and Objects 抽象化 (Abstraction) 關念 Defining Classes [attributes] [access-modifiers] class identifier [:base-class] {class-body} [assembly:AssemblyKeyFile(".\\keyFile.snk")] public class Tester { public static Main( ) { //... }

Classes and Objects-2 Simple Time class using System; public class Time { // public methods public void DisplayCurrentTime( ) { Console.WriteLine(“ 列印 ……"); } // private variables int Year; int Month; int Date; } public class Tester { static void Main( ) { Time t = new Time( ); t.DisplayCurrentTime( ); }

Classes and Objects Access Modifiers public private protected Internal( assembly ) Protected internal

public class Customer { public int Field1; internal int Field2; private int Field3; protected int Field4; protected internal int Field5; } Encapsulation and Member Accessibility Encapsulation-- 物件導向概念中,「封裝」可說是物件狀態的隱藏過程,或指程式實 作的隱藏 (implementation hiding) There are five levels of member accessibility –public: accessible to everyone –internal: accessible from within current assembly –private: accessible from this class only –protected: accessible from this class and from child classes –protected internal: union of protected and internal accessibility

Classes and Objects-5 Creating Objects Time t = new Time( );

Classes and Objects-4 Method Arguments void MyMethod (int firstParam, string secondParam) { //... }

Constructors Constructors are special methods designed to initialize fields –CLR executes constructor whenever New is called on a class –creatable classes must have at least one constructor –constructors defined using procedures with same name as class name –constructors can be parameterized and overloaded –parameters passed by client after class name public class Customer { private string m_Name; private string m_Phone; public Customer(string Name, string Phone) { m_Name = Name; m_Phone = Phone; } Customer c1; c1 = New Customer("Wendy", " "); Customer c2 = New Customer("Bob", " ");

Default constructor Non-parameterized constructor called "default constructor" –allows client to create object without passing parameters –C# compiler automatically adds a default constructor to classes that have no explicit constructor –default constructor (if desired) must be explicitly added to classes that contain other constructors public class Class3{ public Class3(string s){ //implementation } public Class3(){ //implementation } Class1 c1 = new Class1(); Class2 c2 = new Class2(); Class3 c3 = new Class3(); public class Class2{ public Class2(string s){ //implementation } public class Class1{ //no explicit constructor } Calls default constructor Illegal: no default constructor Calls default constructor

Classes and Objects [Constructors] public class Time { public void DisplayCurrentTime( ) { System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",Month, Date, Year, Hour, Minute, Second); } public Time(System.DateTime dt) { Year = dt.Year; Month = dt.Month; Date = dt.Day; Hour = dt.Hour; Minute = dt.Minute; Second = dt.Second; } int Year; int Month; int Date; int Hour; int Minute; int Second; } public class Tester { static void Main( ) { System.DateTime currentTime = System.DateTime.Now; Time t = new Time(currentTime); t.DisplayCurrentTime( ); } // 參考 demo01.txt

Overloading methods and properties Two or more class members can be given the same name –you can overload a set of methods or a set of properties Overloaded members must differ with their parameter lists –parameter lists must differ in size and/or in sequence of types –you cannot overload based on return type or parameter name public class CustomerManager { public string GetCustomerInfo(int ID) { //implementation } public string GetCustomerInfo(string Name) { //implementation } //client-side code string info1, info2; CustomerManager mgr = new CustomerManager(); //call GetCustomerInfo(Integer) info1 = mgr.GetCustomerInfo(23); //call GetCustomerInfo(String) info2 = mgr.GetCustomerInfo(“fu"); // 參考 demo02.txt

Encapsulation using Properties Properties require new syntax –each property must implement a Get block and/or a Set block public class Customer { //private field private string m_Name; //property controlled access to private field public string Name { //perform calculations if necessary get { return m_Name; } //perform validation here if necessary set {m_Name = value;} } //client-side code string s; Customer obj = new Customer(); obj.Name = “fu"; // triggers Set block s = obj.Name; // triggers Get block Set block Get block // 參考 demo03.txt

Implementation inheritance An OOP technique to reuse code across classes –derived class inherits implementation from base class –inheritance relationships create inheritance hierarchies Inheritance establishes "IS A" relationship between classes 「是一個」 VS. “Has A” Manager "IS A" Human Programmer "IS A" Human ManagerTraineeSeniorManager ProgrammerManager Human

Inheriting from a base class Class can explicitly inherit from a base class –class defines base class using Inherits keyword –class can only have one base class (no multiple inheritance) Class without explicit base class inherits from System.Object public class Human{ public string Name; public string Speak() { return "Hi I am a human named:" + Name; } public class Manager : Human { //Manager specific implementation here } public class Programmer : Human { //Programmer specific implementation here } // 參考 demo04.txt

Extending from a base class public class Human { public string Name; public string Speak() { return "Hi I am a human named:" + Name; } public class Manager : Human { //Manager specific implementation here public string Name2; public string Speak2() { return "My Dad is"+Name+"---Hi I am a human named:" + Name2; } // 參考 demo05.txt

Base classes and constructors Constructors and base types have "issues" –derived class contract doesn't include base class constructors –derived class must provide its own constructor(s) –derived class constructor must call a base class constructor –compiler can automatically generate call to accessible default constructor in base class constructor if one exists public class Human { protected string m_Name; public Human(string Name) { //implicit call to System.Object constructor m_Name = Name; } public class Programmer : Human { //doesn't compile //base class has no accessible default constructor! } 編譯出問題

Constructor Chaining public class a { public a() { System.Console.WriteLine(" 產生 a Class"); } public class b : a { public b() { System.Console.WriteLine(" 產生 b Class"); } public class c : b { public c() { System.Console.WriteLine(" 產生 c Class"); } public class d : c { public d() { System.Console.WriteLine(" 產生 d Class"); } // 參考 demo06.txt

Calling base class constructor 解決 1 –Base class constructor called via base keyword can only be called once public class Human { public Human() { } protected string m_Name; public Human(string Name) { //implicit call to System.Object constructor m_Name = Name; } public class Programmer : Human { } 增加 空 default Constructor // 參考 demo07.txt

Calling base class constructor 解決 2 –Base class constructor called via base keyword can only be called once public class Human { protected string m_Name; public Human(string Name) { //implicit call to System.Object constructor m_Name = Name; } public class Programmer : Human { public Programmer(string Name):base(Name) { //chaining a call to base class constructor } Special syntax for constructors // 參考 demo08.txt

待續 ……… 11/21 止