内容简介:语言对比手册是我一直想写的一个系列:经过认真思考,我决定从纵向和横行两个方面来比较Java,Kotlin,Javascript,C++,Python,Dart,六种语言。纵向版按知识点进行划分,总篇数不定,横向版按语言进行划分,共6篇。其中:
语言对比手册是我一直想写的一个系列:经过认真思考,我决定从纵向和横行两个方面
来比较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: github.com/toly1994328
我的简书: www.jianshu.com/u/e4e52c116…
我的简书: www.jianshu.com/u/e4e52c116…
个人网站:www.toly1994.com
3.声明
1----本文由张风捷特烈原创,转载请注明
2----欢迎广大编程爱好者共同交流
3----个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
4----看到这里,我在此感谢你的喜欢与支持
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 编程语言对比手册-纵向版[-文件-]
- 纵向带图片覆盖显示的菜单效果
- Android自定义View - 简单纵向抽屉的实现
- 逻辑式编程语言极简实现(使用C#) - 1. 逻辑式编程语言介绍
- 那些主流编程语言的知识:C 语言(一)
- 那些主流编程语言的知识:C 语言(一)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Building Websites with Joomla!
H Graf / Packt Publishing / 2006-01-20 / USD 44.99
This book is a fast paced tutorial to creating a website using Joomla!. If you've never used Joomla!, or even any web content management system before, then this book will walk you through each step i......一起来看看 《Building Websites with Joomla!》 这本书的介绍吧!