Pascal、VB、C#、Java四种语法对照表

PascalVBC#Java四种语法对照表

ASP.NET支持的几种语言:c#.net,vb.net,delphi.net(非内置支持),由于在.net框架下,所有的语言均使用同一个类库,因此功能上是基本上一模一样,只语法上各不相同。

.net与java为两大阵营,为了比较,在此列出一个简单的语法对照表,看看你到底喜欢那个?

类别

delphi.net语法

vb.net语法(语法不区分大小写)

c#.net语法

java

语法

不区分大小写

不区分大小写

区分大小写

区分大小写

定义变量

Var

x: Integer;

s: string;

s1, s2: string;

o: TObject;

obj: TObject; obj := TObject.Create;

Public property name: String;

Dim x As Integer

Dim s As String

Dim s1, s2 As String

Dim o 'Implicitly Object

Dim obj As New Object()

Public name As String 

int x;

String s;

String s1, s2;      

Object o;

Object obj = new Object();

public String name;

int x;

String s;

String s1, s2;      

Object o;

Object obj = new Object();

public String name;

输出内容

Response.write(‘foo’);

Response.Write("foo")

Response.Write("foo");

Response.Write("foo");

注释

// This is a comment     

/* This is a

multi-line

comment */

' This is a comment

' This is a

' multi-line

' comment

// This is a comment

/* This is a

multi-line

comment */

// This is a comment

/* This is a

multi-line

comment */

读取数据集合数组

var

s, value: String

begin

s := Request.QueryString[‘Name’];

Value := Request.Cookies(‘Key’).Value;

end;

Dim s as String = Request.QueryString("Name")

Dim value As String = Request.Cookies("Key")

String s=Request.QueryString["Name"];

String value=Request.Cookies["key"]; 

String s = request.getParameter("Name");

String value;

Cookie ck = null;

Cookie args[] = Request.getCookies();

for(int i = 0; i<args.length; i++){

ck = args[i];

if(ck.getName().equals("key "))

value = ck.getValue();

}

字符串操作

var

s1, s2: String;

begin

s2 := ‘hello’;

s2 := s2 + ‘world’;

s1 := s2 + ‘!!!’;

End;

Dim s1, s2 As String

s2 = "hello"

s2 &= " world"

s1 = s2 & " !!!" 

String s1;

String s2 = "hello";

s2 += " world";

s1 = s2 + " !!!";

String s1;

String s2 = "hello";

s2 += " world";

s1 = s2 + " !!!";

类别

delphi.net语法

vb.net语法(语法不区分大小写)

c#.net语法

java

初始化变量

var

s: String;

I: Integer;

A: array of double = {3.00, 4.00, 5.00}

Dim s As String = "Hello World"

Dim i As Integer = 1

Dim a() As Double = { 3.00, 4.00, 5.00 } 

String s = "Hello World";

int i = 1

double[] a = { 3.00, 4.00, 5.00 };

String s = "Hello World";

int i = 1

double[] a = new double[]{ 3.00, 4.00, 5.00 };

定义简单数据集

Public Record Name: String;

Function GetName: String; Begin

...;

Result := …;

End;

Procedure SetName(Value: String); Begin

… := Value;

End;

End

Public Property Name As String

Get

...

Return ...;

End Get

Set

... = Value;

End Set

End Property 

public String name

{

get{

...

return ...;

}

set{

... = value;

}

} 

private String name;

public String getName(){

return name;

}

public void setName(String name){

this.name = name;

}

数组

var

a[0..2]: string

a[0..2,0..2]: String

begin

a[0] := ‘1’;

a[1] := ‘2’;

a[2] := ‘3’;

a[0,0] := ‘1’;

a[1,0] := ‘2’;

a[2,0] := ‘3’;

End;

Dim a(3) As String

a(0) = "1"

a(1) = "2"

a(2) = "3"

Dim a(3,3) As String

a(0,0) = "1"

a(1,0) = "2"

a(2,0) = "3"

String[] a = new String[3];

a[0] = "1";

a[1] = "2";

a[2] = "3”;

String[][] a = new String[3][3];

a[0][0] = "1";

a[1][0] = "2";

a[2][0] = "3"; 

String[] a = new String[3];

a[0] = "1";

a[1] = "2";

a[2] = "3";

String[][] a = new String[3][3];

a[0][0] = "1";

a[1][0] = "2";

a[2][0] = "3";

对象操作

var

bj: MyObject;

