静态函数和普通函数之间的区别?并给出示例. [英] difference between static function and normal function?and give Example.

查看:70
本文介绍了静态函数和普通函数之间的区别?并给出示例.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

静态函数和普通函数之间的区别?并给出示例.

Difference between static function and normal function?and give Example.

推荐答案

在C语言中,静态函数仅在同一文件中可见.就其他文件而言,它根本不存在.
您不能为此举一个例子,因为我们这里没有文件!

如果您的意思是C#,那么static具有不同的含义.
对于C#,静态表示方法的单个实例,与该类的任何实例都不相关.您可以通过类名而不是类实例来访问ststic方法:
In C, a static function is only visible within the same file. As far as other files are concerned it does not exists at all.
You can''t give an example for that because we don''t have files here!

If you meant C#, then static has a different meaning.
For C#, static means that there is a single instance of the method, which is not related to any instance of the class. You access a ststic method via teh class name, instead of via a class instance:
class MyClass
   {
   static void sm(){}
   void nm(){}
   ...
   }

MyClass instance = new MyClass();
instance.nm();  // Valid
MyClass.nm();   // Illegal - needs an instance
MyClass.sm();   // Valid



考虑一下汽车:您可以问一个问题:汽车有多少个轮子?"因为所有汽车都有四个车轮. "HowManyWheels"是Car类的静态方法.
但是您不能问汽车是什么颜色?"因为您需要指定您要谈论的是哪辆车:我的车是什么颜色?"还是那辆车是什么颜色?"



Think about cars: you can ask the question "how many wheels has a car?" because all cars have four wheels. "HowManyWheels" is a static method of the Car class.
But you can''t ask "what colour is a car?" because you need to specify which car you are talking about: "what colour is my car?" or "what colour is that car?"


在C(而不是C ++)的上下文中,静态函数仅在其自己的源文件中是已知的.我们说,它的作用域"仅限于源文件.

相比之下,正常"功能可以从其他源文件中的代码访问,只要在头文件(或其他位置)中有一个声明使该文件在该源文件中为人所知即可.链接器负责插入此类函数的正确地址.
In the context of C (and not C++) a static function is only known within its own source file. We say, its "scope" is limited to the source file.

A "normal" function, in contrast, can be accessed from code in other source file, given that there is a declaration in a header file (or other place) that makes them known within that source file. The linker is responsible for inserting the correct addresses of such functions.


这篇关于静态函数和普通函数之间的区别?并给出示例.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