程式語言對比手冊-縱向版[-類-]

張風捷特烈發表於2019-03-02

人不應被語言束縛,我們最重要的是思想。而思想絕對凌駕於語言之上。

前言:

語言對比手冊是我一直想寫的一個系列:經過認真思考,我決定從縱向和橫行兩個方面
來比較Java,Kotlin,Javascript,C++,Python,Dart,六種語言。
縱向版按知識點進行劃分,總篇數不定,橫向版按語言進行劃分,共6篇。其中:

Java基於jdk8
Kotlin基於jdk8
JavaScript基於node11.10.1,使用ES6+
C++基於C++14
Python基於Python 3.7.2
Dart基於Dart2.1.0
複製程式碼

別的先不說,helloworld走起

1.Java版:
public class Client {
    public static void main(String[] args) {
        System.out.println("HelloWorld");
    }
}
複製程式碼

2.Kotlin版:
fun main(args: Array<String>) {
    println("HelloWorld")
}
複製程式碼

3.JavaScript版:
console.log("HelloWorld");
複製程式碼

4.C++版:
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
複製程式碼

5.Python版:
if __name__ == '__main__':
   print("HelloWorld")
複製程式碼

6.Dart版:
main() {
  print("HelloWorld");
}
複製程式碼

一、Java程式碼實現

怎麼看都是我家Java的類最好看

1.類的定義和建構函式

定義一個Shape類,在構造方法中列印語句

java類定義的形式.png

|-- 類定義
public class Shape {
    public Shape() {//構造器
        System.out.println("Shape建構函式");
    }
}

|-- 類例項化
Shape shape = new Shape();
複製程式碼

2.類的封裝(成員變數,成員方法)

私有成員變數+get+set+一參構造器+公共成員方法

public class Shape {
    private String name;
    public Shape(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void draw() {
        System.out.println("繪製" + name);
    }
    ...
}

|-- 使用
Shape shape = new Shape("Shape");
shape.draw();//繪製Shape
shape.setName("四維空間");
System.out.println(shape.getName());//四維空間
複製程式碼

3.類的繼承
public class Point extends Shape {
    public Point(String name) {
        super(name);
    }
    public int x;
    public int y;
}

|-- 使用 子類可使用父類的方法
Point point = new Point("二維點");
point.draw();//繪製二維點
System.out.println(point.getName());//二維點
複製程式碼

4.類的多型性

借用C++的一句話:父類指標指向子類引用

多型.png

---->[Shape子類:Circle]--------------------------
public class Circle extends Shape {
    private int mRadius;

    public int getRadius() {
        return mRadius;
    }

    public void setRadius(int radius) {
        mRadius = radius;
    }

    public Circle() {
    }

    public Circle(String name) {
        super(name);
    }

    @Override
    public void draw() {
        System.out.println("Draw in Circle");
    }
}

---->[Shape子類:Point]--------------------------
public class Point extends Shape {
    public Point() {
    }
    public Point(String name) {
        super(name);
    }
    public int x;
    public int y;
    @Override
    public void draw() {
       System.out.println("Draw in Point");
    }
}

---->[測試函式]--------------------------
private static void doDraw(Shape shape) {
    shape.draw();
}

|-- 相同父類不同類物件執行不同方法
Shape point = new Point();
doDraw(point);//Draw in Point
Shape circle = new Circle();
doDraw(circle);//Draw in Circle
複製程式碼

5.其他特性
|--- 抽象類
public abstract class Shape {
    ...
    public abstract void draw();
    ...
}

|--- 介面
public interface Drawable {
    void draw();
}

|--- 類實現介面
public class Shape implements Drawable {
複製程式碼

二、Kotlin程式碼實現

冉冉升起的新星,功能比java胖了一大圈,就是感覺挺亂的...

1.類的定義和建構函式

Kotlin類定義的形式.png

|-- 類定義
open class Shape {
    constructor() {
        println("Shape建構函式")
    }
    
