zoukankan      html  css  js  c++  java
  • 关于Java、Python、Go编程思想的不同

    Go学习笔记 - 关于Java、Python、Go编程思想的不同

    看了两周七牛团队翻译的《Go语言程序设计》,基本上领略到了Go语言的魅力。学习一个语言,语法什么的任何人都是很容易学会,难就难在充分领略到这门编程语言的思想。

    面向对象

    喂!屌丝码农该找个对象了。

    除去Java Python Go这三种语言底层以及语法的不同,这里以个人的理解只说说其面向对象方面的思想。 一个简单的示例:

    描述人,李雷,韩梅梅,他俩都是好学生。

    将用 java python go 这三种语言分别简单的描述。


    Java 思想

    人,是抽象的概念,可以洗衣做饭的灵长目物种,没法特指一样具体的东西,但它也有一些如性别、撒尿这类的属性和功能。

    /**
     * 抽象出来的人
     */
    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(); // 抽象的方法
    }
    

    这里使用抽象类,是因为名字都是父母起的,但撒尿的方法男女不同。接下来是具象人这个抽象的概念了。这里就固话性别属性并且具体定义撒尿的方式。

    /** * 具象的男性 */ class Male extends Human { public Male() { this.sex = "男"; }

        /**
         * 实现的方法
         */
        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 + "蹲着撒尿.");
        }
    }
    

    现在有男人和女人了,然后李磊和韩梅梅就要来折磨我们了

    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: 韩梅梅 女蹲着撒尿.
    

    李磊和韩梅梅都是好学生,我们这里定义学习的接口,这里的接口就是,大家必须得死学傻学,怎么学看你自己。

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

    上面是教育部规定的,李磊韩梅梅作为学生必须得学,男人女人都得经历的。来实现学习接口。

     class Male extends Human implements Study {
        ......
        ......
    
        /**
         * 实现的接口
         */
        public void learningEnglish() {
            System.out.println(this.name + ": How are you?");
        }
    }
    
    /**
     * 具象的女性
     */
    class Female extends Human implements Study {
        ......
        ......
    
        /**
         * 实现的接口
         */
        public void learningEnglish() {
            System.out.println(this.name + ": I'm fine, thank you!");
        }
    }
    
    ......
    ......
    
    lilei.doPee();
    hanmeimei.doPee();
    
    lilei.learningEnglish();
    hanmeimei.learningEnglish();
    
    _________________________________________________
    output: 李磊: How are you?
    output: 韩梅梅: I'm fine, thank you!
    

    java的思想大致就是这么样。很严谨,就像一个老学究,1就是1,2就是2。

    这是所有的java代码

    Main.java

    public class Main {
    
        public static void main(String[] args) {
    
            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();
    
            lilei.learningEnglish();
            hanmeimei.learningEnglish();
        }
    }
    
    /**
     * 抽象出来的人
     */
    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(); // 抽象的方法
    }
    
    /**
     * 学习接口
     */
    interface Study {
        public abstract void learningEnglish();
    }
    
    /**
     * 具象的男性
     */
    class Male extends Human implements Study {
        public Male() {
            this.sex = "男";
        }
    
        /**
         * 实现的方法
         */
        public void doPee() {
            System.out.println(this.name + " " + this.sex + "站着撒尿.");
        }
    
        /**
         * 实现的接口
         */
        public void learningEnglish() {
            System.out.println(this.name + ": How are you?");
        }
    }
    
    /**
     * 具象的女性
     */
    class Female extends Human implements Study {
        public Female() {
            this.sex = "女";
        }
    
        /**
         * 实现的方法
         */
        public void doPee() {
            System.out.println(this.name + " " + this.sex + "蹲着撒尿.");
        }
    
        /**
         * 实现的接口
         */
        public void learningEnglish() {
            System.out.println(this.name + ": I'm fine, thank you!");
        }
    }
    

    Python 思想

    python无以言状的灵活,你就是上帝!

    这里我们只要创建一个根类,其他的东西,随时随地,想加就加。

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

    下面就边执行边丰满它

    lilei = Human()
    lilei.sex = "男"
    lilei.name = "李磊"
    print "%s %s 出场" % (lilei.name, lilei.sex)
    
    hanmeimei = Human()
    hanmeimei.sex = "女"
    hanmeimei.name = "韩梅梅"
    print "%s %s 出场" % (hanmeimei.name, hanmeimei.sex)
    
    
    # Pee的方法
    def doPee(self, how):
    print "%s %s %s撒尿" % (self.name, self.sex, how)
    
    Human.doPee = doPee #动态绑定方法
    
    lilei.doPee("站着")
    hanmeimei.doPee("蹲着")
    
    # 学习的方法
    def doLearning(self, learn):
    print "%s: %s" % (self.name, learn)
    
    Human.doLearning = doLearning #动态绑定方法
    
    lilei.doLearning("How are you?")
    lilei.doLearning("I'm fine, thank you!")
    
    _________________________________________________
    output: 李磊 男 出场
    output: 李磊韩梅梅 女 出场
    output: 李磊 男 站着撒尿
    output: 韩梅梅 女 蹲着撒尿
    output: 李磊: How are you?
    output: 李磊: I'm fine, thank you!
    

    python中一切对象都是鸭子类型,何谓鸭子类型?只要会"嘎嘎"叫的东西都是鸭子。应用到上面场景中,只要具有学习和撒尿方法的对象都可以看作人了。从另一方面说,我对于鸭子只关注它是否能够"嘎嘎"叫,如果能,不管是什么东西,那么它就是一只鸭子; 对于人,只关注他们是否能撒尿与学习,既能撒尿又能学习,他凭什么就不是人?

    python和java就好像阴阳之替的东方玄学之余西方哲学。

    这是所有的python代码

    test.py:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    class Human:
    """
    人
    """
    def __init__(self):
        self.__name = ""
        self.__sex = ""
    
    def setName(self, name):
        self.__name = name
    
    def getName(self):
        return self.__name
    
    def setSex(self, sex):
        self.__sex = sex
    
    def getSex(self):
        return self.__sex
    
    name = property(getName, setName) # 就像java中的POJO
    sex = property(getSex, setSex) # 就像java中的POJO
    
    
    if __name__ == '__main__':
    
    lilei = Human()
    lilei.sex = "男"
    lilei.name = "李磊"
    print "%s %s 出场" % (lilei.name, lilei.sex)
    
    hanmeimei = Human()
    hanmeimei.sex = "女"
    hanmeimei.name = "韩梅梅"
    print "%s %s 出场" % (hanmeimei.name, hanmeimei.sex)
    
    
    # Pee的方法
    def doPee(self, how):
        print "%s %s %s撒尿" % (self.name, self.sex, how)
    
    Human.doPee = doPee #动态绑定方法
    
    lilei.doPee("站着")
    hanmeimei.doPee("蹲着")
    
    # 学习的方法
    def doLearning(self, learn):
        print "%s: %s" % (self.name, learn)
    
    Human.doLearning = doLearning #动态绑定方法
    
    lilei.doLearning("How are you?")
    lilei.doLearning("I'm fine, thank you!")
    

    Go 思想

    面向对象之于Go,没有继承这么一说,更像是C与Python的结合体,并把鸭子类型发扬到极致。

    接口(interface)就好比是一只"鸭子",而interface结构体内包裹的方法就是这只"鸭子"所具有的功能,Go中,接口可以描述为: 具有这些功能的家伙就是这只"鸭子"

    方法(func)被定义在结构(类/struct)之外,被绑定于这个结构之上,可以描述为: 这是它的功能 ,当一个struct中的一些方法都包含在某个interface中时,我们就说: 啊哈,这就是那只"鸭子",哪怕它多长了几条腿(func),它也是啊

    关于继承,没有,go中虽然内嵌很像继承但不是。继承是一脉相传,而go的内嵌表达出你中有我我中有你的情怀,需要用到某个struct的功能了,那么就对它说 你就是我的一部分

    struct、interface、func 这些几乎就是Go面向对象的全部了,如此简洁。

    package main

    import (
        "fmt"
    )
    
    // 接口 学生
    type Student interface {
        learningEnglish(string)
    }
    
    // 结构
    type Human struct {
        Name string
        Sex  string
    }
    
    // 学习英语方法,绑定于Human
    func (student Human) learningEnglish(learning string) {
        fmt.Printf("%s: %s
    ", student.Name, learning)
    }
    
    // 结构 男人
    // go没有继承这个概念,这里是嵌入
    type Male struct {
        Human "嵌入字段"
    }
    
    type Female Male
    
    // 方法, 绑定到了Human结构
    func (this Human) Pee(how string) {
        fmt.Printf("%s %s %s撒尿
    ", this.Name, this.Sex, how)
    }
    
    // 学习
    func doLearning(learning Student, learing string) {
        learning.learningEnglish(learing)
    }
    
    // Pee
    func doPee(human interface {}) {
        switch sex := human.(type){
        case Male:
            sex.Pee("站着")
        case Female:
            sex.Pee("蹲着")
        }
    }
    
    func main() {
        lilei := Male{}
        lilei.Name = "李雷"
        lilei.Sex = "男"
        fmt.Printf("%s %s 出场
    ", lilei.Name, lilei.Sex)
    
        hanmeimei := Female{}
        hanmeimei.Name = "韩梅梅"
        hanmeimei.Sex = "女"
        fmt.Printf("%s %s 出场
    ", hanmeimei.Name, hanmeimei.Sex)
    
        doPee(lilei)
        doPee(hanmeimei)
    
        doLearning(lilei, "How are you?")
        doLearning(hanmeimei, "I'm fine, thank you!")
    }
    

    摆脱C++/Java/Python等思想的桎梏,才能领略Go的魅力

     
     
    分类: Go
  • 相关阅读:
    阿里容器简介
    docker学习笔记(总纲)
    Apache利用mod_limitipconn模块限制客户端多线程下载
    Android从assets目录下读取文件相关
    android 指纹识别
    App前后台判断
    Error:Failed to create directory 'C:UsersAdministrator.gradlecaches2.8scriptsijinit7_5jx13p26
    com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files
    重复导包:Error:Execution failed for task ':countrynumberlibrary:mergeDebugResources'. > Some file crunching failed, see logs for details
    支付宝集成
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3238574.html
Copyright © 2011-2022 走看看