人不应被语言束缚, 我们最重要的是思想. 而思想绝对凌驾于语言之上.
前言:
语言对比手册是我一直想写的一个系列: 经过认真思考, 我决定从纵向和横行两个方面
来比较 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 类, 在构造方法中打印语句
|-- 类定义
- 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. 类的继承
关键字 extends
- 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++ 的一句话: 父类指针指向子类引用
---->[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. 类的定义和构造 (析构) 函数
|-- 类定义
- open class Shape {
- constructor() {
- println("Shape 构造函数")
- }
- //init {//init 初始化是时也可执行
- // println("Shape 初始化")
- //}
- }
|-- 类实例化
- val shape = Shape()// 形式 1
- val shape: Shape = Shape()// 形式 2
2. 类的封装(成员变量, 成员方法)
|-- 方式一: 构造方法初始化
- class Shape {
- var name: String? = null
- constructor(name: String) {
- this.name = name
- }
- fun draw() {
- println("绘制" + name!!)
- }
- ...
- }
|-- 方式二: 使用初始化列表
- class Shape (name: String) {
- var name = name
- fun draw() {
- println("绘制" + name)
- }
- }
|-- 使用
- val shape = Shape("Shape")
- shape.draw()// 绘制 Shape
- shape.name="四维空间"
- System.out.println(shape.name)// 四维空间
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 Circle(name: String) : Shape(name) {
- var radius: Int = 0
- 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 | 无 |
发布名: 编程语言对比手册 - 纵向版[- 类 -]
捷文链接: juejin.im/post/5c7785...
2. 更多关于我
笔名 | 微信 | |
---|---|---|
张风捷特烈 | 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---- 看到这里, 我在此感谢你的喜欢与支持
来源: https://juejin.im/post/5c7a9595f265da2db66df32c