People should not be bound by language, our most important is the thought. And thought is absolutely above language.

Preface:

The language comparison manual is a series I’ve always wanted to write: after careful consideration, I’ve decided to compare Java, Kotlin, Javascript, C++, Python, and Dart, both vertically and horizontally. The vertical version is divided according to knowledge points, and the total number of articles is uncertain. The horizontal version is divided according to language, with a total of 6 articles. Among them:

Java based on jdk8 Kotlin based on jdk8 JavaScript based on node11.10.1 ES6+ C++ based on C++14 Python based on Python 3.7.2 Dart based on Dart2.1.0Copy the code

Let’s go helloworld

1. Java version:
public class Client {
    public static void main(String[] args) {
        System.out.println("HelloWorld"); }}Copy the code

2. The Kotlin version:
fun main(args: Array<String>) {
    println("HelloWorld")}Copy the code

3. The JavaScript version:
console.log("HelloWorld");
Copy the code

4. A c + + version:
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
Copy the code

5. The Python version:
if __name__ == '__main__':
   print("HelloWorld")
Copy the code

6. The Dart version:
main() {
  print("HelloWorld");
}
Copy the code

One, Java code implementation

How to look at my Java class is the most beautiful

1. Class definition and constructor

Define a Shape class that prints statements in the constructor

| - class defines public class Shape {publicShape() {// the constructor system.out.println ()"Shape constructor"); }} | - class instantiation Shape Shape = new Shape ();Copy the code

2. Class encapsulation (member variables, member methods)

Private member variable + GET +set+ one parameter constructor + public member method

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("Map"+ name); }... } | - using Shape Shape = new Shape ("Shape"); shape.draw(); // Draw shape.setname ()"Four dimensional space"); System.out.println(shape.getName()); // Four dimensional spaceCopy the code

3. Class inheritance
public class Point extends Shape { public Point(String name) { super(name); } public int x; public int y; } | - subclasses can use of the parent class method is used to Point Point = new Point ("Two dimensional point"); point.draw(); System.out.println(point.getName()); / / 2 d pointsCopy the code

4. Class polymorphism

To borrow a phrase from C++ : a pointer to a parent class points to a reference to a child class

