简体   繁体   English

C#和PhP中:: ::运算符的含义

[英]Meaning of :: Operator in C# and PhP

在我的工作中,我遇到了诸如PHP中的global ::(C#)或G:Loading($ value)之类的语法,它们是什么意思?

In C# :: is the namespace alias qualifier operator, and global indicates the global namespace. 在C#中, ::是名称空间别名限定符运算符,而global表示全局名称空间。 It is necessary if you have overlapping classes and namespaces. 如果类和名称空间重叠,则很有必要。

For example, if you have class Foo.Bar and Baz.Foo.Bar then the reference Foo.Bar may be an ambiguous reference as Foo.Bar exists in both the global namespace and the Baz namespace. 例如,如果您具有类Foo.BarBaz.Foo.Bar则引用Foo.Bar可能是模棱两可的引用,因为Foo.Bar存在于全局名称空间和Baz名称空间中。 However, by saying global::Foo.Bar you explicitly scope the reference to the global namespace and thus it is no longer ambiguous. 但是,通过说出global::Foo.Bar您可以将引用显式地作用于全局名称空间,因此不再是歧义的。

More info here . 更多信息在这里

在PHP中:::用于访问静态成员或方法,请参见http://www.php.net/manual/zh/language.oop5.static.php

If C#, :: is called the Namespace Alias Qualifier. 如果为C#,则::称为命名空间别名限定符。

It qualifies which namespace you are in exactly should there be ambiguities. 如果存在歧义,它将完全限定您位于哪个名称空间。 For example: 例如:

using MyNameSpace;

...

global::System.Data 

differentiates the System.Data namespace from MyNameSpace.System.Data . System.Data命名空间与MyNameSpace.System.Data

Have a look at C# reference on the :: operator: 看看::运算符上的C#参考:

http://msdn.microsoft.com/en-us/library/htccxtad.aspx http://msdn.microsoft.com/en-us/library/htccxtad.aspx

and more helpfully, the MSDN article demonstrating its use 更有用的是,MSDN文章演示了其用法

http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx http://msdn.microsoft.com/zh-CN/library/c3ay4x3d.aspx

Consider the following example: 考虑以下示例:

namespace  A { 
    public class MyClass { }
} 

namespace namespaceB
{
    public class A
    {
        public class MyClass { }
    }

    public class OtherClass
    {
        public A.MyClass MyAClass;
    }
}

Explanation: 说明:

In the example above, the compiler would always resolve the MyAClass to the type namespaceB.A.MyClass. 在上面的示例中,编译器将始终将MyAClass解析为名称空间B.A.MyClass类型。

If you wanted it to be A.MyClass, there was no way to do it until C# 2.0. 如果您希望它成为A.MyClass,那么直到C#2.0都无法实现。

In C# 2.0, you would use: public class MyOtherClass 在C#2.0中,您将使用:公共类MyOtherClass

{ public global::A.MyClass yAClass; }

To indicate that you shouldn't use the local namespace scope, but rather, the root namespace. 为了表明您不应该使用本地名称空间范围,而应该使用根名称空间。

in Class based object oriented programming languages (like C++, Java, PHP, ...) there are properties (also called fields) which are the data and are simply variables or constants, and methods which are the functionality of the class and are actually functions (routins). 在基于类的面向对象的编程语言(如C ++,Java,PHP等)中,有一些属性(也称为字段),它们是数据,只是变量或常量,而方法是类的功能,实际上是功能(常规)。 each class can be instanciated as objects. 每个类都可以实例化为对象。 each object has all data and functionality of the base class at the beginning, but the data can be changed and be different from one object, to another. 每个对象在开始时都具有基类的所有数据和功能,但是数据可以更改,并且从一个对象到另一个对象都可以不同。 there are 2 kinds of properties and 2 kinds of methods. 有2种属性和2种方法。 some properties are shared between all objects of a class. 在类的所有对象之间共享某些属性。 this means that if you change this data of the class, the same data of all the instanciated objects will be changed to the neew value. 这意味着,如果您更改该类的数据,则所有实例化对象的相同数据都将更改为neew值。 this kind of properties are called Class properties, or Static properties (because when you are defining these variables, in most languages you will use the static keyword). 这种属性称为类属性或静态属性(由于定义这些变量时,在大多数语言中,您将使用static关键字)。 some other properties are specific for each object. 其他属性对于每个对象都是特定的。 changing the data of on object, does not affect the data of other objects. 更改on对象的数据,不会影响其他对象的数据。 these are called object properties or instance properties. 这些称为对象属性或实例属性。 some methods are called from an object, there are called object methods. 从对象调用某些方法,有一些称为对象方法。 these are object methods and to use them, you need to first instanciate a class and use the object. 这些是对象方法,要使用它们,您需要首先实例化一个类并使用该对象。 some other methods are called by using the class itself. 通过使用类本身可以调用其他一些方法。 these are class methods (or static methods) if you want one of these functions, there is no need for an object. 这些是类方法(或静态方法),如果需要这些功能之一,则不需要对象。 to use object properties/methods, in PHP (like C++) you should use the -> operator: 要使用对象属性/方法,在PHP(如C ++)中,应使用->运算符:

object->method();
object->property;

to use class properties/methods in PHP (like C++) you should use the :: operator: 要在PHP(如C ++)中使用类属性/方法,应使用::运算符:

class::method();
class::property;

in languages like Jave, the operator . 用运算符Jave之类的语言表示。 is used for all of properties/methods: 用于所有属性/方法:

object.method();
class.property;

suppose you have a Circle class. 假设您有一个Circle类。 this class defines some data like radius, and center cordinates, and a constant for PI value. 此类定义一些数据,例如半径和中心坐标,以及PI值的常量。 also has a method to calculate the area of the circle. 也有一种方法来计算圆的面积。

here is some PHP code to make it clear: 这是一些PHP代码以使其更清楚:

<?php
class Circle {
    public $radius;
    public $centerX;
    public $centerY;
    public static $PI = 3.1415;
    public function __construct($r,$x,$y) {
        $this->radius = $r;
        $this->centerX = $x;
        $this->centerY = $y;
    }
    public function getArea() {
        return self::$PI * $this->radius * $this->radius;
    }
}
$circle1 = new Circle(10,0,0);
echo $circle1->getArea() . "<br>\n";
$circle2 = new Circle(15,10,12);
echo $circle2->getArea() . "<br>\n";
Circle::$PI = 3.14;
echo $circle1->getArea() . "<br>\n";
echo $circle2->getArea() . "<br>\n";
?>

in this example we define a circle class which has object properties (radius, centerX, CenterY), object methods (getArea()), and a Class property (PI). 在此示例中,我们定义了一个圆形类,该类具有对象属性(半径,centerX,CenterY),对象方法(getArea())和类属性(PI)。 by changing the PI static property, the area of both objects will be affected. 通过更改PI静态属性,两个对象的面积都会受到影响。

In C# the global:: operator is used to ensure that the following name is interpreted as a global name expression vs. a local one. 在C#中,global ::运算符用于确保将以下名称解释为全局名称表达式,而不是局部名称。 The main use for this is when developers have a nested namespace where the name is the same as a global namespace. 此方法的主要用途是当开发人员拥有嵌套名称空间时,该名称空间与全局名称空间相同。 Without the global:: operator there is no way to access the global version. 如果没有global ::运算符,则无法访问全局版本。

namespace Foo { 
  namespace System {
    class DateTime {} 
    class Example {
      object Example() { 
        return System.DateTime.Now; // Returns Foo.System.DateTime.Now
        return global::System.DateTime.Now; // Returns mscorlib,System.DateTime.Now
      }
    }
  }
}

在PHP中,它称为范围解析运算符(Paamayim Nekudotayim),并允许访问类中的成员或常量。

In C#, the :: operator is the namespace alias qualifier. 在C#中, ::操作符是名称空间别名限定符。 It is used to resolve identifiers to the appropriate namespace, especially in situations where ambiguities exist. 它用于将标识符解析为适当的名称空间,尤其是在存在歧义的情况下。

So suppose that you create a namespace alias via 因此,假设您通过创建一个名称空间别名

using IOAlias = System.IO;

and wish to declare an instance of the StreamReader class. 并希望声明StreamReader类的实例。 You could do this via 您可以通过

IOAlias::StreamReader sr = new StreamReader(path);

Further, you can resolve to the global namespace via 此外,您可以通过以下方式解析为全局名称空间

global::System.IO.StreamReader sr = new StreamReader(path);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM