Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 27 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
27
Dung lượng
275,5 KB
Nội dung
Binding 2 Objectives • Discuss type compatibility in the presence of inheritance • Contrast ways to bind method call – static – dynamic • Cover details of dynamic binding – virtual methods – abstract methods – override – polymorphism and generic code • Use sealed to prevent inheritance and method overriding 3 Is-a relationship • Is-a means derived has everything base does, plus more class Person { public string name public int age; . } class Employee : Person { public double salary; . } Person part name age salary e Employee e = new Employee(); Employee part Employee is-a Person 4 Person p = new Employee(); Reference compatibility • Base reference can refer to derived object – types are compatible because of is-a relationship Person reference name age salary p Employee object 5 Person p = new Employee(); p.name = "Bob"; p.age = 23; p.salary = 42000.00; Access through base reference • Type of reference determines what access is allowed – can only access base class members through base reference Person reference to Employee object ok to access Person members error to access Employee members only Person part accessible name age salary p 6 Parameter compatibility • Can pass derived object to base reference parameter bool CheckAge(Person p) { return p.age >= 21; } base reference Student s = new Student (); Employee e = new Employee(); Evaluate(s); Evaluate(e); pass Student pass Employee 7 Methods in inheritance hierarchy • Common for same method to appear throughout hierarchy – each class supplies own unique version class Employee : Person { public void Promote() { } . } class Faculty : Employee { public void Promote() { salary *= 1.2; level++; if (level > 4) tenure = true; } . } class Staff : Employee { public void Promote() { salary *= 1.1; } . } 8 Binding • Can call method through base class reference – must decide whether to call base or derived version – decision often called binding void Evaluate(Employee e) { e.Promote(); } which version? Faculty f = new Faculty(); Staff s = new Staff (); Evaluate(f); Evaluate(s); pass Faculty pass Staff 9 Binding options • Programmer chooses desired type of binding – when method called through base class reference • Two options available – static – dynamic 10 Static binding • Static binding uses type of reference to determine method – default behavior void Evaluate(Employee e) { e.Promote(); } static binding calls Employee Promote Faculty f = new Faculty(); Staff s = new Staff (); Evaluate(f); Evaluate(s); pass Faculty pass Staff [...]...Dynamic binding • Dynamic binding calls method based on type of object – programmer must request dynamic binding – use keyword virtual in base class – use keyword override in derived class class Employee : Person { public virtual void Promote() {} } binding now based on object type class Faculty : Employee { public override void... Employee[3]; employees[0] = new Faculty (); employees[1] = new Staff (); employees[2] = new Administrator(); Evaluate(employees); 13 Binding efficiency • • Static binding is efficient – type of reference known at compile time – binding done at compile time – no runtime overhead Dynamic binding is less efficient – use incurs small runtime overhead – object type must be determined at runtime 14 Abstract class... public sealed override void Method() { } } class C : B { public override void Method() { } } 26 Summary • • • • • Programmer can choose method binding type – static – dynamic Choice of binding involves trade offs – static binding more efficient – dynamic binding used to write more generic code Can use abstract to model abstract method or class Can use sealed to prevent inheritance or method override... e) { e.Promote(); } Faculty f = new Faculty(); Staff s = new Staff (); Faculty Staff Evaluate(f); Evaluate(s); class Staff : Employee { public override void Promote() { } } 11 Generic code • Dynamic binding used to write generic code – dynamic behavior often called polymorphism generic void Evaluate(Employee[] employees) { foreach (Employee e in employees) e.Promote(); } Employee[] employees = new . Static binding • Static binding uses type of reference to determine method – default behavior void Evaluate(Employee e) { e.Promote(); } static binding. Faculty pass Staff 11 Dynamic binding • Dynamic binding calls method based on type of object – programmer must request dynamic binding – use keyword virtual