定义一个雇员类:

namespace StaticFieldTest1
{
class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Salary { get; set; } //
public static int NextId;
public Employee(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
Id = NextId;
NextId++;
}
}
}

使用雇员类:

using System;

namespace StaticFieldTest1
{
class Program
{
static void Main(string[] args)
{
Employee.NextId = 100000; Employee employee1 = new Employee("Inigo", "Montoya");
Employee employee2 = new Employee("Princess", "Buttercup"); Console.WriteLine("{0} {1} ({2})", employee1.FirstName, employee1.LastName, employee1.Id);
Console.WriteLine("{1} {1} ({2})", employee2.FirstName, employee2.LastName, employee2.Id); Console.WriteLine("NextId = {0}", Employee.NextId);
Console.ReadKey(); }
}
}

输出:

Inigo Montoya (100000)
Buttercup Buttercup (100001)
NextId = 100002

类的静态(Static)成员——字段的更多相关文章

  1. PHP类的静态(static)方法和静态(static)变量使用介绍

    PHP类的静态(static)方法和静态(static)变量使用介绍,学习php的朋友可以看下     在php中,访问类的方法/变量有两种方法: 1. 创建对象$object = new Class ...

  2. C++ 类中的static成员的初始化和特点

    C++ 类中的static成员的初始化和特点 #include <iostream> using namespace std; class Test { public: Test() : ...

  3. 类模板的static成员

    下列代码可以通过编译吗?如何修改使其通过编译? template <class T> struct sum {   static void foo(T op1 , T op2){    c ...

  4. C++学习笔记(3)----类模板的static成员

    与任何其他类相同,类模板可以声明 static 成员: template <typename T> class Foo { public: static std::size_t count ...

  5. cc31a_demo--CppPrimer_静态成员与继承-在派生类中访问基类中的static成员的方法

    //*基类中的static成员,在整个继承层次中只有一个实例 //*在派生类中访问基类中的static成员的方法 //1.基类名::成员名 //2.子类名::成员名 //3.对象.成员名 //4.指针 ...

  6. c++:类中的static成员

    首先静态成员可以是public的,也可以是private的,只需在一般的变量.函数声明语句前加上static关键字即可声明一个static变量. 类中的静态成员存在与任何对象之外,所有该类对象的共享一 ...

  7. [转]C++ 类中的static成员的初始化和特点

    在C++的类中有些成员变量初始化和一般数据类型的成员变量有所不同.以下测试编译环境为: ➜ g++ -v Using built-in specs. COLLECT_GCC=g++ Target: x ...

  8. 【很变态】PHP类实例化对象竟然可以访问类的“静态(static)方法”!!!

    之前发现一个PHP的变态问题:PHP中静态(static)调用非静态方法详解 这次看了下 ThinkPHP 的源码 function.inc.php ,里面有个函数: /** * 取得对象实例 支持调 ...

  9. 类模版的static成员

    类模版中声明static成员 template <class T> class Foo { public: static size_t count() { ++ctr; cout < ...

随机推荐

  1. Async Return Types

    Async methods have three possible return types: Task<TResult>, Task, and void. The Task<TRe ...

  2. POJ3169--Layout(SPFA+差分系统)

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  3. POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25225 ...

  4. sdut 3916

    这道题就是二分枚举加贪心,小蓝书上一开始就讲的,但是我给忘了,很难受 #include <iostream> #include <cstdio> #include <cs ...

  5. JVM的參數

    博客:https://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html#CMSInitiatingOccupancyFraction_v ...

  6. 压力测试 mac ab

    apache ab:http://blog.chinaunix.net/uid-20382003-id-3032167.html 简单用法: ab -n 3000 -c 3000 http://www ...

  7. FNDLOAD使用大全

    FNDLOAD使用大全   Syntax FNDLOAD [username/password] 0 Y [mode] [configuration file] [target data file] ...

  8. Delphi中break,exit,abort跳出循环的比较

    http://www.delphitop.com/html/hanshu/104.html Delphi中break,exit,abort跳出循环的比较 exit: 退出函数体abort: 遇到异常, ...

  9. python--Websocket实现, 加密 sha1,base64

    需要用到gevent-websocket包,这里我们用下图这个 一.websocket简单实现 ep1.py from geventwebsocket.handler import WebSocket ...

  10. [BZOJ2738]矩阵乘法(整体二分+二维树状数组)

    整体二分+二维树状数组. 好题啊!写了一个来小时. 一看这道题,主席树不会搞,只能用离线的做法了. 整体二分真是个好东西,啥都可以搞,尤其是区间第 \(k\) 大这种东西. 我们二分答案,然后用二维树 ...