人不該被語言束縛,咱們最重要的是思想。而思想絕對凌駕於語言之上。
java
語言對比手冊是我一直想寫的一個系列:通過認真思考,我決定從縱向和橫行兩個方面
來比較Java,Kotlin,Javascript,C++,Python,Dart,六種語言。
縱向版按知識點進行劃分,總篇數不定,橫向版按語言進行劃分,共6篇。其中:node
Java基於jdk8
Kotlin基於jdk8
JavaScript基於node11.10.1,使用ES6+
C++基於C++14
Python基於Python 3.7.2
Dart基於Dart2.1.0
複製代碼
public class Client {
public static void main(String[] args) {
System.out.println("HelloWorld");
}
}
複製代碼
fun main(args: Array<String>) {
println("HelloWorld")
}
複製代碼
console.log("HelloWorld");
複製代碼
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
複製代碼
if __name__ == '__main__':
print("HelloWorld")
複製代碼
main() {
print("HelloWorld");
}
複製代碼
怎麼看都是我家Java的類最好看python
定義一個Shape類,在構造方法中打印語句ios
|-- 類定義
public class Shape {
public Shape() {//構造器
System.out.println("Shape構造函數");
}
}
|-- 類實例化
Shape shape = new Shape();
複製代碼
私有成員變量+get+set+一參構造器+公共成員方法git
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());//四維空間
複製代碼
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());//二維點
複製代碼
借用C++的一句話:父類指針指向子類引用github
---->[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
複製代碼
|--- 抽象類
public abstract class Shape {
...
public abstract void draw();
...
}
|--- 接口
public interface Drawable {
void draw();
}
|--- 類實現接口
public class Shape implements Drawable {
複製代碼
冉冉升起的新星,功能比java胖了一大圈,就是感受挺亂的...編程
|-- 類定義
open class Shape {
constructor() {
println("Shape構造函數")
}
//init {//init初始化是時也可執行
// println("Shape初始化")
//}
}
|-- 類實例化
val shape = Shape()//形式1
val shape: Shape = Shape()//形式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()//繪製 四維空間
}
複製代碼
|-- 繼承-構造函數
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);//二維點
複製代碼
doDraw(Point("Point"));//Draw in Point
doDraw(Circle("Circle"));//Draw in Circle
fun doDraw(shape: Shape) {
shape.draw()
}
複製代碼
|--- 抽象類
abstract class Shape (name: String) {
var name = name
abstract fun draw();
}
|--- 接口
interface Drawable {
fun draw()
}
|--- 類實現接口
open class Shape(name: String) : Drawable {
複製代碼
|-- 類定義
class Shape {
constructor() {//構造器
console.log("Shape構造函數");
}
}
module.exports = Shape;
|-- 類實例化
const Shape = require('./Shape');
let shape = new Shape();
複製代碼
|-- 簡單封裝
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);//四維空間
複製代碼
---->[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
複製代碼
這姑且算是多態吧...bash
doDraw(new Point());//Draw in Point
doDraw(new Circle());//Draw in Circle
function doDraw(shape) {
shape.draw();
}
複製代碼
---->[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;
複製代碼
---->[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;
複製代碼
---->[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
複製代碼
---->[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();
}
複製代碼
|-- 含有純虛函數的類爲抽象類
---->[Shape.h]----------------
...
virtual void draw() const = 0;
...
|-- 子類須要覆寫純虛函數,不然不能直接實例化
---->[Circle.h]----------------
...
public:
void draw() const override;
...
複製代碼
|-- 類定義
class Shape:
def __init__(self):
print("Shape構造函數")
|-- 類實例化
from python.Shape import Shape
shape = Shape()
複製代碼
---->[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 四維空間
複製代碼
---->[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
複製代碼
---->[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
複製代碼
|-- 類定義
class Shape {
Shape() {
print("Shape構造函數");
}
}
|-- 類實例化
import 'Shape.dart';
var shape = Shape();
複製代碼
---->[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 四維空間
複製代碼
---->[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
複製代碼
---->[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();
}
複製代碼
|-- 抽象類
---->[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);
}
}
複製代碼
關於各語言認識深淺不一,若有錯誤,歡迎批評指正。微信
項目源碼 | 日期 | 附錄 |
---|---|---|
V0.1--無 | 2018-3-2 | 無 |
V0.2--無 | 2018-3-3 | 修正Kotlin相關點 |
發佈名:
編程語言對比手冊-縱向版[-類-]
捷文連接:https://juejin.im/post/5c7a9595f265da2db66df32capp
筆名 | 微信 | |
---|---|---|
張風捷特烈 | 1981462002 | zdl1994328 |
個人github:https://github.com/toly1994328
個人簡書:https://www.jianshu.com/u/e4e52c116681
個人簡書:https://www.jianshu.com/u/e4e52c116681
我的網站:http://www.toly1994.com
1----本文由張風捷特烈原創,轉載請註明
2----歡迎廣大編程愛好者共同交流
3----我的能力有限,若有不正之處歡迎你們批評指證,一定虛心改正
4----看到這裏,我在此感謝你的喜歡與支持