Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Python如何将表达式分配给对象属性?_Php_Python_Oop - Fatal编程技术网

Php Python如何将表达式分配给对象属性?

Php Python如何将表达式分配给对象属性?,php,python,oop,Php,Python,Oop,我使用PHP已经有一段时间了,刚刚开始使用Python。Python中有一个我在学习时遇到的特性 Python中的 class A: #some class Properties class B: a = A() # assiging an expression to the class Property is possible with python. PHP中的 class A{ } class B{ $a = new A(); // PHP does not

我使用PHP已经有一段时间了,刚刚开始使用Python。Python中有一个我在学习时遇到的特性

Python中的

class A:
  #some class Properties

class B:
    a = A()  # assiging an expression to the class Property is possible with python.
PHP中的

class A{

}

class B{
  $a = new A();   // PHP does not allow me to do this.

  // I need to do this instead.
  function  __construct(){
    $this->a = new A();
  }
}

我想知道为什么。python如何以不同的方式执行代码,以及是否有任何方法可以用PHP实现这一点。

我相信这是语言特定的事情。从

类名:
.
.
.
类定义(如函数定义(def语句))必须在生效之前执行。(你 可以想象将类定义放在if的分支中 语句或函数内部。)


如您所见,这些表达式是经过计算的,您甚至可以使用“if”语句

我相信这是语言特有的东西。从

类名:
.
.
.
类定义(如函数定义(def语句))必须在生效之前执行。(你 可以想象将类定义放在if的分支中 语句或函数内部。)


如您所见,这些表达式是经过计算的,您甚至可以使用“if”语句

Python中的

class A:
  #some class Properties

class B:
    a = A()  # assiging an expression to the class Property is possible with python.
在类定义中声明的变量

class A:
  #some class Properties

class B:
    a = A()  # assigning to the class Property
    # class properties are shared across all instances of class B 
    # this is a static property
在类构造函数中声明的变量

class A:
  #some class Properties

class B:
    def __init__(self):
        self.a = A()  # assigning to the object Property
        # this property is private to this object
        # this is a instance property
更多python阅读资料

PHP中的

class A{

}

class B{
  $a = new A();   // PHP does not allow me to do this.

  // I need to do this instead.
  function  __construct(){
    $this->a = new A();
  }
}
inPHP使用静态变量的概念在对象之间共享实例


希望这能澄清类属性和对象属性。

在Python中

class A:
  #some class Properties

class B:
    a = A()  # assiging an expression to the class Property is possible with python.
在类定义中声明的变量

class A:
  #some class Properties

class B:
    a = A()  # assigning to the class Property
    # class properties are shared across all instances of class B 
    # this is a static property
在类构造函数中声明的变量

class A:
  #some class Properties

class B:
    def __init__(self):
        self.a = A()  # assigning to the object Property
        # this property is private to this object
        # this is a instance property
更多python阅读资料

PHP中的

class A{

}

class B{
  $a = new A();   // PHP does not allow me to do this.

  // I need to do this instead.
  function  __construct(){
    $this->a = new A();
  }
}
inPHP使用静态变量的概念在对象之间共享实例


希望这能澄清关于类属性和对象属性的问题。

对“为什么”的回答是“因为它们是完全不同的语言。”如果您已经使用PHP一段时间了,您可能已经知道正确的PHP方法。用另一种语言编写一种语言永远不会有好的结果。在python解释该语言的情况下,它将基于级别范围,尝试分析在找到函数范围时必须初始化的内容。这有点类似于PHP中的类变量。请看,“为什么”的答案是“因为它们是完全不同的语言。”如果您已经使用PHP一段时间了,您可能已经知道正确的PHP方法。用另一种语言编写一种语言永远不会有好的结果。在python解释该语言的情况下,它将基于级别范围,尝试分析在找到函数范围时必须初始化的内容。这有点类似于PHP中的类变量。看见