Interface implementation question.

agolangf · · 558 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, I&#39;m an aspiring gopher learner trying to wrap my head around interfaces and what implements what. Here&#39;s some code that I wrote to test things out: <a href="https://play.golang.org/p/B0Eew8SvKZL">code</a> And here&#39;s my question:</p> <p>I define two structs. One is &#34;person&#34;. And one is &#34;chemist&#34; which contains &#34;human&#34;. (presuming that all chemists are persons!) Then I create a &#34;human&#34; interface that only has one function &#34;Greeting()&#34; which is declared separately. I also create a &#34;scientist&#34; interface that contains only one function &#34;sciGreeting()&#34;. And then I declare that &#34;sciGreeting()&#34; right after.</p> <p>Later, in the main() I initialize the person variable and assign it to &#34;u&#34; (to be human), following by creating a &#34;v&#34; variable and initializing it with the &#34;u&#34; and another string (to be a chemist). This is where it gets peculiar.</p> <p>I understand that </p> <pre><code>Greeting() </code></pre> <p>implements human because the function name is the only part of the </p> <pre><code>human </code></pre> <p>interface and has a (i *person) as a receiver. And so</p> <pre><code>person </code></pre> <p>or </p> <pre><code>u </code></pre> <p>has access to the greeting. </p> <p>What I can&#39;t understand is why </p> <pre><code>v </code></pre> <p>has access to both </p> <pre><code>sciGreeting() </code></pre> <p>AND </p> <pre><code>Greeting() </code></pre> <p>Meanwhile, &#34;u&#34; doesn&#39;t!</p> <p>I mean, the code is working as intended. A human scientist has access to both human and scientist greetings. But I just can&#39;t see why!!! There&#39;s no point of intersection between the two interfaces or greeting functions. </p> <p>I can only imagine that it&#39;s because &#34;person&#34; struct implements &#34;human&#34; by having Greeting() the pointer to the &#34;person&#34; struct as its receiver? So, when I create another variable, &#34;v&#34;, that contains that struct, &#34;v&#34; (a chemist struct) also inherits the access to Greeting() from &#34;u&#34;, the &#34;person&#34; struct (human interface)? So, the &#34;chemist&#34; struct then implements the &#34;scientist&#34; interface through the &#34;sciGreeting()&#34; function that has the &#34;*chemist&#34; receiver in it? </p> <p>Am I right? </p> <hr/>**评论:**<br/><br/>GopherFromHell: <pre><p>The method Greeting is declared on a pointer receiver and in your chemist struct is declaring a value.</p> <p>take a look <a href="https://play.golang.org/p/0NrhQ_Bn0ed" rel="nofollow">at this example</a>. The documentation of the language on <a href="https://golang.org/ref/spec#Struct_types" rel="nofollow">structs</a> might also be useful to understand embedded fields (and how they interact with interfaces)</p></pre>xiegeo: <pre><p><a href="https://play.golang.org/p/04Lud8ExLVW" rel="nofollow">https://play.golang.org/p/04Lud8ExLVW</a></p> <p>See that last line that I added. It does not compile so a person is not a human.</p> <p>You can use this way to test if a struct correctly implements an interface. This is what interface is for, to make sure certain rules are followed.</p></pre>govision: <pre><p>Yeah it&#39;s because u only has one of the methods and is embedded. It isn&#39;t I guess you can say top level. You are expecting the compiler to kinda do what js does which is run 12 times and make the best guess inside a nested statement. Almost like js eval(). It&#39;s evil for a reason lol but easily misunderstood.</p> <p><a href="https://play.golang.org/p/IA09xBX7dYF" rel="nofollow">https://play.golang.org/p/IA09xBX7dYF</a></p></pre>ODAwake: <pre><p>Ah, that is fantastic, thank you!</p> <p>So, I see that this way structs can &#34;inherit&#34; the properties of structs inside them and have the access to all the functions that are associated with them. I&#39;m a fairly new programmer, so I am not really familiar how inheritances work in other languages. This seems pretty logical.</p> <p>And this makes sense in my code now too. I had </p> <pre><code>person </code></pre> <p>embedded in </p> <pre><code>chemist </code></pre> <p>So chemist was the higher level. And when I created a separate interface with a caller for chemist, it was available only to the top level struct, and not the bottom level, original &#34;human&#34; struct. </p> <p>Cool beans, man! Thank you!</p></pre>govision: <pre><p>Yeah I got a few links on interfaces here. <a href="https://docs.google.com/document/d/1Zb9GCWPKeEJ4Dyn2TkT-O3wJ8AFc-IMxZzTugNCjr-8/edit?usp=drivesdk" rel="nofollow">https://docs.google.com/document/d/1Zb9GCWPKeEJ4Dyn2TkT-O3wJ8AFc-IMxZzTugNCjr-8/edit?usp=drivesdk</a></p> <p>But to be honest, I wouldn&#39;t over use them cause they really are specialized glue for a &#34;reusable&#34; package. It&#39;s not really meant to just carry over methods. </p> <p>I&#39;m kinda trying to write an article on types....ish but it&#39;s really dry cause I didn&#39;t add context but the point was an appreciation of the design of the types not so much high level stuff. But it helps you understand packages instead of just using them.</p> <p><a href="https://medium.com/@d35da8f0b148/2ba61290a5cf?sk=" rel="nofollow">https://medium.com/@d35da8f0b148/2ba61290a5cf?sk=</a></p> <p>And</p> <p><a href="https://medium.com/@d35da8f0b148/30fa200bef8f?sk=" rel="nofollow">https://medium.com/@d35da8f0b148/30fa200bef8f?sk=</a></p> <p>And thank you for the gold!!!!</p></pre>jerf: <pre><p>Can you put some code capturing your question clearly up on <a href="https://play.golang.org/" rel="nofollow">the Go playground</a>? It would help answer this.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

558 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传