    //init {//init初始化是時也可執行
    //    println("Shape初始化")
    //}
}

|-- 類例項化
val shape = Shape()//形式1
val shape: Shape = Shape()//形式2
複製程式碼

2.類的封裝(成員變數,成員方法)
|-- 方式一:構造方法初始化
open class Shape {
    var name:String
    constructor(name: String = ""){
        this.name = name
    }
   open fun draw(){
        println("繪製 "+name)
    }
}

|-- 方式二:使用初始化列表
open class Shape(var name: String = "") {
    open fun draw(){
        println("繪製 "+name)
    }
}

|-- 使用
val shape = Shape("Shape")
shape.draw()//繪製Shape
shape.name="四維空間"
System.out.println(shape.name)//四維空間

|-- apply呼叫
println(Shape("Shape").apply {
    this.draw()//也可以不用this,這裡只是強調本this為Shape物件
    this.name="四維空間"
}.name)

|-- let呼叫
Shape("Shape").let {
    it.name = "四維空間"
    it.draw()//繪製 四維空間
}
複製程式碼

3.類的繼承
|-- 繼承-建構函式
class Point : Shape {
    var x: Int = 0
    var y: Int = 0
    constructor(name: String) : super(name) {}
    override fun draw() {
        println("Draw in Point")
    }
}

|-- 從優雅的角度來看,下面更適合
class Point(var x: Int = 0,var y: Int = 0,name: String) : Shape(name) {
    override fun draw() {
        println("Draw in Point")
    }
}

|-- 繼承-父構造器
class Circle(var radius: Int = 0,name: String) : Shape(name) {
    override fun draw() {
        println("Draw in Circle")
    }
}

|--使用
val point = Point("二維點");
point.draw();//繪製二維點
System.out.println(point.name);//二維點
複製程式碼

4.類的多型性
doDraw(Point("Point"));//Draw in Point
doDraw(Circle("Circle"));//Draw in Circle

fun doDraw(shape: Shape) {
    shape.draw()
}
複製程式碼

5.其他特性
|--- 抽象類
abstract class Shape (name: String) {
    var name = name
    abstract fun draw();
}

|--- 介面
interface Drawable {
    fun draw()
}

|--- 類實現介面
open class Shape(name: String) : Drawable {
複製程式碼

三、JavaScript程式碼實現

1.類的定義和建構函式
|-- 類定義
class Shape {
    constructor() {//構造器
        console.log("Shape建構函式");
    }
}
module.exports = Shape;

|-- 類例項化
const Shape = require('./Shape');

let shape = new Shape();
複製程式碼

2.類的封裝(成員變數,成員方法)
|-- 簡單封裝
class Shape {
    get name() {
        return this._name;
    }
    set name(value) {
        this._name = value;
    }
    constructor(name) {
        this._name = name;
    }
    draw() {
        console.log("繪製" + this._name);
    }
}
module.exports = Shape;

|-- 使用
let shape = new Shape("Shape");
shape.draw();//繪製Shape
shape.name = "四維空間";
console.log(shape.name);//四維空間
複製程式碼

3.類的繼承
---->[Point.js]-----------------
const Shape = require('./Shape');
class Point extends Shape {
    constructor(name) {
        super(name);
        this.x = 0;
        this.y = 0;
    }
    draw() {
        console.log("Draw in " + this.name);
    }
}
module.exports = Point;

---->[Circle.js]-----------------
const Shape = require('./Shape');
class Circle extends Shape {
    constructor(name) {
        super(name);
        this.radius = 0;
    }
    draw() {
        console.log("Draw in " + this.name);
    }
}
module.exports = Circle;

|-- 使用
const Point = require('./Point');
const Circle = require('./Circle');

let point =new Point("Point");
point.draw();//Draw in Point
point.x = 100;
console.log(point.x);//100

let circle =new Circle("Circle");
circle.draw();//Draw in Circle
circle.radius = 100;
console.log(circle.radius);//100
複製程式碼

4.類的多型性

這姑且算是多型吧...

doDraw(new Point());//Draw in Point
doDraw(new Circle());//Draw in Circle

function doDraw(shape) {
    shape.draw();
}
複製程式碼

四、C++程式碼實現

1.類的定義和構造(析構)函式
---->[Shape.h]-----------------
#ifndef C_SHAPE_H
#define C_SHAPE_H
class Shape {
public:
    Shape();
    ~Shape();
};
#endif //C_SHAPE_H

---->[Shape.cpp]-----------------
#include "Shape.h"
#include <iostream>
using namespace std;

Shape::Shape() {
    cout << "Shape建構函式" << endl;
}
Shape::~Shape() {
    cout << "Shape析造函式" << endl;
}

|-- 類例項化
Shape shape;//例項化物件

Shape *shape = new Shape();//自己開闢記憶體例項化
delete shape;
shape = nullptr;
複製程式碼

2.類的封裝(成員變數,成員方法)
---->[Shape.h]-----------------
...
#include <string>
using namespace std;
class Shape {
public:
    ...
    string &getName();
    Shape(string &name);
    void setName(string &name);
    void draw();
private:
    string name;
};
...

---->[Shape.cpp]-----------------
...
string &Shape::getName() {
    return name;
}
void Shape::setName(string &name) {
    Shape::name = name;
}
Shape::Shape(string &name) : name(name) {}
void Shape::draw() {
    cout << "draw " << name << endl;
}


|-- 使用(指標形式)
Shape *shape = new Shape();
string name="four side space";
shape->setName(name);
shape->draw();//draw four side space
delete shape;
shape = nullptr;
複製程式碼

3.類的繼承
---->[Point.h]------------------
#ifndef CPP_POINT_H
#define CPP_POINT_H
#include "Shape.h"
class Point : public Shape{
public:
    int x;
    int y;
    void draw() override;
};
#endif //CPP_POINT_H

---->[Point.cpp]------------------
#include "Point.h"
#include <iostream>
using namespace std;
void Point::draw() {
    cout << "Draw in Point" << endl;
}

|-- 使用
Point *point = new Point();
point->draw();//Draw in Point
point->x = 100;
cout <<  point->x << endl;//100
複製程式碼

4.類的多型性
---->[Circle.h]------------------
#ifndef CPP_CIRCLE_H
#define CPP_CIRCLE_H
#include "Shape.h"
class Circle : public Shape{
public:
    void draw() override;
private:
    int mRadius;
    
};
#endif //CPP_CIRCLE_H

---->[Circle.cpp]------------------
#include "Circle.h"
#include <iostream>
using namespace std;
void Circle::draw() {
    cout << "Draw in Point" << endl;
}

|-- 使用
Shape *point = new Point();
Shape *circle = new Circle();
doDraw(point);
doDraw(circle);

void doDraw(Shape *pShape) {
    pShape->draw();
}
複製程式碼

5.其他特性
|-- 含有純虛擬函式的類為抽象類
---->[Shape.h]----------------
...
virtual void draw() const = 0;
...

|-- 子類需要覆寫純虛擬函式,否則不能直接例項化
---->[Circle.h]----------------
... 
public:
    void draw() const override;
...
複製程式碼

五、Python程式碼實現

1.類的定義和建構函式
|-- 類定義
class Shape:
    def __init__(self):
        print("Shape建構函式")

|-- 類例項化
from python.Shape import Shape
shape = Shape()
複製程式碼

2.類的封裝(成員變數,成員方法)
---->[Shape.py]-----------------
class Shape:
    def __init__(self, name):
        self.name = name
        print("Shape建構函式")