iObj: IMyObject;

begin

obj := Session(‘Some Value’);

iObj = Obj as IMyObject;

end;

Dim bj As MyObject = Session("Some Value")

Dim iObj As IMyObject = CType(obj, IMyObject)

MyObject obj = (MyObject)Session["Some Value"];

IMyObject iObj = obj;

MyObject obj = (MyObject)Session.getItem ("Some Value");

IMyObject iObj = obj;

类型转换

var

i: Integer;

s: String;

d: Double;

begin

i := 3;

s = i.ToString();

d := Double.Parse(s);

end;

Dim i As Integer = 3

Dim s As String = i.ToString()

Dim d As Double = Double.Parse(s):

int i = 3;

String s = i.ToString();

double d = Double.Parse(s);

int i = 3;

String s = Integer.valueof(i).toString();

double d = Double.valueof(s);

类别

delphi.net语法

vb.net语法(语法不区分大小写)

c#.net语法

java

If 结构

If Not (Request.QueryString = Null) then

begin

...

End;

If Not (Request.QueryString = Null)

...

End If 

if (Request.QueryString != null)

{

...

} 

if (Request.QueryString != null)

{

...

} 

Case结构

Case FirstName of

‘John’:

...

‘Paul’:

‘Ringo’:

End

Select (FirstName)

case "John" :

...

case "Paul" :

...

case "Ringo" :

...

End Select

switch (FirstName){

case "John" :

...

break;

case "Paul" :

...

break;

case "Ringo" :

...

break;

}

int flag = 1;

switch (flag){

case 1:

...

break;

case 2:

...

break;

}

For循环

Var

I: Integer;

Begin

For I := 0 to 2 do

A[i] := ‘test’;

End;

Dim I As Integer

For I = 0 To 2

 a(I) = "test"

Next

for (int i=0; i<3; i++)

a(i) = "test";

for (int i=0; i<3; i++)

a[i] = "test";

While循环

Var

I: Integer;

Begin

I := 0;

While i< 3 do

Begin

Console.WriteLen(i.ToString());

I := I + 1;

End;

End;

Dim I As Integer

I = 0

Do While I < 3

Console.WriteLine(i.ToString());

I = I + 1

Loop

int i = 0;

while (i<3)

{

Console.WriteLine(i.ToString());

i += 1;

}

int i = 0;

while (i<3)

{

System.out.println(i);

i += 1;

}

类别

delphi.net语法

vb.net语法(语法不区分大小写)

c#.net语法

java

类定义和继承

unit MySpace

interface

uses System;

type

Foo = Class(Bar)

private

x: integer;

public

procedure Create; override; //构造函数

procedure Add(x: Integer);

function GetNum: Integer;

end;

implementation

procedure Foo.Create;

begin

inherited; x := 4;

end;

procedure Foo.Add(x: Integer); begin

Self.x := Self.x + x;

end;

function Foo.GetNum: Integer; begin

Result := x;

end;

end;

Imports System

Namespace MySpace

Public Class Foo : Inherits Bar

Dim x As Integer

Public Sub New() ‘构造函数

MyBase.New()

x = 4

End Sub

Public Sub Add(x As Integer)

Me.x = Me.x + x

End Sub

Public Function GetNum() As Integer

Return x

End Function

End Class

End Namespace

' vbc /out:libraryvb.dll /t:library library.vb 

using System;

namespace MySpace

{

public class Foo : Bar

{

int x;

public Foo() {x = 4; } //构造函数

public void Add(int x) { this.x += x; }

public int GetNum() { return x; }

}

}

// csc /out:librarycs.dll /t:library library.cs

import java.lang.*;

package MySpace;

public class Foo extends Bar{

private int x; //私有变量

public Foo() {x = 4; } //构造函数

public void Add(int x) { this.x += x; } //过程

public int getNum() { return x; } //函数

}

事件处理

procedure Button1OnClick(Sender: Object;E: EventArgs);

begin

...

end;

‘ByRef 在Pascal中对象是缺省参数传递方式

Sub Button1_Click(Sender As Object, E As EventArgs)

...

End Sub

‘ByVal 在VB中对象是是缺省参数传递方式

void Button1_Click(Object sender, EventArgs E)

{

...

} 

‘ByRef 在C#中对象是是缺省参数传递方式

jButton1.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event

.MouseEvent evt) {

Button1_Click(evt);

}

});

private void Button1_Click(java.awt. event

.MouseEvent evt) {

}