The magic go language (Object-Oriented) and go language object-oriented

Source: Internet
Author: User

The magic go language (Object-Oriented) and go language object-oriented


[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]


Anyone who has had C ++ language learning experience knows that Object-oriented has three basic features: encapsulation, inheritance, and polymorphism. Encapsulation means that the running data and functions are bound together, and C ++ mainly uses the this pointer. Inheritance means that classes can inherit attributes and functions from each other; polymorphism is mainly to use a unified interface to process general logic. Each class only needs to implement its own callback function according to the interface.


The go language, as the aggregator, naturally does not do anything in Object-Oriented aspects. Compared with object-oriented languages such as C ++, java, and C #, the object-oriented language is simpler and easier to understand. Below, we may use three simple examples to illustrate what object orientation is in the go language.


Encapsulation features,

package mainimport "fmt"type data struct {val int}func (p_data* data)set(num int) {p_data.val = num}func (p_data* data)show() {fmt.Println(p_data.val)}func main() {p_data := &data{4}p_data.set(5)p_data.show()}
Inheritance features,

package mainimport "fmt"type parent struct {val int}type child struct {parentnum int}func main() {var c childc = child{parent{1}, 2}fmt.Println(c.num)fmt.Println(c.val)}
Polymorphism,

package mainimport "fmt"type act interface {write()}type xiaoming struct {}type xiaofang struct {}func (xm *xiaoming) write() {fmt.Println("xiaoming write")}func (xf *xiaofang) write() {fmt.Println("xiaofang write")}func main() {var w act;xm := xiaoming{}xf := xiaofang{}w = &xmw.write()w = &xfw.write()}


In terms of object-oriented, The go language is more concise and straightforward. On the one hand, this comes from a thorough understanding of object-oriented with the language designer; on the other hand, I think it also comes from the google designer's summary of past experiences. I personally think these features are very interesting. If you are interested, you can try them yourself.






Is the go language object-oriented or process-oriented?

There is no obvious tendency in the go language. It may be because the designers of the language are cool people. They are more inclined to let Programmers think about how to use it. Maybe it features flexibility. You can use it to implement object-oriented, but it does not support object-oriented semantics.

What is the difference between advanced languages and object-oriented languages? Process-oriented language refers to the language. Is it a high-level language?

By category, computer programming languages can be classified into advanced and non-advanced languages, or object-oriented and non-object-oriented languages (such as object-oriented and process-oriented). Of course, other types are required.
Therefore, the advanced language and object-oriented language are not comparable and belong to different classifications. It's like being tall, fat, and thin. I cannot answer the difference between high and thin.
The object-oriented language is a high-level language.
Process-oriented language is the language in which the language code is compiled step by step according to the algorithm process. For example, to span a column, we should first span the first column, then the second column, and one by one. This is the process. The process-oriented language is similar to this. When you span a column, we should first leave or right foot, this is also a process. To use a process-oriented language to write a cross-bar program, you must write the program step by step.
This is a process-oriented language.
As for whether process-oriented language is a high-level language, it seems that this person is very thin and he is not tall? Not necessarily. Process-oriented languages include low-level (such as assembly) and advanced (such as C ).
Object-oriented language? When I write a cross-bar program, I write an object called a person. This person has a function called a cross-bar program. When I use it, I will say to this person: You, show me the dashboard. As for how you interact, you can.
In my opinion, object-oriented language is encapsulated. In essence, the machine code 0 and 1 are still being executed. But it is more in line with our abstract concept of the real world. For example, the cross-column object is a person. The cross-bar function is still procedural. Of course, you can write different types of cross-bar methods, which are more flexible than simply process-oriented cross-bar.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.