关于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\n", student.Name, learning)
}
// 结构 男人
// go没有继承这个概念,这里是嵌入
type Male struct {
Human "嵌入字段"
}
type Female Male
// 方法, 绑定到了Human结构
func (this Human) Pee(how string) {
fmt.Printf("%s %s %s撒尿\n", 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 出场\n", lilei.Name, lilei.Sex)
hanmeimei := Female{}
hanmeimei.Name = "韩梅梅"
hanmeimei.Sex = "女"
fmt.Printf("%s %s 出场\n", hanmeimei.Name, hanmeimei.Sex)
doPee(lilei)
doPee(hanmeimei)
doLearning(lilei, "How are you?")
doLearning(hanmeimei, "I'm fine, thank you!")
}
摆脱C++/Java/Python等思想的桎梏,才能领略Go的魅力
关于Java、Python、Go编程思想的不同的更多相关文章
- java异常(编程思想)
通过异常处理错误 java的基本理念是“结构不佳的代码不能运行” 发现错误的理想时机是在编译阶段,也就是在你试图运行程序之前.然而编译期间并不能找出所有的错误,余下的问题必须在运行间解决.这就需要错误 ...
- 1.Java基础-面向对象编程思想(封装继承多态接口)
封装: 1.定义:隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别. 2.封装的目的是:增强安全性和简化编程,使用者不必了解具体的实现细节,而只是要通过外部接口,一特定的 ...
- Go学习笔记 - 关于Java、Python、Go编程思想的不同
***看了两周七牛团队翻译的<Go语言程序设计>,基本上领略到了Go语言的魅力.学习一个语言,语法什么的任何人都是很容易学会,难就难在充分领略到这门编程语言的思想.*** ## 面向对象 ...
- 《Java编程思想第四版完整中文高清版.pdf》-笔记
D.2.1 安插自己的测试代码 插入下述“显式”计时代码,对程序进行评测: long start = System.currentTimeMillis(); // 要计时的运算代码放在这儿 long ...
- (Java编程思想)Thinking in Java
1. 为什么突然想去研读<Thinking in Java>? 最近终于下定决心撸了一本<Thinking in Java>第四版,虽然在此之前我就久闻这本书的大名,但一直未曾 ...
- Java编程思想(第4版) 中文清晰PDF完整版
Java编程思想(第4版) 中文清晰PDF完整版 [日期:2014-08-11] 来源:Linux社区 作者:Linux [字体:大 中 小] <Java编程思想>这本书赢得了全 ...
- JAVA编程思想——分析阅读
需要源码.JDK1.6 .编码风格参考阿里java规约 7/12开始 有点意识到自己喜欢理论大而泛的模糊知识的学习,而不喜欢实践和细节的打磨,是因为粗心浮躁导致的么? cron表达式使用 设计能力.领 ...
- 【系列】Python编程思想(1):Python简介与开发环境搭建
李宁老师的 开始学习. 本系列文章深入介绍了Python的各种技术,堪称是目前最全的Python教程.主要目的是让读者可以了解Python的各种核心技术,包括各种Python函数库.本教程使用Py ...
- JAVA编程思想(第四版)学习笔记----4.8 switch(知识点已更新)
switch语句和if-else语句不同,switch语句可以有多个可能的执行路径.在第四版java编程思想介绍switch语句的语法格式时写到: switch (integral-selector) ...
随机推荐
- JDBC连接池的简单实现
首先解释一下,我在做自己android发育.java web这是我的弱点,就在最近,京东云免费,因此,要折腾几.有一点经验,特别是作为共享. 假设内容的文章是错,还请高手指正. 我在这里web结束,需 ...
- 使用PHP生成PDF文档
原文:使用PHP生成PDF文档 实际工作中,我们要使用PHP动态的创建PDF文档,目前有许多开源的PHP创建PDF的类库,今天我给大家来介绍一款优秀的PDF库,它就是TCPDF,TCPDF是一个用于快 ...
- Post和Get差异
GET和POST差别例如以下: 1,生成方式 get方式有四种:1)直接在URL地址栏中输入URL.2)网页中的超链接.3)form中method为get. 4)form中method为空时.默认是g ...
- JUnit介绍
8.1.1 JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试,其设计非常小巧,但功能却非常强 大. 下面是JUnit一些特性的总结: — 提供的API可以让开发人员写出测试结果明 ...
- ActiveReports 9实战教程(2): 准备数据源(设计时、运行时)
原文:ActiveReports 9实战教程(2): 准备数据源(设计时.运行时) 在上讲中<ActiveReports 9实战教程(1): 手把手搭建环境Visual Studio 2013 ...
- OSG(OpenSceneGraphcow.osg)配置笔记
OpenSceneGraph是一款高性能的3D图形开发库.广泛应用在可视化仿真.游戏.虚拟现实.高端技术研发以及建模等领域.使用标准的C++和OpenGL编写而成,可以运行在Windows系列.OSX ...
- AJAX 表单提交 文件上传
1. AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载 ...
- 软件协作工具Trello
软件协作工具Trellohttps://trello.com/ Q群里conan发了个UE4的RODEMAP的trello链接,感受了一下Trello这款软件协作工具.(https://trello. ...
- ASP.NET MVC应用程序使用异步及存储过程
ASP.NET MVC应用程序使用异步及存储过程 是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译 ...
- Windows使用SSH管理Ubuntu
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/manage-ubuntu-on-wind ...