c#基本语法解释备忘


using System; //it means it's going to use System's method, here is Console.WriteLine(), namespace RectangleApplication   //namespace { class Rectangle          //class { // 成员变量 double length;        //double is a type of data structures double width; public void Acceptdetails() { length = 4.5; width = 3.5; } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); //print length. Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } class ExecuteRectangle { static void Main(string[] args) //static: no need to work in a object reference, void:means no return, Main:this is the main function ,one class one Main function only
                         //string[] means the type of parameter , args means you can decide if you need to fill it in.  
{ Rectangle r = new Rectangle(); //must do it, because another class can only call method of the other class only if it's an object reference,
                            why? just assume this,you have a house design drawing,but you can not live in,you need to build a house and then you can live in it. r.Acceptdetails(); //call Acceptdetails() method of r object reference r.Display();       //call Display() method of r object reference Console.ReadLine();   //this will wait you press a button an then continue } } }
 

Here is a demo that i wrote.
using System;
namespace RectangleApplication
{
    class Rectangle
    {
        public string length = "wuxian"; // public means it's gettable by outside of class, relatively, private means it's only gettable within class;
   }
    static void Main(string[] args) //if you do not want make a 'instance' to run this method, static is a must.
        {
            Rectangle r = new Rectangle();
            string x = r.length;
            Console.WriteLine(x);
        }
    }
}
     

posted on 2018-12-21 20:23  techguybright  阅读(36)  评论(0)    收藏  举报

导航