    def draw(self):
        print("draw " + self.name)

|-- 使用
shape = Shape("Shape")
shape.draw()#draw Shape
shape.name="四維空間"
shape.draw()#draw 四維空間
複製程式碼

3.類的繼承
---->[Point.py]------------------
from python.Shape import Shape

class Point(Shape):
    def __init__(self, name):
        super().__init__(name)
        self.x = 0
        self.y = 0
    def draw(self):
        print("Draw in Point")

|-- 使用
point = Point("Point")
point.draw()#Draw in Point
point.x=100
print(point.x)#100
複製程式碼

4.類的多型性
---->[Circle.py]------------------

from python.Shape import Shape
class Circle(Shape):
    def __init__(self, name):
        super().__init__(name)
        self.radius = 0
    def draw(self):
        print("Draw in Circle")

|-- 使用
def doDraw(shape):
    shape.draw()

doDraw(Point("Point"))#Draw in Point
doDraw(Circle("Circle"))#Draw in Circle
複製程式碼

六、Dart程式碼實現

1.類的定義和建構函式
|-- 類定義
class Shape {
  Shape() {
    print("Shape建構函式");
  }
}

|-- 類例項化
import 'Shape.dart';
var shape = Shape();
複製程式碼

2.類的封裝(成員變數,成員方法)
---->[Shape.dart]-----------------
class Shape {
  String name;
  Shape(this.name);

  draw() {
    print("draw " +name);
  }
}

|-- 使用
var shape = Shape("Shape");
shape.draw();//draw Shape
shape.name="四維空間";
shape.draw();//draw 四維空間
複製程式碼

3.類的繼承
---->[Point.dart]------------------
import 'Shape.dart';
class Point extends Shape {
  Point(String name) : super(name);
  int x;
  int y;
  @override
  draw() {
    print("Draw in Point");
    
  }
}

|-- 使用
var point = Point("Point");
point.draw();//Draw in Point
point.x=100;
print(point.x);//100
複製程式碼

4.類的多型性
---->[Circle.dart]------------------
import 'Shape.dart';
class Circle extends Shape {
  Circle(String name) : super(name);
  int radius;
  @override
  draw() {
    print("Draw in Circle");
  }
}

|-- 使用
doDraw(Point("Point"));//Draw in Point
doDraw(Circle("Circle"));//Draw in Circle

void doDraw(Shape shape) {
  shape.draw();
}
複製程式碼

5.其他特性
|-- 抽象類
---->[Drawable.dart]----------------
abstract class Drawable {
    void draw();
}
...

|-- 實現
---->[Shape.dart]----------------
import 'Drawable.dart';
class Shape implements Drawable{
  String name;
  Shape(this.name);
  @override
  void draw() {
    print("draw " +name);
  }
}
複製程式碼

關於各語言認識深淺不一,如有錯誤,歡迎批評指正。


後記:捷文規範

1.本文成長記錄及勘誤表
專案原始碼 日期 附錄
V0.1--無 2018-3-2
V0.2--無 2018-3-3 修正Kotlin相關點

釋出名:程式語言對比手冊-縱向版[-類-]
捷文連結:https://juejin.im/post/5c7a9595f265da2db66df32c

2.更多關於我
筆名 QQ 微信
張風捷特烈 1981462002 zdl1994328

我的github:https://github.com/toly1994328
我的簡書:https://www.jianshu.com/u/e4e52c116681
我的簡書:https://www.jianshu.com/u/e4e52c116681
個人網站:http://www.toly1994.com

3.宣告

1----本文由張風捷特烈原創,轉載請註明
2----歡迎廣大程式設計愛好者共同交流
3----個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正
4----看到這裡,我在此感謝你的喜歡與支援

icon_wx_200.png

相關文章