Αποτελέσματα Αναζήτησης
Use keyword “abstract” for polymorphic methods for which the base class does not define a body If any method in class abstract, class must be abstract
Abstract class is a special type of class which cannot be instantiated and acts as a base class for other classes. Abstract class members marked as abstract must be implemented by derived classes.
• Interface = purely abstract class; only signatures, no implementation. • May contain methods, properties, indexers and events (no fields, constants, constructors, destructors, operators, nested types).
void HandleShape(Shape s) { s.Draw(); ... abstract class Shape { public abstract void Draw(); } class Box : Shape { public override void Draw() { ... } } class Sphere : Shape { public override void Draw() { ... } } void HandleShape(Shape s) { s.Draw(); ... HandleShape(new Sphere()); HandleShape(new Shape()); // Error!
C#.net Classes and objects 3 Definitions (cont’d) • Interface – in principal a purely abstract class – used for “can-do” instead of “is-a” relationships. – removes most needs for multiple inheritance •this – reference to current object
The abstract keyword is used for classes and methods: Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the derived class (inherited from).
It is possible to define an abstract class that contains no abstract methods. This class is used as a base class for defining new subclasses. We still define its constructors, which are invoked in the constructors of its subclasses through constructor chaining. 16 Rectangle classes. geo[0] = new Circle(); geo[1] = new Rectangle(); ...