Pascal、VB、C#、Java四种语法对照表
ASP.NET支持的几种语言:c#.net,vb.net,delphi.net(非内置支持),由于在.net框架下,所有的语言均使用同一个类库,因此功能上是基本上一模一样,只语法上各不相同。
.net与java为两大阵营,为了比较,在此列出一个简单的语法对照表,看看你到底喜欢那个?
类别 | delphi.net语法 | vb.net 语法(语法不区分大小写) | c#.net 语法 | java |
语法 | 不区分大小写 | 不区分大小写 | 区分大小写 | 区分大小写 |
定义变量 | Var 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 | ' This is a comment ' multi-line | // This is a comment /* This is a | // This is a comment /* This is a |
读取数据集合数组 | 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 | String s1; | String s1; |
类别 | 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" | String s = "Hello World"; | String s = "Hello World"; |
定义简单数据集 | Public Record Name: String;
Function GetName: String; Begin ...; Result := …; End;
Procedure SetName(Value: String); Begin … := Value; End;
End | Public Property Name As String
Get
... = Value;
| public String name {
| 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 | String[] a = new String[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"]; | MyObject obj = (MyObject)Session.getItem ("Some Value"); |
类型转换 | 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; | int i = 3; 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) | if (Request.QueryString != null) { |
if (Request.QueryString != null) { |
Case 结构 | Case FirstName of ‘John’: ... ‘Paul’: … ‘Ringo’: … End | Select (FirstName) ... case "Paul" : | switch (FirstName){ | 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
| 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 Do While I < 3 Console.WriteLine(i.ToString()); I = I + 1 | int i = 0; { } | int i = 0; { } |
类别 | 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
End Class
| using System;
{
{ int x; public void Add(int x) { this.x += x; } public int GetNum() { return x; } }
|
import java.lang.*;
package MySpace;
public class Foo extends Bar{
private int x; //私有变量 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) ... ‘ByVal 在VB中对象是是缺省参数传递方式 | void Button1_Click(Object sender, EventArgs E) { | jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event Button1_Click(evt); } });
private void Button1_Click(java.awt. event } |
张弓 2007/4/21 于台中, 2008/4/18 补充加入java