2008年3月5日
定義一個長方形,求它的周長和面積。用面向對象的方法。
?
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.面積());
????}
}
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");
????}
}
/*
* 理解繼承是理解面向對象程序設計的關鍵
* 在java中,通過關鍵字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();
????????
????}
}
/*
*在子類當中定義一個與父類同名,返回類型,參數類型均一致的方法,稱為方法的覆蓋
*方法的覆蓋發生在子類和父類之間。
*調用父類的方法使用super
*/
/*特殊變量super,提供了父類的訪問
* 可以使用super訪問被父類被子類隱藏的變量或覆蓋的方法
* 每個子類構造方法的第一句,都是隱藏的調用super(),如果父類沒有這種形式的構造函數,那么在編譯器中就會報錯。
*
*
*
*/
靜態方法和靜態變量是屬于某一個類,而不屬于類的對象。
靜態方法和靜態變量的引用直接通過類名引用。
在靜態方法中不能調用非靜態的方法和引用非靜態的成員變量。反之,則可以。
可以用類的對象obj去調用靜態的方法method(),如:obj.method()。
?
Final在聲明時需要進行初始化。
使用關鍵字final定義常量,例如:final double PI=3.1415926
作為一種約定,在定義常量時,通常采用大寫的形式。
Final常量可以在聲明的同時賦初值,也可以在構造函數中賦初值。
為了節省內存,我們通常將常量聲明為靜態的(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;
?
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區分成員變量和局部變量。
使用this簡化構造函數的調用。
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();
????}
}
我們使用一個不帶參數的構造方法來調用帶參數的構造方法,在不帶參數的構造方法中使用this(1,1);this本身表示pt對象,他調用帶參數的成員方法,來給x和y賦值。大大簡化了調用方法。
在一個類中所有的實例(對象)調用的成員方法在內存中只有一份拷貝,盡管在內存中可能有多個對象,而數據成員(實例變量,成員變量)在類的每個對象所在的內存中都存在著一份拷貝。This變量允許相同的實例方法為不同的對象工作。每當調用一個實例方法時,this變量將被設置成引用該實例方法的特定的類對象。方法的代碼接著會與this所代表的對象的特定數據建立關聯。
面向對象的方法要先建一個類,這個類相當于一個模板,然后要為這個類實例化一個對象。然后對這個對象才能進行操作。
類具有狀態和行為的方式。
狀態就像人這個類的狀態有身高和體重,行為有吃飯這個行為。
下面用一個點來說明
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();????????????
????????}
????}
}
構造函數,構造函數和類的方法類似。構造方法的名字和類名相同,并且沒有返回值,構造方法主要為類的對象定義初始化狀態。
我們不能直接調用構造函數,只能通過new關鍵字來調用從而創建類的實例
Java的類都要求有構造方法,如果沒有定義構造方法,則java會默認使用一個缺省的方法,就是不帶參數的方法。
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();????????????
????????}
????}
}
對于構造方法,還可以使用參數的方法,在實例化對象的時候,直接傳遞參數就可以了
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關鍵字的作用
為對象分配內存空間。
引起對象構造方法的調用。
為對象返回一個引用。
?
各種數據類型的默認值是:
數值型: 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
帶參數的構造方法和不帶參數的構造方法可以同時使用。只要參數類型或參數個數不同。在調用是是通過對指定參數類型和參數個數的方法來調用哪個構造方法。
?
?
這就是方法的重載(overload):重載構成的條件:方法的名稱相同,但參數類型或參數個數不同,才能構成方法的重載。
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種方法都是可以使用的。