-- -- -- - > [Shape subclasses: Circle] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - public class Circle extends Shape {private int mRadius; public intgetRadius() {
        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 subclasses: Point] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - public class Point extends Shape {publicPoint() {
    }
    public Point(String name) {
        super(name);
    }
    public int x;
    public int y;
    @Override
    public void draw() {
       System.out.println("Draw in Point"); }} - > [test function] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - private static voiddoDraw(Shape shape) { shape.draw(); } | - the same parent class is not the same kind object method perform different Shape point = new point ();doDraw(point); //Drawin Point
Shape circle = new Circle();
doDraw(circle); //Drawin Circle
Copy the code

5. Other features
| - abstract class public abstract class Shape {... public abstract void draw(); . } | - interface public interface Drawable {void the draw (); } | - class implements the interface public class Shape implements Drawable {Copy the code

Two, Kotlin code implementation

Rising star, function than Java fat circle, but feel quite messy…

1. Class definition and constructor

| - class defines the open class Shape {constructor() {
        println("Shape constructor")} //init {//init initialization can be executed if yes // println("Shape initialization") / /}} | - class instantiation val shape = shape () / / form 1 val shape: shape = shape () / 2 / formCopy the code

2. Class encapsulation (member variables, member methods)
| - a: initialization method to construct the open class Shape {var name: String constructor (name: String =""){
        this.name = name
    }
   open fun draw(){
        println("绘制 "+ name)}} | - way 2: use the initialization list open class Shape (var name: String ="") {
    open fun draw(){
        println("绘制 "+ name)}} | - using val shape = shape ("Shape") shape.draw()// Draw shape Shape.name ="Four dimensional space"System. Out.println (shape. Name) / / | four-dimensional space - the apply call println (shape ("Shape").apply {this.draw()// This is the Shape object this.name="Four dimensional space"
}.name)

|-- letCall the Shape ("Shape").let {
    it.name = "Four dimensional space"It.draw()// draw a four-dimensional space}Copy the code

3. Class inheritance
| - inheritance - constructor class Point: Shape {var x: Int = 0 var y: Int = 0 constructor (name: String) : super (name) {} override fundraw() {
        println("Draw in Point")}} | - from the Point of view of elegant, more suitable for the class below Point (var x: Int = 0, var y: Int = 0, name: String), Shape (name) {override fundraw() {
        println("Draw in Point")}} | - inheritance - the parent class constructor Circle (radius, var: Int = 0, name: String), Shape (name) {override fundraw() {
        println("Draw in Circle")}} | - using val point = point ("Two dimensional point"); point.draw(); System.out.println(point.name); / / 2 d pointsCopy the code

4. Class polymorphism
doDraw(Point("Point")); //Drawin Point
doDraw(Circle("Circle")); //Drawin Circle

fun doDraw(shape: Shape) {
    shape.draw()
}
Copy the code

5. Other features
| - abstract class abstract class Shape (name: String) {var name = name the abstract fun the draw (); } | - interface interface Drawable {fun the draw ()} | - class implements the interface open class Shape (name: String) : Drawable {Copy the code

Third, JavaScript code implementation

1. Class definition and constructor
| - class defines a class Shape {constructor() {// the constructor console.log()"Shape constructor"); } } module.exports = Shape; | - class instantiation const Shape = require ('./Shape');

let shape = new Shape();
Copy the code

2. Class encapsulation (member variables, member methods)
| - simple encapsulation class Shape {the getname() {
        return this._name;
    }
    set name(value) {
        this._name = value;
    }
    constructor(name) {
        this._name = name;
    }
    draw() {
        console.log("Map"+ this._name); } } module.exports = Shape; | - uselet shape = new Shape("Shape"); shape.draw(); // Draw Shape. Shape. Name ="Four dimensional space"; console.log(shape.name); // Four dimensional spaceCopy the code

3. Class inheritance
---->[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; | - using const Point = require ('./Point');
const Circle = require('./Circle');

let point =new Point("Point"); point.draw(); //DrawinPoint point.x = 100; console.log(point.x); / / 100let circle =new Circle("Circle"); circle.draw(); //DrawinCircle circle.radius = 100; console.log(circle.radius); / / 100Copy the code

4. Class polymorphism

It’s a polymorphism…

doDraw(new Point()); //Drawin Point
doDraw(new Circle()); //Drawin Circle

function doDraw(shape) {
    shape.draw();
}
Copy the code

Four, C++ code implementation

1. Class definition and constructor (destructor) functions
---->[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 constructor" << endl;
}
Shape::~Shape() {
    cout << "Shape building function"<< endl; } | - class instantiation Shape Shape; Shape * Shape = new Shape(); // Delete shape; shape = nullptr;Copy the code

2. Class encapsulation (member variables, member methods)
---->[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; } | - use form (pointer) Shape * Shape = new Shape (); string name="four side space";
shape->setName(name); shape->draw(); //draw four side space delete shape; shape = nullptr;Copy the code

3. Class inheritance
---->[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; } | - using Point * Point = new Point (); point->draw(); //DrawinPoint point->x = 100; cout << point->x << endl; / / 100Copy the code

4. Class polymorphism
---->[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; } | - using Shape * point = new point (); Shape *circle = new Circle();doDraw(point);
doDraw(circle);

void doDraw(Shape *pShape) {
    pShape->draw();
}
Copy the code

5. Other features
| - contains pure virtual functions such as abstract classes -- -- -- - > [Shape. H] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -... virtual void draw() const = 0; . | - subclass need overwrite the pure virtual function, otherwise cannot be directly instantiated -- - > [Circle. H] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -... public: void draw() const override; .Copy the code

5. Python code implementation

1. Class definition and constructor
| - class defines a class Shape: def __init__ (self) :print("Shape constructor") | -- - class instantiates the from python. Shape import Shape Shape = Shape ()Copy the code

2. Class encapsulation (member variables, member methods)
---->[Shape.py]-----------------
class Shape:
    def __init__(self, name):
        self.name = name
        print("Shape constructor")

    def draw(self):
        print("draw "+ self. Name) | - using shape = shape ("Shape")
shape.draw()#draw Shape
shape.name="Four dimensional space"
shape.draw()#draw 4-dimensional space
Copy the code

3. Class inheritance
---->[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"Use point = point) | - ("Point")
point.draw()#Draw in Point
point.x=100
print(point.x)# 100
Copy the code

4. Class polymorphism
---->[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") | -- - use the defdoDraw(shape):
    shape.draw()

doDraw(Point("Point"))#Draw in Point
doDraw(Circle("Circle"))#Draw in Circle
Copy the code

Dart code implementation

1. Class definition and constructor
| - class defines a class Shape {Shape() {
    print("Shape constructor"); }} | - class instantiation import'Shape.dart';
var shape = Shape();
Copy the code

2. Class encapsulation (member variables, member methods)
---->[Shape.dart]-----------------
class Shape {
  String name;
  Shape(this.name);

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

|-- 使用
var shape = Shape("Shape"); shape.draw(); //draw Shape shape.name="Four dimensional space"; shape.draw(); //draw a four-dimensional spaceCopy the code

3. Class inheritance
---->[Point.dart]------------------
import 'Shape.dart';
class Point extends Shape {
  Point(String name) : super(name);
  int x;
  int y;
  @override
  draw() {
    print("Draw in Point"); }} | - using var point = point ("Point"); point.draw(); //Drawin Point
point.x=100;
print(point.x); / / 100Copy the code

4. Class polymorphism
---->[Circle.dart]------------------
import 'Shape.dart';
class Circle extends Shape {
  Circle(String name) : super(name);
  int radius;
  @override
  draw() {
    print("Draw in Circle"); }} | - usedoDraw(Point("Point")); //Drawin Point
doDraw(Circle("Circle")); //Drawin Circle

void doDraw(Shape shape) {
  shape.draw();
}
Copy the code

5. Other features
| - abstract class -- -- -- - > [Drawable. Dart] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the abstract class Drawable {void the draw (); }... | -- -- -- -- - > [Shape. The dart] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the import'Drawable.dart';
class Shape implements Drawable{
  String name;
  Shape(this.name);
  @override
  void draw() {
    print("draw "+name); }}Copy the code

About each language understanding depth is different, if there is a mistake, welcome to criticize and correct.


Postscript: Jie wen standard

1. Growth record and Errata of this paper
Program source code The date of The appendix
V0.1, The 2018-3-2 There is no
V0.2, The 2018-3-3 Fixed Kotlin correlation points

Release name: compared with manual programming language – vertical version [- class -] rapid link: https://juejin.cn/post/6844903788612943885

2. More about me
Pen name QQ WeChat
Zhang Feng Jie te Li 1981462002 zdl1994328

My github:https://github.com/toly1994328 Jane books: https://www.jianshu.com/u/e4e52c116681 my Jane books: https://www.jianshu.com/u/e4e52c116681 website: http://www.toly1994.com

3. The statement

1—- this article is originally written by Zhang Feng Jetelie, please note 2—- all programming enthusiasts are welcome to communicate with each other 3—- personal ability is limited, if there is any error, you are welcome to criticize and point out, will be modest to correct 4—- See here, I thank you here for your love and support