數(shù)據(jù)加載中……

          2008年3月20日

          練習(xí)題,長方形求周長和面積

          定義一個長方形,求它的周長和面積。用面向?qū)ο蟮姆椒ā?

          ?

          class 長方形 {

          ????int ;int ;

          ????int 周長()

          ????{

          ????????return 2*(+);

          ????}

          ????int 面積()

          ????{

          ????????return *;

          ????}????????

          ????public static void main(String[] args)

          ????{

          ????????長方形 chang1=new 長方形();

          ????????長方形 chang2=new 長方形();

          ????????chang1.=10;

          ????????chang1.=5;

          ????????System.out.println("周長="+chang1.周長());

          ????????System.out.println("面積="+chang1.面積());

          ????????chang2.=20;

          ????????chang2.=8;

          ????????System.out.println("周長="+chang2.周長());

          ????????System.out.println("面積="+chang2.面積());

          ????}

          }

          posted @ 2008-03-20 11:59 rick 閱讀(1624) | 評論 (0)編輯 收藏
          繼承

          public class Animal

          {

          ????int height;

          ????int weight;

          ????void animal()

          ????{

          ????????System.out.println("Animal constract");

          ????}

          ????void eat()

          ????{

          ????????System.out.println("Animal eat");

          ????}

          ????void sleep()

          ????{

          ????????System.out.println("Animal sleep");

          ????}

          ????void breathe()

          ????{

          ????????System.out.println("Animal breathe");

          ????}

          }

          /*

          * 理解繼承是理解面向?qū)ο蟪绦蛟O(shè)計的關(guān)鍵

          * 在java中,通過關(guān)鍵字extends繼承一個已有的類,被繼承的類稱為父類(超類,基類),新的類稱為子類(派生類)。

          * * 在java中,不允許多繼承

          */

          class Fish extends Animal

          {

          ????void fish()

          ????{

          ????????

          ????????System.out.println("fish constract");

          ????}

          ????void breathe()

          ????{

          ????????//super.breathe();

          ????????//super.height=40;

          ????????System.out.println("fish boo");

          ????}

          }

          class Integration

          {

          ????public static void main(String[]args)

          ????{

          ????????//Animal an=new Animal();

          ????????Fish fh=new Fish();

          ????????//an.breathe();

          ????????//fh.height=30;

          ????????fh.breathe();

          ????????

          ????}

          }

          /*

          *在子類當中定義一個與父類同名,返回類型,參數(shù)類型均一致的方法,稱為方法的覆蓋

          *方法的覆蓋發(fā)生在子類和父類之間。

          *調(diào)用父類的方法使用super

          */

          /*特殊變量super,提供了父類的訪問

          * 可以使用super訪問被父類被子類隱藏的變量或覆蓋的方法

          * 每個子類構(gòu)造方法的第一句,都是隱藏的調(diào)用super(),如果父類沒有這種形式的構(gòu)造函數(shù),那么在編譯器中就會報錯。

          *

          *

          *

          */

          posted @ 2008-03-20 11:58 rick 閱讀(197) | 評論 (0)編輯 收藏

          2008年3月5日

          關(guān)鍵字static和final

          靜態(tài)方法和靜態(tài)變量是屬于某一個類,而不屬于類的對象。

          靜態(tài)方法和靜態(tài)變量的引用直接通過類名引用。

          在靜態(tài)方法中不能調(diào)用非靜態(tài)的方法和引用非靜態(tài)的成員變量。反之,則可以。

          可以用類的對象obj去調(diào)用靜態(tài)的方法method(),如:obj.method()。

          ?

          Final在聲明時需要進行初始化。

          使用關(guān)鍵字final定義常量,例如:final double PI=3.1415926

          作為一種約定,在定義常量時,通常采用大寫的形式。

          Final常量可以在聲明的同時賦初值,也可以在構(gòu)造函數(shù)中賦初值。

          為了節(jié)省內(nèi)存,我們通常將常量聲明為靜態(tài)的(static)

          ?

          在聲明為static時,就要在聲明final常量時進行初始化。

          static final double //PI=3.1415926;

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????PI=3.1415926;

          ????????x=a;

          ????????y=b;

          ????}

          這種方式是錯誤的。

          正確的方法如下:

          static final double PI=3.1415926;

          ?

          posted @ 2008-03-05 17:13 rick 閱讀(274) | 評論 (0)編輯 收藏
          特殊變量this

          This變量表示成員對象本身。

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????point()

          ????{????????

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????void output(int x,int y)

          ????{

          ????????this.x=x;

          ????????this.y=y;

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????/*pt=new point();

          ????????{

          ????????????

          ????????????pt.output();????????????

          ????????}*/

          ????????pt=new point(3,3);

          ????????{

          ????????????pt.output(5,5);

          ????????????pt.output();

          ????????}

          ????}

          }

          當類中有2個同名變量,一個屬于類(類的成員變量),而另一個屬于某個特定的方法(方法中的局部變量),使用this區(qū)分成員變量和局部變量。

          使用this簡化構(gòu)造函數(shù)的調(diào)用。

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????point()

          ????{????

          ????????this(1,1);

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????void output(int x,int y)

          ????{

          ????????this.x=x;

          ????????this.y=y;

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point();

          ????????pt.output();

          ????}

          }

          我們使用一個不帶參數(shù)的構(gòu)造方法來調(diào)用帶參數(shù)的構(gòu)造方法,在不帶參數(shù)的構(gòu)造方法中使用this(1,1);this本身表示pt對象,他調(diào)用帶參數(shù)的成員方法,來給x和y賦值。大大簡化了調(diào)用方法。

          在一個類中所有的實例(對象)調(diào)用的成員方法在內(nèi)存中只有一份拷貝,盡管在內(nèi)存中可能有多個對象,而數(shù)據(jù)成員(實例變量,成員變量)在類的每個對象所在的內(nèi)存中都存在著一份拷貝。This變量允許相同的實例方法為不同的對象工作。每當調(diào)用一個實例方法時,this變量將被設(shè)置成引用該實例方法的特定的類對象。方法的代碼接著會與this所代表的對象的特定數(shù)據(jù)建立關(guān)聯(lián)。

          posted @ 2008-03-05 13:57 rick 閱讀(181) | 評論 (0)編輯 收藏
          面向?qū)ο蟮姆椒?/a>

          面向?qū)ο蟮姆椒ㄒ冉ㄒ粋€類,這個類相當于一個模板,然后要為這個類實例化一個對象。然后對這個對象才能進行操作。

          類具有狀態(tài)和行為的方式。

          狀態(tài)就像人這個類的狀態(tài)有身高和體重,行為有吃飯這個行為。

          下面用一個點來說明

          public class point

          {

          ????int x,y;

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point();

          ????????{

          ????????????pt.x=10;

          ????????????pt.y=10;

          ????????????pt.output();????????????

          ????????}

          ????}

          }

          構(gòu)造函數(shù),構(gòu)造函數(shù)和類的方法類似。構(gòu)造方法的名字和類名相同,并且沒有返回值,構(gòu)造方法主要為類的對象定義初始化狀態(tài)。

          我們不能直接調(diào)用構(gòu)造函數(shù),只能通過new關(guān)鍵字來調(diào)用從而創(chuàng)建類的實例

          Java的類都要求有構(gòu)造方法,如果沒有定義構(gòu)造方法,則java會默認使用一個缺省的方法,就是不帶參數(shù)的方法。

          public class point

          {

          ????int x,y;

          ????point()

          ????{

          ????????x=5;

          ????????y=10;

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point();

          ????????{

          ????????????pt.output();????????????

          ????????}

          ????}

          }

          對于構(gòu)造方法,還可以使用參數(shù)的方法,在實例化對象的時候,直接傳遞參數(shù)就可以了

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point(3,3);

          ????????{

          ????????????pt.output();????????????

          ????????}

          ????}

          }

          New關(guān)鍵字的作用

          為對象分配內(nèi)存空間。

          引起對象構(gòu)造方法的調(diào)用。

          為對象返回一個引用。

          ?

          各種數(shù)據(jù)類型的默認值是:

          數(shù)值型: 0

          Boolean: false

          Char: "\0"

          對象: null

          ?

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point(3,3);

          ????????{

          ????????????pt.output();????????????

          ????????}

          ????}

          }

          輸出是0 0

          帶參數(shù)的構(gòu)造方法和不帶參數(shù)的構(gòu)造方法可以同時使用。只要參數(shù)類型或參數(shù)個數(shù)不同。在調(diào)用是是通過對指定參數(shù)類型和參數(shù)個數(shù)的方法來調(diào)用哪個構(gòu)造方法。

          ?

          ?

          這就是方法的重載(overload):重載構(gòu)成的條件:方法的名稱相同,但參數(shù)類型或參數(shù)個數(shù)不同,才能構(gòu)成方法的重載。

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????point()

          ????{

          ????????

          ????}

          ????

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point();

          ????????{

          ????????????pt.output();????????????

          ????????}

          ????????/*pt=new point(3,3);

          ????????{

          ????????????pt.output();

          ????????}*/

          ????}

          }

          這2種方法都是可以使用的。

          2008年2月28日

          移位運算符

          Java中有3個移位運算符

          左移: <<

          帶符號右移:>>

          無符號右移:>>>

          數(shù) x x<<2 x>>2 x>>>2

          17 00010001 01000100 00000100 00000100

          -17 11101111 10111100 11111011 00111011

          看一下程序0x表示16進制。ffffffff表示-1

          public class test {

          ????public static void main(String[] args) {

          ????????int i=0xffffffff;

          ????????int c=i<<2;

          ????????System.out.println(i);

          ????????System.out.println(c);

          ????????}

          }

          輸出是-1和-4.這表示

          public class test {

          ????public static void main(String[] args) {

          ????????int i=0xffffffff;

          ????????int c=i<<2;

          ????????System.out.println(Integer.toHexString(i));

          ????????System.out.println(Integer.toHexString(c));

          ????????}

          }

          使用Integer.toHexString()將10進制轉(zhuǎn)換位16進制。

          輸出位ffffffff 和fffffffc左移2位最后補2個0,最后的1100轉(zhuǎn)換位16進制就是c

          ?

          public class test {

          ????public static void main(String[] args) {

          ????????int i=0xffffffff;

          ????????int c=i>>>2;

          ????????System.out.println(Integer.toHexString(i));

          ????????System.out.println(Integer.toHexString(c));

          ????????}

          }

          無符號右移輸出是ffffffff和3fffffff 右移2位后最左段是0011,轉(zhuǎn)換位16進制就是3

          ?

          練習(xí):

          將一個整數(shù)110從右端開始的4到7位變?yōu)?.

          答:要想將4到7位變?yōu)?,先構(gòu)造一個4到7位是0的數(shù),然后用110和這個數(shù)與&。任何數(shù)和0與都是0,就完成了任務(wù)。要構(gòu)造一個4到7位是0的數(shù),先構(gòu)造一個低4位是1的數(shù)15,然后將它左移3位,然后取反,就構(gòu)造成4到7位是0的數(shù)了。程序如下。

          public class test {

          ????public static void main(String[] args) {

          ????????int i=15;

          ????????int j=i<<3;

          ????????int c=~j;

          ????????int k=110;

          ????????int l=k&c;

          ????????System.out.println(Integer.toBinaryString(i));

          ????????System.out.println(Integer.toBinaryString(j));

          ????????System.out.println(Integer.toBinaryString(c));

          ????????System.out.println(Integer.toBinaryString(k));

          ????????System.out.println(Integer.toBinaryString(l));

          ????????}

          }

          輸出是

          00001111

          01111000

          10000111

          01101110

          00000110

          ?

          將一個數(shù)左移一位等于是將數(shù)*2,右移一位相當于將數(shù)/2。

          左移2位相當與乘兩次2,右移2位相當與除兩次2.

          posted @ 2008-02-28 17:15 rick 閱讀(522) | 評論 (2)編輯 收藏
          位運算

          & 按位與 都真才真

          01101101

          &

          00110111

          00100101

          | 按位或 只要1個真就真,都假才假

          01101101

          |

          00110111

          01111111

          ^按位異或 一真一假才為真,都真都假就是假。

          01101101

          ^

          00110111

          01011010

          ~按位取反 0變1,1變0

          ~01101101

          10010010

          posted @ 2008-02-28 15:47 rick 閱讀(188) | 評論 (0)編輯 收藏
          原碼,反碼,和補碼

          在java中一個字節(jié)由8個二進位組成。

          計算機中有原碼,反碼,和補碼。

          原碼

          將最高為作為符號位0正,1負。其余各位表示數(shù)值的絕對值。

          +7的原碼是 00000111

          -7的原碼是 10000111

          原碼的問題是+0和-0的表示

          +0是00000000

          -0是 10000000

          2個數(shù)值不同。

          反碼

          一個數(shù)如果為正,則它的反碼與原碼相同,如果為負,則符號位為1,其余取反。

          +7是00000111

          -7 是 11111000

          反碼的問題也是+0和-0的問題

          +0是 00000000

          -0 是 11111111

          2個數(shù)值不同。

          補碼

          利用溢出,我們將減法變成加法。 一個數(shù)如十進制,一個字節(jié)一個數(shù),有8位,超過就進一。

          一個數(shù)為正,則它的原碼,反碼,補碼相同。如果為負,則符號位為1,其余對原碼取反。然后加1

          +7是 00000111

          -7是 11111001

          +0是00000000

          -0是100000000,然后丟掉最高的9位則為00000000

          和+0的表示相同,所以計算機中是采用的是補碼。

          已知一個負數(shù)的補碼,轉(zhuǎn)換為十進制。

          1. 先對各位取反。
          2. 將其轉(zhuǎn)換為十進制數(shù)
          3. 加上負號,再減去1.

          如 11111010 先取反

          00000101 轉(zhuǎn)換為十進制

          是5 加上負號

          是-5 再減去1

          是-6

          posted @ 2008-02-28 15:02 rick 閱讀(445) | 評論 (0)編輯 收藏
          控制語句for

          public class Welcome {

          ????public static void main(String[] args) {

          ????????for(int i=0;i<10;i++)

          ????????{System.out.println(i);

          ????????}

          ????}

          }

          對于java中for語句中定義的變量的作用域只在{}內(nèi)。For以外不能訪問。這點和c語言不同,知道就可以了。

          posted @ 2008-02-28 11:39 rick 閱讀(172) | 評論 (0)編輯 收藏
          Java中的++操作

          I++ 是先取出i的值,然后參與運算+1。

          public class Welcome {

          ????public static void main(String[] args) {

          ????????int i=3;

          ????????System.out.println(i++);

          ????????}

          }

          輸出為3

          ++i是先+1,然后參與運算。

          public class Welcome {

          ????public static void main(String[] args) {

          ????????int i=3;

          ????????System.out.println(++i);

          ????????}

          }

          輸出為4

          下面這個例子說明了這個道理

          public class Welcome {

          ????public static void main(String[] args) {

          ????????int i=3;

          ????????int count=(i++)+(i++)+(i++);

          ????????System.out.println(i);

          ????????System.out.println(count);

          ????????}

          }

          輸出為6和12,i取了3次是6,count是3+4+5=12

          public class Welcome {

          ????public static void main(String[] args) {

          ????????int i=3;

          ????????int count=(++i)+(++i)+(++i);

          ????????System.out.println(i);

          ????????System.out.println(count);

          ????????}

          }

          輸出為6和15,i取了3次是6,count是4+5+6=15

          posted @ 2008-02-28 11:28 rick 閱讀(324) | 評論 (0)編輯 收藏
          僅列出標題  下一頁
          主站蜘蛛池模板: 丽水市| 奉化市| 惠州市| 新绛县| 密山市| 武定县| 抚远县| 府谷县| 远安县| 长治市| 肥西县| 新化县| 玉屏| 屏东市| 嵊泗县| 政和县| 自治县| 凤台县| 嘉黎县| 平武县| 宁南县| 阳江市| 界首市| 佳木斯市| 定襄县| 乌鲁木齐市| 乳源| 康乐县| 禹城市| 龙井市| 沙湾县| 五指山市| 木兰县| 巴东县| 英吉沙县| 通江县| 高雄市| 容城县| 台州市| 闽清县| 临沂市|