C # method example

Source: Internet
Author: User

1. Differences between static and non-static methods: Example 1 :( 1) static Method namespace static method {class Program {public static int Add (int x, int y) // declare a static method {return x + y;} static void Main (string [] args) {Console. writeLine ("Xue min, our calculation result is:" + Program. add (2, 4); // use the class name to call the static method }}} running result: (2) non-static method namespace non-static method {class Program {public int Add (int x, int y) // declare a non-static method {return x + y ;} static void Main (string [] args) {Program xuemin = new Progr Am (); // instantiate the class Program Console. writeLine ("Xue min, the calculation result is" + xuemin. add (2, 4); // use the instantiated object xuemin to call the defined non-static method.} running result: 2. Method overloading and method rewriting are different: example 2: (1) method overload namespace method overload {class Program {public static int Add (int x, int y) // defines the static method Add and returns the int type, two int type parameters: {return x + y;} public double Add (int x, double y) // Method Add. Two parameters are of the double type, one int type {return x + y;} public int Add (int x, int y, int z) // Method Add, three parameters, all of which are int type {re Turn x + y + z;} static void Main (string [] args) {Program xuemin = new Program (); int x = 3; int y1 = 4; double y2 = 3.2; int z = 5; Console. writeLine ("Xue min, the following is the calculation result. Please check it !! "+" \ N "); // call the Console of the Add overload method based on the input parameter type and number. writeLine ("x" + "y1" + "=" + Program. add (x, y1); Console. writeLine ("x" + "y2" + "=" + xuemin. add (x, y2); Console. writeLine ("x" + "y1" + "z" + "=" + xuemin. add (x, y1, z) ;}} running result: (2) for method rewriting, see Example 3. There is method rewriting. 3 can be learned through example 3: 1, abstract classes cannot be directly instantiated. How can they be instantiated? -Instantiate the derived class first, and then use the object of the derived class to instantiate the use of abstract class 2, abstract class and abstract class: Example 3: declare an abstract class myClass, which declares two attributes and a method, the specific implementation of the two attributes is provided, and the method is an abstract method. Then declare a derived class DriveClass, inherit from myClass, override the abstract method in myClass in the DriveClass derived class, and provide specific implementation. Finally, instantiate an object of the DriveClass derived class in the Main method of the Main Program class Program, use this object to instantiate the abstract class, and use the abstract class object to access the attributes of the abstract class and the method of rewriting in the derived class. Use of the namespace abstract class and abstract method {abstract class myClass // declare an abstract class myClass {string id; string name; public string ID // ID {get {return id ;} set {id = value ;}} public string Name // Name attribute name {get {return name ;}set {Name = value ;}} public abstract void ShowInfo (); // declare an abstract method ShowInfo} class DriveClass: myClass // declare a derived class DriveClass, derived from class myClass {public override void ShowInfo () // The derived class DriveClass overrides the abstract method ShowInfo {Console. writeLine (ID + "" + Name) ;}} class Program {static void Main (string [] args) {DriveClass driveclass = new DriveClass (); // instantiate the derived class DriveClass myClass xuemin = driveclass; // use the derived class Object driveclass to instantiate the abstract class xuemin. ID = "001 Langfang Normal University"; xuemin. name = "Han xuemin"; xuemin. showInfo (); // The abstract class object calls the method Console in the derived class. writeLine ("\ n") ;}} running result: 4. Examples of sealing class and method 4: www.2cto. comnamespace sealing class and sealing method {class myClass1 // Declaration class myClass1 {public virtual void ShowInfo () // virtual method ShowInfo {}} sealed class myClass2: myClass1 // declare the sealing class myClass2, and inherit from myClass1 {string id = ""; string name = ""; public string ID // number attribute ID {get {return id ;} set {id = value ;}} public string Name // Name attribute name {get {return name ;}set {Name = value ;}} public sealed override void ShowInfo () // seal and override the ShowInfo method {Console in the base class myClass1. writeLine (ID + "" + Name) ;}} class Program {static void Main (string [] args) {Console. writeLine (" min, result after ShowInfo method is called:" + "\ n"); myClass2 xuemin = new myClass2 (); // instantiate the sealing class myClass2 xuemin. ID = "001 Langfang Normal University"; xuemin. name = "Han xuemin"; xuemin. showInfo (); // call the Sealing Method in the sealing class myClass2. myClass2 xueyang = new myClass2 (); xueyang. ID = "002 Langfang Normal University"; xueyang. name = "Han xueyang"; xueyang. showInfo () ;}} running result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.