C # static method calls a non-static method

Source: Internet
Author: User

Reprint: http://blog.csdn.net/seattle1215/article/details/6657814

  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Namespace Cash
  6. {
  7. Class Program
  8. {
  9. static void Main (string[] args)
  10. {
  11. (new Program ()). Run ();
  12. //Writefee (Calculate (dailyrate, WorkDay));
  13. //For non-static methods, if you imagine a direct call like above, you will report the following error
  14. //Error 1 An object reference is required
  15. //For the Non-static field, method, or
  16. //Property ' Cash.Program.readDouble (String) '
  17. Printfotherthing ("static method is called."); The //static method can be called directly
  18. Program program = new program (); //Non-static method requires an object to be created first
  19. //Then call with Object
  20. Program.printsomething ("Non-static Menthod is called.");
  21. }
  22. public Void Run ()
  23. {
  24. Double dailyrate = readdouble ("Please enter your rate:");
  25. int workDay = readInt ("Please enter your days of working:");
  26. Writefee (Calculate (dailyrate, WorkDay));
  27. }
  28. private void Writefee (double p)
  29. {
  30. Console.WriteLine ("The consultant ' s fee is: {0}", p * 1.6);
  31. }
  32. private Double calculate (double dailyrate, int workDay)
  33. {
  34. return dailyrate * WORKDAY;
  35. }
  36. private int readInt (string p)
  37. {
  38. Console.WriteLine (P);
  39. string a = Console.ReadLine (); //Read the input characters (ReadLine read
  40. //characters are of type string)
  41. return Int.                    Parse (a); //Convert a to int type, and return
  42. }
  43. private Double readdouble (string p)
  44. {
  45. Console.WriteLine (P);
  46. String line = Console.ReadLine ();
  47. return Double.  Parse (line);
  48. }
  49. private void printsomething (string a) //Non-static method
  50. {
  51. Console.WriteLine (a);
  52. }
  53. private static void printfotherthing (string a) //static method
  54. {
  55. Console.WriteLine (a);
  56. }
  57. }
  58. }

C # static method calls a non-static method

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.