Python,java,go the difference between languages!

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In addition to the Java Python go, the three languages underlying and grammatical differences, here is a personal understanding of only the object-oriented thinking. A simple example:

Describe people, Li Lei, Han Meimei, both of whom are good students.

The three languages of Javapythongo will be described in brief.

Java Ideas

People are abstract concepts that can be used to wash and cook a primate species that cannot be specified as specific things, but it also has some properties and functions such as sex, pee, and the like.

/** * 抽象出来的人 */abstract class Human {    protected String sex;    protected String name;    public void setSex(String sex) {        this.sex = sex;    }    public String getSex() {        return this.sex;    }    public void setName(String name) {        this.name = name;    }    public String getName() {        return this.name;    }    abstract void doPee(); // 抽象的方法}

Abstract classes are used here because the names are from parents, but the methods of urinating are different for men and women. Then there is the abstract concept of figurative man. Here is the fixed sex attribute and defines the way to pee.

/** * Figurative male */class Male extends Human {public Male () {this.sex = "male";}

    /**     * 实现的方法     */    public void doPee() {        System.out.println(this.name + " " + this.sex + "站着撒尿.");    }}/** * 具象的女性 */class Female extends Human {    public Female() {        this.sex = "女";    }    /**     * 实现的方法     */    public void doPee() {        System.out.println(this.name + " " + this.sex + "蹲着撒尿.");    }}

Now there are men and women, and then Li Lei and Han Meimei are going to torture us.

Male lilei = new Male();lilei.setName("李磊");System.out.println(lilei.getName() + " " + lilei.getSex() + " " + "出场");Female hanmeimei = new Female();hanmeimei.setName("韩梅梅");System.out.println(hanmeimei.getName() + " " + hanmeimei.getSex() + " " + "出场");lilei.doPee();hanmeimei.doPee();_________________________________________________output: 李磊 男 出场output: 韩梅梅 女 出场output: 李磊 男站着撒尿.output: 韩梅梅 女蹲着撒尿.

Li Lei and Han Meimei are good students, we define the interface of learning here, the interface here is, we have to die learning silly, how to learn to see yourself.

/** * 学习接口 */interface Study {    public abstract void learningEnglish();}

Above is stipulated by the Ministry of Education, Li Lei Han Meimei as a student must learn, men and women have to experience. To implement the learning interface.

 class Male extends Human implements Study {  .../** * Implemented interface */public void Learningenglish () {System.out.println (this.name + ": how Is you? ");}} /** * Female */class Female extends Human implements Study {..../** * Implemented interface */public void L.    Earningenglish () {System.out.println (THIS.name + ": I ' m fine, thank you!"); }}............lilei.dopee (); Hanmeimei.dopee (); Lilei.learningenglish (); Hanmeimei.learningenglish (); ____________ _____________________________________output: Li Lei: How is you?output: Han Meimei: I ' m fine, thank you!  

The idea of Java is roughly like this. Very rigorous, like an old scholar, 1 is 2.

This is all the Java code

Main.java

public class Main {public static void main (string[] args) {Male Lilei = new Male ();        Lilei.setname ("Li Lei");        System.out.println (Lilei.getname () + "" + lilei.getsex () + "" + "appearances");        Female Hanmeimei = new Female ();        Hanmeimei.setname ("Han Meimei");        System.out.println (Hanmeimei.getname () + "" + hanmeimei.getsex () + "" + "appearances");        Lilei.dopee ();        Hanmeimei.dopee ();        Lilei.learningenglish ();    Hanmeimei.learningenglish ();    }}/** * Abstract out of the person */abstract class Human {protected String sex;    protected String name;    public void Setsex (String sex) {this.sex = sex;    } public String Getsex () {return this.sex;    } public void SetName (String name) {this.name = name;    } public String GetName () {return this.name; } abstract void Dopee (); Abstract Method}/** * Learning interface */interface Study {public abstract void learningenglish ();} /** * Figurative male */class Male extends Human implements Study {publicMale () {this.sex = "male";    }/** * Implemented method */public void Dopee () {System.out.println (THIS.name + "" + This.sex + "standing pee.");    }/** * Implemented interface */public void Learningenglish () {System.out.println (this.name + ": How is You?");    }}/** * Figurative female */class Female extends Human implements Study {public Female () {this.sex = "female";    }/** * Implemented method */public void Dopee () {System.out.println (THIS.name + "" + This.sex + "squatting pee."); }/** * Implemented interface */public void Learningenglish () {System.out.println (THIS.name + ": I ' m fine, thank yo U! ");}}

Python Ideas

Python is not a word-like flexibility, you are God!

Here we just create a root class, other things, anytime, anywhere, want to add.

class Human:"""人"""def __init__(self):    self.__name = ""    self.__sex = ""def setName(self, name):    self.__name = namedef getName(self):    return self.__namedef setSex(self, sex):    self.__sex = sexdef getSex(self):    return self.__sexname = property(getName, setName) # 就像java中的POJO setter以及gettersex = property(getSex, setSex) # 就像java中的POJO setter以及getter

Below on the side of the execution side plump it

lilei = Human () lilei.sex =" male "Lilei.name =  "Li Lei" print "%s"% (Lilei.name, lilei.sex) Hanmeimei = Human () Hanmeimei.sex = "female" hanmeimei.name = "Han Meimei" print "%s%s appeared" % (Hanmeimei.name, Hanmeimei.sex) # pee Method def dopee (self, how):p rint "%s%s%s pee"% (Self.name, self.sex, how) human.dopee = Dopee #动态绑定方法lilei. Dopee ("Standing") hanmeimei.dopee ("squatting") # Learn the method def dolearning (self, learn):p rint "%s:%s"% (Self.name, Learn) human.dolearning = dolearning #动态绑定方法lilei. Dolearning ("How is You?") Lilei.dolearning ("I ' m fine, thank you!") _________________________________________________output: Li Lei male appearances output: Li Lei Han Meimei female appearances output: Li Lei man standing pee output: Han Meimei girl squatting in the urine outpu T: Li Lei: How is you?output: Li Lei: I ' m fine, thank you!  

All objects in Python are duck type, what is duck type? Anything that "quack" calls is a duck. Applied to the above scenario, as long as the object with the learning and urinating methods can be considered human. On the other hand, I am only concerned about whether the duck can "quack", if it can, whatever it is, then it is a duck; For people, only pay attention to whether they can pee and learn, both can pee and can learn, he is not a person?

Python and Java are like the Oriental metaphysics of Yin and Yang and Western philosophy.

This is all the Python code

test.py:

#!/usr/bin/env python#-*-coding:utf-8-*-class Human: "" "Person" "" Def __init__ ( Self): Self.__name = "" Self.__sex = "def setName (self, name): Self.__name = Namedef getName (self): return SE  Lf.__namedef setsex (self, sex): Self.__sex = Sexdef getsex (self): return Self.__sexname = Property (GetName, SetName) # just like pojosex in Java = property (Getsex, Setsex) # just like pojoif __name__ = ' __main__ ' in Java: Lilei = Human () lilei.sex = "Male" Lilei.  name = "Li lei" print "%s"% (Lilei.name, lilei.sex) Hanmeimei = Human () Hanmeimei.sex = "female" hanmeimei.name = "Han Meimei" print "%s %s Exit "% (Hanmeimei.name, Hanmeimei.sex) # pee Method def dopee (self, How): print"%s%s%s pee "% (Self.name, self.sex, how) H Uman.dopee = Dopee #动态绑定方法lilei. Dopee ("Standing") Hanmeimei.dopee ("Crouching") # Learning Method def dolearning (self, learn): print "%s:%s"% (s Elf.name, learn) human.dolearning = dolearning #动态绑定方法lilei. Dolearning ("How is You?") Lilei.dolearning ("I ' m fine, thank you!") 

Go Ideas

Object-oriented to go, without inheritance, is more like a combination of C and Python, and the duck type is carried forward to the extreme.

Interface (interface) is like a "duck", and the interface structure in the body of the method is the "duck" has the function, go, the interface can be described as: the guy with these functions is the "duck"

The method (func) is defined outside of the struct (class/struct) and is bound to this structure and can be described as: this is its function , and when some of the methods in a struct are contained in a interface, we say: Aha , this is the "Duck", even if it grows several legs (func), it is also ah

On inheritance, no, although the inline is much like inheritance but not. Inheritance is traced, and the inline expression of go is that you have me and I have your feelings, need to use a struct function, then said to it you are a part of me

struct, interface, func these are almost all the go object-oriented, so concise.

package main

Import ("FMT")//interface student type Student interface {learningenglish (string)}//struct type Human struct {Name string S Ex string}//learning English method, bound to Humanfunc (student Human) learningenglish (learning string) {FMT. Printf ("%s:%s\n", student.  Name, learning)}//structure man//go does not inherit this concept, here is embedded type Male struct {Human "embedded field"}type Female male//method, bound to the Human struct func (this Human) Pee (how string) {Fmt. Printf ("%s%s%s pee \ n", this.) Name, this. Sex, how)}//learning func dolearning (learning Student, learing string) {learning.learningenglish (learing)}//Peefunc dopee (H Uman Interface {}) {switch sex: = Human. ( Type) {case Male:sex. Pee ("standing") case female:sex. Pee ("Crouching")}}func Main () {lilei: = male{} lilei. Name = "Li lei" lilei. Sex = "Male" FMT. Printf ("%s%s appeared \ n", Lilei. Name, Lilei. SEX) Hanmeimei: = female{} hanmeimei. Name = "Han Meimei" Hanmeimei. Sex = "female" FMT. Printf ("%s%s appeared \ n", Hanmeimei. Name, Hanmeimei. Sex) Dopee (Lilei) dopee (Hanmeimei) dolearning (Lilei,"How is it?") Dolearning (Hanmeimei, "I ' m fine, thank you!")}

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.