php “公共”和“公共静态”的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5527120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The difference between "public" and "public static"?
提问by Alex
What does static
mean?
什么static
意思?
I know public
means that it can be accessed from outside the class, and private
only from inside the class…
我知道这public
意味着它可以从类外部访问,并且private
只能从类内部访问......
采纳答案by BillThor
Static means that it can be accessed without instantiating a class. This is good for constants.
静态意味着可以在不实例化类的情况下访问它。这对常量有好处。
Static methods need to have no effect on the state of the object. They can have local variables in addition to the parameters.
静态方法不需要对对象的状态产生影响。除了参数之外,它们还可以有局部变量。
回答by Cristian David
public: Public declared items can be accessed everywhere.
public:可以在任何地方访问公共声明的项目。
protected: Protected limits access to inherited and parent classes (and to the class that defines the item).
protected: Protected 限制对继承类和父类(以及定义项的类)的访问。
private: Private limits visibility only to the class that defines the item.
private: Private 将可见性限制为仅定义项目的类。
static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
static:静态变量仅存在于局部函数作用域中,但当程序执行离开该作用域时,它不会丢失其值。
final: Final keywords prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
final:Final 关键字通过在定义前加上 final 来防止子类覆盖方法。如果类本身被定义为 final,则它不能被扩展。
Beside of PHP:
除了 PHP:
transient: A transient variable is a variable that may not be serialized.
瞬态:瞬态变量是一个不能被序列化的变量。
volatile: A variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile won't be optimized by the compiler because their value can change at anytime.
volatile:一个可能被多个线程同时修改的变量应该被声明为 volatile。声明为 volatile 的变量不会被编译器优化,因为它们的值可以随时更改。
回答by bensiu
From http://php.net/manual/en/language.oop5.static.php
来自http://php.net/manual/en/language.oop5.static.php
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
将类属性或方法声明为静态使它们无需实例化即可访问。声明为静态的属性不能用实例化的类对象访问(尽管静态方法可以)。
回答by zloctb
An example: when use the static
keyword, we cannot use $this
一个例子:当使用static
关键字时,我们不能使用$this
class Foo {
private $foo='private';
private function priv_func() {
echo 'priv_method';
}
public static function get() {
echo $this->foo;
$this->priv_func();
}
}
$obj = new Foo();
$obj->get();
Fatal error: Using $this when not in object context in (…)
致命错误:在 (...) 中不在对象上下文中时使用 $this
回答by Usha Bhat
Example:
例子:
public class Methods_Test1
{
public static void Display(String Name)
{
System.out.println("Hello There " + Name);
System.out.println("I am from Display method");
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter name");
String name = sc.next();
Obj.Display(name);
}
The public static void Display(String name) method accessible as static method within its own class which can be accessed without creating object of the class where as the same method behaves as public for the outside classes which can be accessed by creating object.
public static void Display(String name) 方法可在其自己的类中作为静态方法访问,无需创建类的对象即可访问,而对于可以通过创建对象访问的外部类,相同的方法表现为公共。