SUNWEN教程之----C#进阶11

来源:互联网 发布:数据库实时同步软件 编辑:程序博客网 时间:2024/06/05 06:18
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
SUNWEN教程之----C#进阶
(十一)
mrfat@china.com  
OK,我又回来了,前面写了那么多,跟JAVA的对比也够多了,现在我就用我自己试验的例子,来看一看C#这个被称为JAVA#的新一代编程语言到底与JAVA有多大的区别.

首先我们建立一个C#的程序文件cs1.cs,然后再建立一个JAVA的源程序文件cs1.java.它们的内容分别是:

cs1.cs:

using System;
public class cs1{
    public static void Main(){
       Console.WriteLine("i am SUNWEN!");
       SUNWEN mySUNWEN=new SUNWEN();
       Console.WriteLine(mySUNWEN.name);
       }
}


class SUNWEN{
    public String name="chenbin!";
}

cs1.java:

import System.*;

public class cs1{
    public static void main(String args[]){
        System.out.println("i am SUNWEN,how are you!");
        SUNWEN mySUNWEN=new SUNWEN();
        System.out.println(mySUNWEN.name);
    }
}

class SUNWEN{
    String name="chenbin!";
}


OK,让我们来运行一下这两个程序.在编译过程中我们发现,C#的速度的确比JAVA要快好多.(不是说M$的好话)其实,两个程序的输出是一样的,都是:

i am SUNWEN!
chenbin!

有一个重要的区别就是看有一行是publc String name="chenbin!",而在JAVA中却是String name="chenbin!".如果我们在cs1.cs中把这个public去掉,就会产生错误,因为在C#中,不用任何范围修饰符时,默认的是protect,因而不能在类外被访问.

这是一个重要的区别之一.还有就是:如果我们把cs1.cs这个C#程序中的一句public class cs1改为public class cs2,存盘,再编译,可以看到,程序正常运行.而在JAVA中,这显然不行,因为JAVA规定,在一个文件中只能有一个public类,而且这个类的名称必须与文件名一模一样.这又是一个区别,在C#中,它是以Main方法来定位入口的.如果一个程序中没有一个名为Main的方法,就会出"找不到入口的错误".不要把Main写成main哟,嘻嘻,我经常犯这样的错误.

好了,就说到这了,以后再遇到再说吧!

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击