子类是否从父类继承私有成员? [英] Is subclass inherits private members from parent class?

查看:196
本文介绍了子类是否从父类继承私有成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个与OOP继承对象相关的问题:

I have two questions related to OOP Inheritance object :


  1. 第一个是根据以下代码:

  1. The first one is that according to the below code:

class Test {

private $name = "Youhana";

    function getPrivate(){
        echo   $this->name ;
    }

}

Class Test2 extends Test {

}
$obj2 = new Test2();
$obj2->getPrivate();


结果将是=> Youhana,但是这是如何工作的,尽管继承意味着成员从父级克隆到子级,所以如果这是正确的,子类中的代码必须类似于以下逻辑:

The result will be => Youhana, but how does this work, despite the inheritance means that members are cloned from the parent to child, so if this is correct, the code in the child class must be like the below logic:

 Class Test2 {// simple imagination to the Test2 after extends Test CLass
      function getPrivate(){ //which must return null not the value of the private member.
        echo   $this->name ;
      }
}

参考:手册


  1. 第二个问题是考虑以下代码:

  1. Second question is that consider the below code:

 Class Ex1{

    function checkClassName(){
        var_dump(__CLASS__); 
    }
}

Class Ex2 extends Ex1{

}


$obj2 = new Ex2();

$obj2->checkClassName();//how this will return EX1 although we invoked this function from the second object which after the inheritance will have a clone of public and protected members of the parent class?


虽然我们调用了这个,但这将如何返回EX1来自第二个对象的函数,在继承之后将具有父类的公共成员和受保护成员的克隆?

How this will return EX1 although we invoked this function from the second object which after the inheritance will have a clone of public and protected members of the parent class?

推荐答案

当你从超类扩展,没有任何东西被克隆。这是看错的方法。该类基本上是用于创建实例的模板。

When you extend from a superclass, nothing gets "cloned". That's the wrong way to look at this. The class is basically a template for creating an instance.

超类中的 private 属性仅对于已在该超类中定义的方法。这就是它被称为可见性的原因。

The private attributes in the superclass are visible only to the methods, that have been defined in that superclass. That's why it is called "visibility".

创建子类时, private 属性仍然存在。它们只是在子类中定义的方法中看不到。

The private attributes are still there, when you create a child class. They are just not visible from the methods, that have been defined in the child class.



class Foo {
    private $x = 1;        
    public function check() {
        return $this->x;
    }
}

class Bar extends Foo {
    public $x = 2;
}

class Buz extends Foo {
    private $x = 3;
    public function check() {
        return $this->x;
    }
}

$n = new Bar;
var_dump($n); /*
 object(Bar)#1 (2) {
   ["x"]=>
   int(2)
   ["x":"Foo":private]=>
   int(1)
 } */
var_dump($n->check()); // int(1)

$k = new Buz;
var_dump($k->check()); // int(3)

https://3v4l.org/KkTD2

当你扩展一个类时,你正在创建一个专门的子类型 案例仍然与原始超类兼容。当 private extend 一起使用时,点是定义了不受任何更改的部分行为。

When you extend a class, you are are creating a specialized subtype case, which should be still compatible with the original superclass. The point of private, when used with extend, is to defined the parts, that will not be subject to any changes in behavior.

至于 __ CLASS __ ,它是一个神奇常数(暗示,实际上并不是常数) -ish),这取决于它被访问的上下文。它将为您提供类的名称,然后在其中写入 __ CLASS __

As for __CLASS__, it is a "magic constant" (implying, that is not actually constant-ish), that being determined by the context in which it gets accessed. It will give you the name of class, inside of which it then __CLASS__ was written.

虽然,我不知道你在尝试用它做什么,因为你不应该在生产代码中。

Though, I have no idea, what you were trying to do with it, since you should not it in production code.

这篇关于子类是否从父类继承私有成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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