gpt4 book ai didi

oop - 封装原理

转载 作者:行者123 更新时间:2023-12-01 10:16:40 26 4
gpt4 key购买 nike

有一些面向对象的工程原则表明“一个类应该只知道它作为参数的类的契约,或者它使用的任何内部契约”。

C++ 中的反例是:

Foo::bar( Baz* baz)
{
baz()->blargh()->pants()->soil(); // this is bad, Foo knows about blarghs and pants
}

这个原则有名字吗?另外,实际原理而不是我上面的解释会很高兴看到。

最佳答案

law of demeter 感谢 Jim Burger 说:

The Law of Demeter (LoD), or Principle of Least Knowledge, is a design guideline for developing software, particularly object-oriented programs. The guideline was invented at Northeastern University towards the end of 1987, and can be succinctly summarized as “Only talk to your immediate friends.” The fundamental notion is that a given object should assume as little as possible about the structure or properties of anything else (including its subcomponents).

关于oop - 封装原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/297589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com