• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            M-A-T Tory's Blog

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              16 隨筆 :: 1 文章 :: 1 評論 :: 0 Trackbacks
            1. 隨機(jī)數(shù)
            ? // Create a random number generator,
            ?? // seeds with current time by default:

            Random?rand?=?new?Random();

            ????
            int?i,?j,?k;
            ???
            //?Choose?value?from?1?to?100:
            ????j?=?rand.nextInt(100)?+?1;
            2. Aliasing during method calls
            class?Letter?{
            ??char?c;
            }

            public?
            class?PassObject?{
            ????static?void?f(Letter?y)?{
            ????y.c?
            =?'z';
            ??}
            ??public?static?void?main(String[]?args)?{
            ????Letter?x?
            =?new?Letter();
            ????x.c?
            =?'a';
            ????System.out.println(
            "1:?x.c:?"?+?x.c);
            ????f(x);
            //result:"1:?x.c:?a",
            //??????"2:?x.c:?z"
            }
            }

            Passing objects to methods may not be what you're used to. It just pass a reference
            3.Also reference
            ????????Integer?n1?=?new?Integer(47);
            ??????Integer?n2?
            =?new?Integer(47);
            ????????System.out.println(n1?
            ==?n2);
            ????????
            //result??false
            If you want to compare the actual contents of an object for equivalence. You must use the special method equals(?) that exists for all objects .REMEMBER the default behavior of equals(?) is to compare references.So the result of the fellowing code is false
            class?Value?{
            ??int?i;
            }

            public?
            class?EqualsMethod2?{
            ???public?static?void?main(String[]?args)?{
            ????Value?v1?
            =?new?Value();
            ????Value?v2?
            =?new?Value();
            ????v1.i?
            =?v2.i?=?100;
            ????System.out.println(v1.equals(v2));
            ??
            ??}
            }?
            ///:~

            4.Here’s an example that demonstrates the use of all the operators involving bits:
            static?void?printBinaryInt(String?s,?int?i)?{
            ????System.out.println(
            ??????s?
            +?",?int:?"?+?i?+?",?binary:?");
            ????System.out.print(
            "???");
            ????
            for(int?j?=?31;?j?>=?0;?j--)
            ??????
            if(((1?<<?j)?&??i)?!=?0)
            ????????System.out.print(
            "1");
            ??????
            else
            ????????System.out.print(
            "0");
            ????System.out.println();
            ??}

            ??
            static?void?printBinaryLong(String?s,?long?l)?{
            ????System.out.println(
            ??????s?
            +?",?long:?"?+?l?+?",?binary:?");
            ????System.out.print(
            "???");
            ????
            for(int?i?=?63;?i?>=?0;?i--)
            ??????
            if(((1L?<<?i)?&?l)?!=?0)
            ????????System.out.print(
            "1");
            ??????
            else
            ????????System.out.print(
            "0");
            ????System.out.println();
            ??}

            Since then goto-bashing has been a popular sport, with advocates? of the cast-out keyword scurrying for cover.

            As is typical in situlations like this, the middle ground is the most fruitfull.

            5.Here is a demonstration of labeled break and continue statements with whileloops:

            import?com.bruceeckel.simpletest.*;

            public?class?LabeledWhile?{
            ??
            static?Test?monitor?=?new?Test();
            ??
            public?static?void?main(String[]?args)?{
            ????
            int?i?=?0;
            ????outer:
            ????
            while(true)?{
            ??????System.out.println(
            "Outer?while?loop");
            ??????
            while(true)?{
            ????????i
            ++;
            ????????System.out.println(
            "i?=?"?+?i);
            ????????
            if(i?==?1)?{
            ??????????System.out.println(
            "continue");
            ??????????
            continue;
            ????????}

            ????????
            if(i?==?3)?{
            ??????????System.out.println(
            "continue?outer");
            ??????????
            continue?outer;
            ????????}

            ????????
            if(i?==?5)?{
            ??????????System.out.println(
            "break");
            ??????????
            break;
            ????????}

            ????????
            if(i?==?7)?{
            ??????????System.out.println(
            "break?outer");
            ??????????
            break?outer;
            ????????}

            ??????}

            ????}

            ????monitor.expect(
            new?String[]?{
            ??????
            "Outer?while?loop",
            ??????
            "i?=?1",
            ??????
            "continue",
            ??????
            "i?=?2",
            ??????
            "i?=?3",
            ??????
            "continue?outer",
            ??????
            "Outer?while?loop",
            ??????
            "i?=?4",
            ??????
            "i?=?5",
            ??????
            "break",
            ??????
            "Outer?while?loop",
            ??????
            "i?=?6",
            ??????
            "i?=?7",
            ??????
            "break?outer"
            ????}
            );
            ??}

            }
            ?///:~

            RULES:

            1. A plain continue goes to the top of the innermost loop and continues.
            2. A labeled continue goes to the label and reenters the loop right after that label.
            3. A break “drops out of the bottom” of the loop.
            4. A labeled break drops out of the bottom of the end of the loop denoted by the label.

            6. Even differences in the ordering of arguments are sufficient to distinguish two methods.
            ?
            7. Calling constructors from constructors

            public?class?Flower?{
            ??
            ??
            int?petalCount?=?0;
            ??String?s?
            =?new?String("null");
            ??Flower(
            int?petals)?{
            ????petalCount?
            =?petals;
            ????System.out.println(
            ??????
            "Constructor?w/?int?arg?only,?petalCount=?"
            ??????
            +?petalCount);
            ??}

            ??Flower(String?ss)?
            {
            ????System.out.println(
            ??????
            "Constructor?w/?String?arg?only,?s="?+?ss);
            ????s?
            =?ss;
            ??}

            ??Flower(String?s,?
            int?petals)?{
            ????
            this(petals);
            //!????this(s);?//?Can't?call?two!
            ????this.s?=?s;?//?Another?use?of?"this"
            ????System.out.println("String?&?int?args");
            ??}

            ??Flower()?
            {
            ????
            this("hi",?47);
            ????System.out.println(
            "default?constructor?(no?args)");
            ??}

            ??
            void?print()?{
            //!?this(11);?//?Not?inside?non-constructor!
            ????System.out.println(
            ??????
            "petalCount?=?"?+?petalCount?+?"?s?=?"+?s);
            ??}

            }
            9. equal() and "=="
            String?name1?=?new?String("Tory");
            Stirng?name2?
            =?new?String("Tory");

            //兩個對象的引用,不相等
            System.out.println(?name1?==?name2?);
            //內(nèi)容,相等
            System.out.println(?name1.equals(name2));

            String?name3?
            =?"Tory";
            String?name4?
            =?"Tory";

            //相同常量的引用,相等
            System.out.println(?name3?==?name4?);
            //內(nèi)容,相等
            System.out.println(?name3.equals(name4));
            10.自己的equals()方法
            class?MyDate?{

            ????
            int?day,?month,?year;
            ????
            ????
            public?MyDate?(int?day,?int?month,?int?year){
            ????
            ????????
            this.day?=?day;
            ????????
            this.month?=?month;
            ????????
            this.year?=?year;
            ????????
            ????}
            ?

            }


            class?MyOkDate?extends?MyDate?{

            ????
            public?MyOkDate?(?int?day,?int?month,?int?year?)?{
            ????
            ????????
            super(day,?month,?year);
            ????????
            ????????}

            ????????
            ????????
            public?boolean?equals?(?Object?obj?)?{
            ????????
            ????????????
            if(?obj?instanceof?MyOkDate?)?{
            ????????????????MyOkDate?m?
            =?(MyOkDate)?obj;
            ????????????????
            if(m.day?==?day?&&?m.month?==?month?&&?m.year?==?year)
            ????????????????
            return?true;
            ????????????????
            ????????????}

            ????????????
            return?false;
            ????????}
            ?
            }


            public?class?TestEqualsObject{

            ????
            public?static?void?main?(?String?[]?args?){
            ????????
            ????????MyDate?m1?
            =?new?MyDate(4,?12,?1985);
            ????????MyDate?m2?
            =?new?MyDate(4,?12,?1985);
            ????????
            ????????
            //不相等
            ????????System.out.println(m1.equals(m2));
            ????????
            ????????m1?
            =?new?MyOkDate(4,?12,?1985);
            ????????m2?
            =?new?MyOkDate(4,?12,?1985);
            ????????
            ????????
            //相等
            ????????System.out.println(m1.equals(m2));
            ?????????
            ????}

            ????
            }
            posted on 2006-04-24 22:36 Tory 閱讀(325) 評論(0)  編輯 收藏 引用 所屬分類: Java Learning
            久久91精品综合国产首页| 久久不射电影网| 国产69精品久久久久观看软件| 性高朝久久久久久久久久| 无码国内精品久久综合88| 亚洲欧美日韩中文久久| 国产亚洲欧美成人久久片| 久久久久久青草大香综合精品| 免费无码国产欧美久久18| 国产亚洲欧美精品久久久| 办公室久久精品| 久久精品国产亚洲AV嫖农村妇女 | 人人狠狠综合久久亚洲高清| 中文字幕日本人妻久久久免费 | 欧美日韩成人精品久久久免费看| 久久精品人妻中文系列| 国产 亚洲 欧美 另类 久久| 亚洲中文久久精品无码| 久久久久亚洲AV无码专区桃色| 蜜臀久久99精品久久久久久小说| 久久精品国产精品亚洲下载| 久久精品天天中文字幕人妻| 91麻豆国产精品91久久久| 草草久久久无码国产专区| 久久精品国产亚洲av高清漫画| 伊人 久久 精品| 日韩精品无码久久一区二区三| 国产精久久一区二区三区| 久久99国产综合精品| 婷婷久久久亚洲欧洲日产国码AV| 欧美久久亚洲精品| 色天使久久综合网天天| 久久九九久精品国产| 亚洲精品高清国产一久久| 久久狠狠色狠狠色综合| 久久精品一区二区| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 狠狠色婷婷久久一区二区| 欧美亚洲国产精品久久| 久久人人爽人人爽人人片AV东京热 | 久久亚洲国产最新网站|