C #

xiaoxiao2021-03-06  65

// * The role of this example * Designer: Weng Fuguo QQ: 171716420 * 1. Analyze Custom Abnormal * 2. Analyze the serializable process * 3. Example of attribute analysis * 4. Usage of indexes * 5. Static data, method, definition and use of attributes * /

Using system; using system.runtime.serialization.formatters.binary; using system.io;

// Digest description for analyzing custom abnormal AAANAMESPACE WFGA002 {///

/// Class1. /// /// public class myexception: system.exception {public myException (INT A): base (A "age is not more than 0--100") {

} Public void show () {system.console.writeline; system.console.writeline ("TOSTRING:" Base.toString ()); System.Console.writeline ("Target: " Base.targetsite; System.Console.writeline (" Soure: " Base.Source); System.Console.writeline (" stact: " base.stacktrace);

}} // Ordinary Human [system.serializable] public class person {// data represents a student number private int ino; // Data means name private string strname; // data represents age private int IAGE; // Constructor PUBLIC PERSON INT NO, STRING Name, INT AGE) {Inumber ;

INO = NO; STRNAME = Name; IAGE = AGE;} // Attribute Indicates a student number public int No {get {return ino;}} // property means name public string name {get {return name;}} // attribute Age Public Int Age;} Set {IAGE;} SET {IF (Value> 0 && Value <100) {// Write Age> 0 and <100 IAGE = VALUE;

} Else {// The age of <0 and> 100 occurrence of exception IAGE = -1;

Throw new myexception (value);

}} // end IAGE-SET} // -------------------- Inherit and overload from the Object class ----------- ---- // TOSTRING method for object public override string toString () {return "no:" ino "/ nname:" strname "/ nage:" IAGE;} / / More than two classes Public override Bool Equals (Object Obj) {Return Base.equals (OBJ); PUBLIC OVERRIDE INT GETHASHCODE () {Return Base.gethashcode ();} // -------------- --------- Static area ---------------- // Total number private static int inumber; // Static construction method static person () {inumber = 0;

} // Static attribute Returns the total number of people, (read-only) public static int number {get {return inumber;}} // Static method: The total number of people is displayed. Public static void showshare () {system.console.writeline ("Now there is a total of: {0}", inumber;

}

} [System.serializable] public class student: Person {private double [] dblscrocE; // internal processing method. ///

/// Method Private void prcess () /// function: calculation average and total score /// private void prcess () {dblscroce [dblscrocroce.length-2] = 0.0d ;

For (int i = 0; i

} DBLScroce [dblscrocroce.Length-1] = dblscroce [dblscrocroce.length-2] / (dblscription.Length-2);} public double this [int index] {set {system.console.writeline ("Value1: {0} Value2: {1} ", index, dblscrocrocroce.length -2;

IF (index <0 || index> = dblscrocroce.length-2) {system.console.out.writeLine ("sss");

Throw new indexoutofrangeexception ("Beyond. No. Kid");

} Else {dblscroce [index] = value; this.prcess ();

}

} Get {if (INDEX <0 || index> = dblscrocroce.length) {throw new indexoutofrangeexception ("WF is beyond. No, kid");

} Return DBLScroce [INDEX]; PUBLIC Student (int No, string name, int agent): base (no, name, agn) {dblscroce = new double [scroce.length 2]; //// Calculate the total score and average grades for (int i = 0; i

} Dblscroce [scrocrocength 1] = dblscroce [scroce.length] /scroce.length;

}

Public Double [] scrudE {get {// Re-calculate the average score this.prcess ();

Return dblscroc

} // End get_scroce

} Public override string toString () {string temp = "/ n"; for (int i = 0; i

Return Base.toString () TEMP;

}

} Class Class1 {///

/// The primary entry point of the application. /// public static void serializable (object obj) {if (null == obj) {throw new system.nullreferenceException ("OBJ has no instance", new system.exception ("Weng Fuguo sss");

} System.Runtime.Serialization.IFormatter foramtter = new BinaryFormatter (); System.IO.Stream stream = new FileStream ( "D: //JFC//a.bin", FileMode.Create, FileAccess.Write, FileShare.None) ; foramtter.Serialize (stream, obj); stream.Close ();} public static void DeSerializable (out object obj) {System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter (); System.IO.Stream stream = new FileStream ("D: //jfc//a.bin", filemode.open, fileaccess.read, fileshare.read; obj = formatter.deSerialize (stream); if (null == obj) {throw new system.nullreferenceException "This backlink failed");}} public static void show (Object obj) {// If OBJ is an instance of a class Student to enforce the type transformation Student Obj1 = Obj Is Student? (Student) OBJ: NULL; System.Console .Writeline ()); System.Console.writeline ("------------- Score --------------") ((Student) obj). Scroce [0] = 123; for (int I = 0; i <((student) Obj). Scroe.Length; i ) system.console.writeline ("score:" ( STUDENT) OBJ). Scroce [i] .tostring ()); System.Consol E.Writeline ("------------- this [index] -----------------"); try {// (Student) OBJ) [4] = 88; ((student) OBJ) [1] = 70;

} Catch (system.exception ex) {system.console.writeLine (ex);

} For (int i = 0; i <(student Obj). Scroe.Length; i )

System.console.writeline ("Score:" ((Student) Obj). Scroe [i] .tostring ());

} Public static person getStuent () {Return New Person (1, "SS", 2);

} [Stathread] static void main (string [] args) {// // Todo: Add code to start the application // Object obj, obj1; double [] scrocE = {60, 78, 90}; OBJ = New Student (2005, "Weng Fuguo", 200, Scroce; Obj1 = NULL; try {// ((( (Person )Obj ).age = 200; serializable (OBJ); Deserializable (out obj1); show (Obj1) Student.showshare ();

} Catch (system.indexoutofrangeexception ex) {system.console.writeline (ex);

} Catch (myException ex) {system.console.writeline (ex); ex.show ();

}

}}}

转载请注明原文地址:https://www.9cbs.com/read-90349.html

New Post(0)