• Contrasting C# and Java Syntax(摘录)


    http://www.ondotnet.com/pub/a/dotnet/2001/06/14/csharp_4_java.html

    1. 简单类型

    C#中的primitive type也是object的子类,因此可以直接调用ToString GetType

      IntSystem.Int32的假名,后者继承了System.Object

    注意:C#中简单类型仍然是pass-by-value

     2. 常量定义
    Javastatic final
    C#

    const int TWO = 2;

                  the keyword “readonly”(在声明处或构造函数中可以赋值,可以是动态赋值)

    3. switch语句中的差异

    C#中不允许fall-through, 否则会有编译错误。

    In C#, an explicit break or goto case to a different case is required somewhere in the case to explicitly define the control flow.

           switch(n)
            {
            case 1:

                cost += 25;

                break;

            case 2:

                cost += 25;

                goto case 1;

    4.特有的循环语句 foreach
    实现了System.Collections.IEnumerable 接口的object都可以使用foreach, array.

    5.C#中,所有的异常都是run-time exceptions; 都继承了System.Exception.

    6. 函数参数修饰符: params, ref, out

    (1) params(其实只是为了简化把数组作为函数参数的过程)

           方法的最后一个参数如果是数组,可以在这个参数前加params关键词。

    public void methodCaller( params int[] a );

    and the method can be called with any of

    methodCaller( 1 );

    methodCaller( 1, 2, 3, 4, 5 );

    Inside methodCaller, the parameters can be accessed through the "a" array defined.

    7. Property(对应于Java class里的一个属性及getter/setter)

    定义

    private int property;

    public int Property {

      get {

        return this.property;

      }

      set {

        // value is an implicit variable generated by the

        //  compiler to represent the parameter

        this.property = value;

      }

    }

    使用

    int currentValue = Property;

    Property = newValue;

    8. Extra Accessibility Modifier
    internal - this is a new one to Java programmers, as this means that a member is accessible from the entire assembly. All the objects you define inside a .cs file (you can define more than one object inside the .cs file, unlike Java, where you can usually define only one object) have a handle to the internal member.

     protected internal - think of this one as the union of protected and internal, as the item is is modifying is either protected or internal. This item can be accessed from the entire assembly, or within objects which derive from this class.

     9. C#中的类继承语法

    class D : B, C {

    // B,C可以是class,也可以时interface;如果是一个是class,一个是interfaceclass要放在interface前面。

     }

     10 C#中的Struct

        * structs are passed by value instead of by reference

        * structs cannot extend anything (nor can anything extend them) -- they may, however, implement interfaces

        * structs may not define a constructor with no parameters

        * structs that define constructors with parameters must explicitly define all fields before they return control to the caller

     11. C#中的base对应Java中的super

     12. Overriding Methods

     All methods in an object are "final" (in the Java sense of the word) by default. In order for a method to be overridden by an inheriting object, the original method needs to be marked "virtual" and all methods overriding the virtual method in the inherited classes must be marked "override." For example:

     public class A : Object {

      public virtual void toOverride() { }

     }

     public class B : A {

      public override void toOverride() { }

     }

     is required in C# for the compiler not to complain when defining the inheriting class.

     13. 类型转换

    public class FloorDouble : Object {

      private double value;

       public FloorDouble( double value ) {

        this.value = Math.Floor( value );

      }

       public double Value {

        get {

          return this.value;

        }
      }

       public static explicit operator FloorDouble( double value ) {

        return new FloorDouble( value );

      }

       public static implicit operator double( FloorDouble value ) {

        return value.Value;

      }

    }

     // this class can be used by

    FloorDouble fl = (FloorDouble)10.5

    double d = fl;

     14 操作符重载

     15. C#里的namespace

        (1) 不能引入单独的一个类

           (2) alias for long namespace

    using short = a.really.long.namespace.theClass;

           (3)不与文件系统中的结构对应
  • 相关阅读:
    POJ3928 Pingpong(统计比 K 小的个数 + 树状数组)
    C++ Primer Plus读书笔记
    HDU1698Just a Hook(线段树 + 区间修改 + 求和)
    POJ3468A Simple Problem with Integers(区间加数求和 + 线段树)
    POJ2528Mayor's posters(离散化 + 线段树)
    约瑟夫环
    编写一个JavaWeb项目
    四则运算在线答题系统
    JAVA项目中的常用的异常处理情况
    第八周动手动脑
  • 原文地址:https://www.cnblogs.com/yuquanlaobo/p/622839.html
Copyright © 2020-2023  润新知