首页 | 主题 | 图库 | 问答 | 文摘 | 原创 | 百科

历史 | 地理 | 人物 | 艺术 | 体育 | 科学 | 音乐 | 电影 | 信息技术 | 世界遗产

 开放、中立,源自维基百科

个人工具


D語言

维库,知识与思想的自由文库

跳转到: 导航, 搜索

Image:03wiki-zn-frontpage-icon.gifD語言正在翻译。欢迎您积极翻译与修订
目前已翻译5%,原文在en:D (programming language)

D語言是根據C語言改造的編程語言之一,由Digital Mars公司的Walter Bright製作。Walter Bright也是Symantec C++編譯器的作者。

[编辑] 特徵

  • 可以任意地引進任何的文件。

[编辑] D語言中的Hello World程序

以下是D語言中的Hello World程序。

#!/usr/bin/dmd -run
/* sh style script syntax is supported! */
/* Hello World in D
 * To compile:
 *   dmd hello.d
 * or to optimize:
 *   dmd -O -inline -release hello.d
 * or to get generated documentation:
 *   dmd hello.d -D
 */


import
std.stdio;                        // References to  commonly used I/O routines.


int
main(char

[][] args)

{
    // 'writefln' (Write-Formatted-Line) is the type-safe 'printf'
    writefln("Hello World, "
            // automatic concatenation of string literals
             "Reloaded"

);

    // Strings are denoted as a dynamic array of chars 'char[]'
    // auto type inference and built-in foreach
    foreach

(argc, argv; args)

    {
        auto
cl = new
CmdLin(argc, argv);                      // OOP is supported
   writefln(cl.argnum, cl.suffix, " arg: %s"

, cl.argv); // user-defined class properties.


   delete
cl;                   // Garbage Collection or explicit memory management - your choice
    }

    // Nested structs, classes and functions
    struct
specs
    {
        // all vars automatically initialized to 0 at runtime
   int
count, allocated;
        // however you can choose to avoid array initialization
   int

[10000] bigarray = void

    }

    specs argspecs(char

[][] args)

    // Optional (built-in) function contracts.
    in
    {
        assert

(args.length > 0); // assert built in

    }
    out

(result)

    {
        assert

(result.count == CmdLin.total);

   assert

(result.allocated > 0);

    }
    body
    {
        specs* s = new
specs;
        // no need for '->'
   s.count = args.length;  // The 'length' property is number of elements.
   s.allocated = typeof

(args).sizeof; // built-in properties for native types

   foreach

(arg; args)

       s.allocated += arg.length * typeof

(arg[0]).sizeof;

   return
*s;
    }

    // built-in string and common string operations, eg. '~' is concatenate.
    char

[] argcmsg = "argc = %d"

    char

[] allocmsg = "allocated = %d"

    writefln(argcmsg ~ ", "
~ allocmsg,
            argspecs(args).count,argspecs(args).allocated);
    return
0;
}

/**
 * Stores a single command line argument.
 */
class
CmdLin
{
    private
    {
        int
_argc;
        char

[] _argv;

   static uint
_totalc;
    }

    public
   /**
         * Object constructor.
         * params:
         *   argc = ordinal count of this argument.
         *   argv = text of the parameter
         */
   this

(int

argc, char

[] argv)

   {
            _argc = argc + 1;
            _argv = argv;
            _totalc++;
        }

        ~this

() // Object destructor

   {
            // Doesn't actually do anything for this example.
   }

        int
argnum() // A property that returns arg number
   {
            return
_argc;
        }

        char

[] argv() // A property that returns arg text

   {
            return
_argv;
        }

        wchar

[] suffix() // A property that returns ordinal suffix

   {
            wchar

[] suffix; // Built in Unicode strings (UTF-8, UTF-16, UTF-32)

       switch

(_argc)

       {
                case
1:
                    suffix = "st"
               break
           case
2:
                    suffix = "nd"
               break
           case
3:
                    suffix = "rd"
               break
           default
// 'default' is mandatory with "-w" compile switch.
               suffix = "th"
       }
            return
suffix;
        }

        /**
         * A static property, as in C++ or Java,
         * applying to the class object rather than instances.
         * returns: The total number of commandline args added.
         */
   static
typeof

(_totalc) total()

   {
            return
_totalc;
        }

        // Class invariant, things that must be true after any method is run.
   invariant
   {
            assert

(_argc > 0);

       assert

(_totalc >= _argc);

   }
}

[编辑] 外部链接



電腦小作品 这是一个与计算机相关的小作品,您可以帮助维库扩充其内容。
其它语言
AD Links