<pre>
軟件出現(xiàn)的目的
用計算機(jī)的語言描述現(xiàn)實(shí)世界
用計算機(jī)解決現(xiàn)實(shí)世界的問題
為什么使用面向?qū)ο?/h3>
世界由對象組成
面向?qū)ο蟮乃枷?描述 面向?qū)ο蟮氖澜?符合人類思維習(xí)慣

從現(xiàn)實(shí)中抽象出類分三步:
找出它的種類
找出它的屬性
找出它的行為
用面向?qū)ο竺枋鍪澜?/h3>
第一步:發(fā)現(xiàn)類
class Dog {
}
根據(jù)“對象”抽象出“類”
第二步:發(fā)現(xiàn)類的屬性
狗類共有的特征:
品種
年齡
昵稱
健康情況
-
跟主人的親密度
… …
class Dog {
String name = "旺財"; // 昵稱
int health = 100; // 健康值
int love = 0; // 親密度
String strain = "拉布拉多犬"; // 品種
}
只放和業(yè)務(wù)相關(guān)的屬性
第三步:發(fā)現(xiàn)類的方法
狗類共有的行為:
跑
吠
-
輸出自己的信息
… …
class Dog {
String name = "旺財"; // 昵稱
int health = 100; // 健康值
int love = 0; // 親密度
String strain = "拉布拉多犬"; // 品種
/* 輸出狗的信息 */
public void print() {
// 輸出狗信息的代碼
}
}
只放和業(yè)務(wù)相關(guān)的方法
使用類圖描述類

實(shí)踐
實(shí)現(xiàn)領(lǐng)養(yǎng)寵物功能
編寫寵物類Dog和Penguin
創(chuàng)建寵物對象,輸入領(lǐng)養(yǎng)的寵物信息并輸出


對象初始化
Penguin pgn = new Penguin();
pgn.name = "qq";
pgn.sex = "Q仔";
能否在創(chuàng)建對象的同時就完成賦值?
使用構(gòu)造方法:
Penguin pgn1 = new Penguin();
class Penguin {
// 屬性
/* 無參構(gòu)造方法 */
public Penguin() {
name = "qq";
love = 20;
sex = "Q仔";
System.out.println("執(zhí)行構(gòu)造方法");
}
}
構(gòu)造方法

系統(tǒng)提供默認(rèn)無參構(gòu)造方法
public Penguin() {
}
自定義構(gòu)造方法
public Penguin () {
name = "qq";
love = 20;
sex = "Q仔";
}
public Penguin (String name,int health,int love,String sex ) {
this.name = name;
this.health = health;
this.love = love;
this.sex = sex;
}
系統(tǒng)不再提供默認(rèn)無參構(gòu)造方法
this關(guān)鍵字是對一個對象的默認(rèn)引用,這里用以區(qū)分同名成員變量
方法重載

System.out.println(45);
System.out.println(true);
System.out.println("狗在玩耍!");
調(diào)用重載方法
pgn = new Penguin();
pgn.print();
pgn = new Penguin("美美", 80, 20, "Q仔");
pgn.print();
一個例子
class Penguin {
String name = null; //昵稱
int health = 0; // 健康值
String sex = null; // 性別
public void Penguin() {
health=10;
sex="雄";
System.out.println("執(zhí)行構(gòu)造方法");
}
public void print() {
System.out.println("企鵝的名字是" + name + ",健康值是"
+ health + ",性別是" + sex);
}
}
Penguin pgn3= new Penguin();
pgn3.print();

