C#基础-面向对象/static/this

C#基础-面向对象
基本跟java一样,区别:
1.内部成员,包内可见用:internal
2.派生类(继承):class Class2:Class1{}
3.总结this与static的用法

This
this的用法再次小结一下:
例如:
构造函数
public Employee(string name, decimal salary)
{
      this.name = name;//denote this construction method.
      this.salary = salary;
}

method(this)// denote this current instance of class

Sample of  static
 1 using System;
 2 
 3 class Employee
 4 {
 5     public static decimal salary;//static member
 6     public string name;//non-static member
 7     public static void setSalary(decimal B)//static method
 8     {
 9         salary = B;//right,equivalence declaration:Employee.Salary = B;
10     }
11     public void setName(string N)//non-static memeber
12     {
13         name = N;//right,equivalence declaration:this.name = N;
14     }
15 }
16 
17 public class Sample
18 {
19     public static void Main()
20     {
21         Employee.salary = 8000.0m;//right,static member can access through name of class
22         Employee.setSalary(8000.0m);//right,static method can access through name of class
23         Employee  e = new Employee();
24         e.name = "Einstein"//right, non-static memeber must access through the instance of class
25         e.setName("Einstein");//right,non-static method must access through the instence of class
26         Console.WriteLine("name of stuff is:{0}\nstuff salary:{1}",e.name,Employee.salary);
27     }    
28 }

posted on 2007-05-24 01:40  nut  阅读(300)  评论(0编辑  收藏  举报

导航