Example 3 Mapping $f: X \rightarrow X$. If for all $x \in X, f(f(x))=f(x)$ holds, then it is called idempotent. Let $|X|=n$, try to find the number of idempotent mappings.
Let $|f(X)|=k$, then $f(X)$ has $C_{n}^{k}$ choices. For any element $x$ in $f(X)$, it is obvious that $f(x)=x$. And for each element in $X-f(X)$, its image has $k$ choices. Therefore, there are a total of idempotent mappings $\sum_{k=1}^{n} C_{n}^{k} k^{n-k}$.