2010/03/01

C# week5_2

23/02/2010

<static, non-static>

 
 

class student

{

public int i;

static public int j=5;

}

student s1 = new student();

student s2 = new student();

 
 

j=5: global

i=0

i=0

s1 s2

 
 

Static: initialize static value of your class

*static can't have any parameter.

 
 

class student

{

int id; object valuables or instance valuables

int age;

static string cname; class valuables

 
 

student(int i,j)

{

id=i;

age=j;

}

}

 
 

static student() can't have any parameter (error)

{

cname="GDIT";

}

*static object runs first ready. (Share = global)

 
 

  1. Compiler helps

    class xyz

    {

    static void Main() private (default private)

    {

    Console.WriteLine("Hello");

    }

    }

    class ABC

    {

    static void Main()

    {

    xyz x=new xyz(); It will work, because even though xyz is public, it helps coming from compiler.

    }

    }

     
     

  2. Compiler doesn't help

    class xyz

    {

    static void Main() private (default private)

    {

    Console.WriteLine("Hello");

    }

    xyz();

    }

    class ABC

    {

    static void Main()

    {

    xyz x=new xyz(); It will be error, because it has already initialized in the class, so it's public. (Compiler doesn't support)

    }

    }

     
     

     
     

    class ABC

    {

    public static void xyz()

    *it is required if this method is called outside this class.

    {

    Console.WriteLine("Hello");

    }

    }

    class one

    {

    static void Main()

    {

    ABC.xyz();

    }

    }

     
     

  3. Class program sample

    class Program

    {

    static void Main(string[] args)

    {

    student s = new student(1, 20, "xyz");

    student s1 = new student(2, 22, "abc");

    student s2 = new student(3, 23, "def");

    student s3 = new student(4, 24, "mno");

     
     

    Console.WriteLine("The student id is " + s.sid);

    Console.WriteLine("The student age is " + s.age);

    Console.WriteLine("The student name is " + s.name);

    Console.WriteLine("The student coursename is " + student.coursename);

     
     

    Console.WriteLine("The student id is " + s1.sid);

    Console.WriteLine("The student age is " + s1.age);

    Console.WriteLine("The student name is " + s1.name);

    Console.WriteLine("The student coursename is " + student.coursename);

     
     

    Console.WriteLine("The student id is " + s2.sid);

    Console.WriteLine("The student age is " + s2.age);

    Console.WriteLine("The student name is " + s2.name);

    Console.WriteLine("The student coursename is " + student.coursename);

     
     

    Console.WriteLine("The student id is " + s3.sid);

    Console.WriteLine("The student age is " + s3.age);

    Console.WriteLine("The student name is " + s3.name);

    Console.WriteLine("The student coursename is " + student.coursename);

     
     

    }

    }

    class student

    {

    public int sid;

    public int age;

    public string name;

    public static string coursename;

     
     

    public student(int i, int j, string s)

    {

    sid = i;

    age = j;

    name = s;

    }

     
     

    static student() //constructor don't need public. (It has already initialized, so we can call this class from outside.)

    {

    coursename = "GDIT";

    }

    }

     
     

0 件のコメント:

コメントを投稿

UA-9417263-1