找出下面代碼的問題
class Dog {
private String name = "旺財"; // 昵稱
private int health = 100; // 健康值
private int love = 0; // 親密度
public void play(int n) {
int localv;
health = health - n;
System.out.println(name+" "+ localv +" "+health+" "+love);
}
public static void main(String[] args) {
Dog d=new Dog();
d.play(5);
}
}
static靜態(tài)成員
一個例子 統(tǒng)計對象被創(chuàng)建出來的個數(shù)
class Person
{
public String name;
public int age;
static public long all_count;
public Person(){
all_count++;
}
public Person( String name , int age ){
all_count++;
this.name = name;
this.age = age;
}
// 統(tǒng)計人數(shù)的函數(shù)
public long getCount(){
return all_count;
}
// 應(yīng)該具備找同齡人的功能
public boolean isSameAge( Person p1 ){
return this.age == p1.age;
}
}
class Demo9
{
public static void main(String[] args)
{
Person p1 = new Person( "jame" , 34 );
Person p2 = new Person( "lucy" , 34 );
Person p3 = new Person( "lili" , 34 );
Person p4 = new Person();
System.out.println( p1.getCount() + " " + p2.getCount() + " " + p3.getCount() );
System.out.println( p1.isSameAge( p2 ) );
System.out.println( p1.isSameAge( p3 ) );
}
}
4:static特點(diǎn)
1 隨著類的加載而加載,靜態(tài)會隨著類的加載而加載,隨著類的消失而消失。說明它的生命周期很長。
2 優(yōu)先于對象存在。—>靜態(tài)是先存在,對象是后存在。
3 被所有實(shí)例(對象)所共享。
4 可以直接被類名調(diào)用

使用static定義方法
用類名調(diào)用: Person.print();
靜態(tài)方法只能訪問靜態(tài)屬性,不能訪問實(shí)例屬性
找錯誤
class Dog {
private String name = "旺財"; // 昵稱
private int health = 100; // 健康值
private int love = 0; // 親密度
public void play(int n) {
static int localv=5;
health = health - n;
System.out.println(name+" "+localv+" "+health+" "+love);
}
public static void main(String[] args) {
Dog d=new Dog();
d.play(5);
}
}
封裝
Dog d = new Dog();
d.health = -1000;
屬性隨意訪問,不合理的賦值
封裝的概念
封裝:將類的某些信息隱藏在類內(nèi)部,不允許外部程序直接訪問,而是通過該類提供的方法來實(shí)現(xiàn)對隱藏信息的操作和訪問
封裝的好處
1.隱藏類的實(shí)現(xiàn)細(xì)節(jié)
2.只能通過規(guī)定方法訪問數(shù)據(jù)
3.方便加入控制語句
4.方便修改實(shí)現(xiàn)
封裝的步驟

class Dog {
private String name = "旺財"; // 昵稱
private int health = 100; // 健康值
private int love = 0; // 親密度
private String strain = "拉布拉多犬"; // 品種
public int getHealth() {
return health;
}
public void setHealth (int health) {
if (health > 100 || health < 0) {
this.health = 40;
System.out.println("健康值應(yīng)該在0和100之間,默認(rèn)值是40");
} else
this.health = health;
}
// 其它getter/setter方法
}
this
用類名定義一個變量(對象,實(shí)例)的時候,定義的只是一個引用,外面可以通過這個引用來訪問這個類里面的屬性和方法。
那么類里面是夠也應(yīng)該有一個引用來訪問自己的屬性和方法呢?
JAVA提供了一個很好的東西,就是 this 對象,它可以在類里面來引用這個類的屬性和方法。
先來個簡單的例子:
public class ThisDemo {
String name="Mick";
public void print(String name){
System.out.println("類中的屬性 name="+this.name);
System.out.println("局部傳參的屬性="+name);
}
public static void main(String[] args) {
ThisDemo tt=new ThisDemo();
tt.print("Orson");
}
}
關(guān)于返回類自身的引用,《Thinking in Java》有個很經(jīng)典的例子。
通過this 這個關(guān)鍵字返回自身這個對象然后在一條語句里面實(shí)現(xiàn)多次的操作
public class ThisDemo {
int number;
ThisDemo increment(){
number++;
return this;
}
private void print(){
System.out.println("number="+number);
}
public static void main(String[] args) {
ThisDemo tt=new ThisDemo();
tt.increment().increment().increment().print();
}
}
一個類中定義兩個構(gòu)造函數(shù),在一個構(gòu)造函數(shù)中通過 this 這個引用來調(diào)用另一個構(gòu)造函數(shù)
public class ThisDemo {
String name;
int age;
public ThisDemo (){
this.age=21;
}
public ThisDemo(String name){
this();
this.name="Mick";
}
private void print(){
System.out.println("最終名字="+this.name);
System.out.println("最終的年齡="+this.age);
}
public static void main(String[] args) {
ThisDemo tt=new ThisDemo("zhangsan"); //隨便傳進(jìn)去的參數(shù)
tt.print();
}
}
練習(xí)
創(chuàng)建Dog類
編寫Test類

創(chuàng)建Penguin類
編寫Test類
