3. C # object-oriented: encapsulation, inheritance, polymorphism, String, set, file (bottom ),
Object-oriented Polymorphism
I. packing and unpacking
Packing: Convert the value type to the reference type. Object o = 1; Value Type assigned to reference type
Binning: converts a reference type to a value type. Int n = (int) o; forced conversion to Value Type
Conditions met: whether there is an inheritance relationship between the two types.
Int n = Convert. ToInt32 ("12"); no packing or unboxing occurs. Int is stored in the stack, string is stored in the heap
Packing or unpacking may affect program running events.
2. Object-oriented Polymorphism
Multiple States are displayed when an object calls the same method.
1. Virtual Methods
Mark the method of the parent class as a virtual method and use the keyword virtual. You can re-write the method of the quilt class.
Protected void Page_Load (object sender, EventArgs e) {person [] p = new person [2]; chinese p1 = new chinese (); jpan p2 = new jpan (); p [0] = p1; p [1] = p2; Response. write (p [0]. sayHi (); // The method called depends on the object Response. write (p [1]. sayHi ();} public class person {public virtual string SayHi () // The parent class method uses virtual to indicate that this method is a virtual method {return "human ";}} public class chinese: person {public override string SayHi () // The subclass method uses override to overwrite the virtual method {return "" ;}} public class jpan: person {public override string SayHi () // override {return "Japanese ";}}
2. abstract class
When the methods in the parent class do not know how to implement them, you can enter the parent class as an abstract class and the methods as abstract methods.
Abstract classes are defined as abstract classes, abstract methods are defined as abstract methods, and abstract functions have no method bodies.
The abstract class cannot instantiate the parent class. It is called directly by instantiating the subclass.
The virtual method is used when the parent method is meaningful, and the abstract class is used when the parent method is meaningless.
Protected void Page_Load (object sender, EventArgs e) {// The abstract class cannot be instantiated. Only the subclass object xingzhuang y = new yuan (5) can be instantiated. double mianji = y. mianji (); double zhouchang = y. zhouchang (); Response. write ("the prototype with a radius of 5 is:" + mianji + ", perimeter:" + zhouchang); xingzhuang ju = new juxing (10, 20); double mianji1 = ju. mianji (); double zhouchang1 = ju. zhouchang (); Response. write ("the rectangle with a width of 10 and 20 is:" + mianji1 + ", perimeter:" + zhouchang1);} publ Ic abstract class xingzhuang // The class marked as abstract is called the abstract class {public string Name {get; set;} // The abstract class can contain instance members, and the strength members can be implemented without the quilt class. Public abstract double mianji (); // abstract methods must be marked as abstract, and cannot have any implementations. They must be in abstract classes. Public abstract double zhouchang (); // The access modifier of the abstract member cannot be private} public class yuan: xingzhuang {public double R {get; set;} public yuan (double r) {this. R = r;} public override double mianji () // After the subclass inherits the abstract class, all abstract members in the parent class must be overwritten {return Math. PI * this. R * this. r;} public override double zhouchang () // After the subclass inherits the abstract class, all abstract members in the parent class must be overwritten {return 2 * Math. PI * this. R ;}} public class juxing: xingzhuang {public double width {get; set;} public double height {get; set ;} public juxing (double height, double width) {this. height = height; this. width = width;} public override double mianji () // After the subclass inherits the abstract class, all abstract members in the parent class must be overwritten {return this. height * this. width;} public override double zhouchang () // After the subclass inherits the abstract class, it must overwrite all abstract members in the parent class {return (this. height + this. width) * 2 ;}}View Code