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的魅力
---------------------------------
浙公網安備 33010602011771號