Polymorphic:
With inheritance, there are many States;
Concept: Inheriting all subclasses of the same parent class, using different, they are a separate individual;
In the ordinary parent class, virtual methods can appear, and virtual methods can be overridden in subclasses;
Virtual method: Virtual
Override: Override
Parent class, base class
Subclass, derived class, Super class
Namespace Polymorphism { class Ren {public void Chifan () { Console.WriteLine ("I have to eat!") "); } public virtual void Xihuanchi () -----virtual method { Console.WriteLine ("Like to eat meat, vegetables, fruit! "); } }}
Namespace Polymorphism { class Nan:ren {public override void Xihuanchi () ---=--override { Console.WriteLine ("I Am a man, I only like to eat meat!") "); } public void Zuqiu () --------Subclass owned, the parent class does not have a method { Console.WriteLine ("I like football! "); } }
Namespace Polymorphism { class Nv:ren {public override void Xihuanchi () { Console.WriteLine ("I Am a woman, I only like to eat vegetables and fruit! "); } public void Gouwu () { Console.WriteLine ("I like shopping!") "); } }}
Namespace Polymorphic { class Xiaonanhai:nan --------subclasses can be inherited by another subclass { }}
------------------------------------------------------------------
Abstract class: Abstraction
is used to be inherited, cannot instantiate an object, because there is no constructor function;
Abstract methods, can not have the main body of methods, can only define the structure of the method;
Abstract methods or abstract attributes can only exist in abstract classes;
Abstract methods and abstract attributes are not necessarily the only abstractions in the class.
Namespace abstract class {class program { static void Main (string[] args) { nan n = new Nan (); -----is used to be inherited, can not instantiate the object, because there is no constructor, abstract method, cannot have a method of the body, can only define the structure of the method;
N.chifan (); N.xihuanchi (); Ren r = new Nan (); R.xihuanchi (); Console.readkey (); } }
Namespace abstract Classes {abstract class Ren ----Abstract classes {public virtual void Chifan ()-----are used for inheritance and cannot instantiate objects. Because there is no constructor, the abstract method cannot have the main body of the method, only the structure of the method can be defined;
{Console.WriteLine ("Eat with your mouth! "); Abstract methods and abstract attributes are not necessarily------abstract classes;
} public abstract void Xihuanchi (); -------abstract methods or abstract attributes that can exist only in abstract classes;
}}
The namespace abstract class {class Nan:ren {public override void Chifan () ----inherit when you want to define the method { Console.WriteLine ("I Am a man, I have a big mouth to eat!" "); } public override void Xihuanchi () { Console.WriteLine ("I Am a man, I like to eat meat!") "); } }}
-----------------------------------------------------------------
Interface:
In team development, a class that requires multiple modules to be combined is the complete class;
Multi-personal development of different modules, and finally to put them together, relying on the interface;
A class that needs to inherit multiple classes is complete, but the program stipulates that a class can inherit only one parent class;
In order to solve this problem, there is an interface, a class can inherit countless interfaces;
========= is like a subclass that inherits multiple parent classes
People in this category, need to eat, skills, sports, is a complete person;
Eating this function is a separate development, as the most basic parent class, using the abstract class;
Skill and movement, which are developed by B and C, require people to inherit this class, but already have the parent class;
Then B and C on the interface used to let people this class can inherit their written two function modules;
The method inside the interface is much like an abstract method;
Interface is also a function of norms and constraints;
Namespace Interface { class program { static void Main (string[] args) { ArrayList arr = new ArrayList (); ren r = new Ren (); R.chifan (); R.skill (); R.sport1 ();
Namespace interface { abstract class Huozhe {public abstract void Chifan (); }
Namespace interface {Public interface Sports { void Sport1 ();} }
Namespace interface {Public interface work { void Skill ();} }
Namespace Interface { class Ren:huozhe, work, Sports {public override void Chifan () { Console.WriteLine ( "Eat with your mouth!" "); } public void Skill () { Console.WriteLine ("Will program! "); } public void Sport1 () { Console.WriteLine ("will play football! "); }
------------------------------------------------------------------------------------------------
IS and as operators:
is to determine if it is a type that returns TRUE or False
o as Ren; if the conversion succeeds, no problem;
If the conversion is not successful, the error is not reported, but a null value is returned
= = = = = is a method of preventing the crash of a forced conversion failure program
Random ran = new random (); Arr. ADD (r); Arr. ADD (ran); foreach (Object o in arr) { ren rr = o as Ren; if (RR! = null) { rr.chifan (); } else { Console.WriteLine ("No conversion succeeded!") "); } } Console.readkey (); } }
Four-object-oriented-polymorphic, interface is, as operations