天天看點

[零基礎學JAVA]Java SE面向對象部分-19.面向對象進階(07)

上季内容回顧:

代理設計、擴充卡設計 

抽象類和接口的差別

本季主要知識點:

本季以題目講解為主,詳細的講解了抽象類和接口的實際應用及典型的執行個體分析。

練習題一

<a href="http://redking.blog.51cto.com/attachment/200902/9/27212_1234192366Af4m.png"></a>

abstract class Employee    

{    

        private String name;    

        private int age;    

        private String sex;    

        public Employee(){}    

        public Employee(String name,int age,String sex)    

        {    

                this.setName(name);    

                this.setAge(age);    

                this.setSex(sex);    

        }    

        public void setName(String name)    

                this.name = name;    

        public void setAge(int age)    

                this.age = age;    

        public void setSex(String sex)    

                this.sex = sex;    

        public String getName()    

                return this.name;    

        public int getAge()    

                return this.age;    

        public String getSex()    

                return this.sex;    

        //顯示資料    

        public abstract String getInfo();    

}    

class Manager extends Employee    

        //職務    

        private String job;    

        //年薪    

        private float income;    

        public Manager(){}    

        public Manager(String name,int age,String sex,String job,float income)    

                super(name,age,sex);    

                this.setJob(job);    

                this.setIncome(income);    

        public void setJob(String job)    

                this.job = job;    

        public void setIncome(float income)    

                this.income = income;    

        public String getJob()    

                return this.job;    

        public float getIncome()    

                return this.income;    

        public String getInfo()    

                return "管理層資訊:"+"\n"    

                                +"\t|- 姓名: "+super.getName()+"\n"    

                                +"\t|- 年齡: "+super.getAge()+"\n"    

                                +"\t|- 性别: "+super.getSex()+"\n"    

                                +"\t|- 職務: "+this.getJob()+"\n"    

                                +"\t|- 年薪: "+this.getIncome();    

class Worker extends Employee    

        private String dept;    

        private float salary;    

        public Worker(){}    

        public Worker(String name,int age,String sex,String dept,float salary)    

                this.setDept(dept);    

                this.setSalary(salary);    

        public void setDept(String dept)    

                this.dept = dept;    

        public void setSalary(float salary)    

                this.salary = salary;    

        public String getDept()    

                return this.dept;    

        public float getSalary()    

                return this.salary;    

                return "職員資訊:"+"\n"    

                                +"\t|- 部門: "+this.getDept()+"\n"    

                                +"\t|- 月薪: "+this.getSalary();    

public class Demo01    

        public static void main(String args[])    

                Employee w = new Worker("張三",30,"男","技術部",5000.0f);    

                Employee m = new Manager("李四",35,"女","經理",10000.0f);    

                System.out.println(w.getInfo());    

                System.out.println(m.getInfo());    

<a href="http://redking.blog.51cto.com/attachment/200902/9/27212_1234192368AGw7.png"></a>

練習題二(重點來咯~~~)

<a href="http://redking.blog.51cto.com/attachment/200902/9/27212_1234192369k4sw.png"></a>

分析: 

  隻要是寵物則肯定可以向寵物商店中加入 

   貓  --&gt; 寵物 

   狗  --&gt; 寵物 

  寵物商店存放寵物

<a href="http://redking.blog.51cto.com/attachment/200902/9/27212_1234192370oHuA.png"></a>

五種寵物?如果說不是五種,可能是更多種了,那該如何? 

  5種寵物 --&gt; 5個接口對象 --&gt; 對象數組

//寵物    

interface Pet    

//寵物商店    

class PetShop    

//小狗    

class Dog implements Pet    

//小貓    

class Cat implements Pet    

}

寵物資訊:  

· 名字  

· 年齡 

· 顔色 

· 價錢 

還應該具備一個傳回全部資訊的方法。

我們繼續看下面哈~

        //傳回寵物的名字    

        public String getName();    

        //傳回寵物的年齡    

        public int getAge();    

        //傳回寵物的顔色    

        public String getColor();    

        //傳回寵物的價錢    

        public float getPrice();    

        //傳回寵物的全套資訊    

        public String getInfo();    

        //必須有一個對象數組可以儲存全部的寵物    

        private Pet p[] = null;    

        //必須定義一個目前已經加到了多少個寵物    

        private int foot = 0;    

        //對象數組的大小,可以由程式運作時動态配置設定    

        //len表示對象數組的長度    

        public PetShop(int len)    

                //動态得開辟了對象數組空間    

                this.p = new Pet[len];    

        //增加寵物    

        //假設說寵物商店裡面的寵物已經夠多了,那還可以繼續增加嗎?    

        public boolean add(Pet p)    

                if (this.foot&lt;this.p.length)    

                {    

                        //還有地方加入寵物    

                        this.p[foot] = p;    

                        //寵物數量增加    

                        this.foot++;    

                        return true;    

                }    

                else    

                        return false;    

        //查找寵物資訊的方法    

        public Pet[] search(String keyWord)    

                //要傳回的對象數組    

                Pet pet[] = null;    

                //最後要根據count的内容開辟對象數組pet,把此數組傳回    

                int count =0;    

                //pet的大小是否确定呢?    

                //先求出符合關鍵字的全部寵物資訊    

                //循環驗證    

                for (int i=0;i&lt;this.p.length ;i++ )    

                        //判斷傳回的資訊是否有指定的關鍵字存在    

                        if (this.p[i].getInfo().indexOf(keyWord)!=-1)    

                        {    

                                //如果不等于-1,表示已經找到了    

                                //就表示可以增加一個記錄    

                                count++;    

                        }    

                //經過以上代碼之後,肯定count包含了全部已經滿足要求的寵物資訊個數    

                if (count!=0)    

                        //已經有内容    

                        pet = new Pet[count];    

                        count = 0;    

                        //還需要重新循環一次    

                        for (int i=0;i&lt;this.p.length;i++)    

                                //判斷傳回的資訊是否有指定的關鍵字存在    

                                if(this.p[i].getInfo().indexOf(keyWord)!=-1)    

                                {    

                                        //如果不等于-1,表示已經查找到了    

                                        //表示向傳回的對象數組中加入内容    

                                        pet[count]=this.p[i];    

                                }    

                        //表示全部符合要求的對象數組    

                        return pet;    

                        //表示沒有查詢到内容    

                        return null;    

        private String color;    

        private float price;    

        //加入兩個構造方法    

        public Dog(){}    

        public Dog(String name,int age,String color,float price)    

                this.setColor(color);    

                this.setPrice(price);    

        public void setColor(String color)    

                this.color = color;    

        public void setPrice(float price)    

                this.price = price;    

        public String getColor()    

                return this.color;    

        public float getPrice()    

                return this.price;    

                return "狗的資訊:"+"\n"    

                                +"\t|- 狗的名字: "+this.name+"\n"    

                                +"\t|- 狗的顔色: "+this.color+"\n"    

                                +"\t|- 狗的年齡: "+this.age+"\n"    

                                +"\t|- 狗的價格: "+this.price+"\n";    

        public Cat(){}    

        public Cat(String name,int age,String color,float price)    

                return "貓的資訊:"+"\n"    

                                +"\t|- 貓的名字: "+this.name+"\n"    

                                +"\t|- 貓的顔色: "+this.color+"\n"    

                                +"\t|- 貓的年齡: "+this.age+"\n"    

                                +"\t|- 貓的價格: "+this.price+"\n";    

//編寫主方法進行測試    

public class Demo02    

                //指定裡面存放寵物的個數    

                PetShop shop = new PetShop(5);    

                //向商店中增加寵物    

                System.out.println(shop.add(new Dog("拉布拉多",3,"黃色",5000.0f))?"添加寵物成功!":"添加寵物失敗!");    

                System.out.println(shop.add(new Cat("黑貓",2,"黑色",500.0f))?"添加寵物成功!":"添加寵物失敗!");    

                System.out.println(shop.add(new Dog("美卡",1,"金色",2000.0f))?"添加寵物成功!":"添加寵物失敗!");    

                System.out.println(shop.add(new Cat("波斯貓",2,"白色",2800.0f))?"添加寵物成功!":"添加寵物失敗!");    

                System.out.println(shop.add(new Dog("哈巴狗",3,"棕色",120.0f))?"添加寵物成功!":"添加寵物失敗!");    

                //這個是第六個寵物,已經放不下了,是以就不放了    

                System.out.println(shop.add(new Dog("人造狗",1,"雜色",10.0f))?"添加寵物成功!":"添加寵物失敗!");    

                //增加成功之後進行查詢    

                //傳回的應該是一個對象數組(即接口數組)    

                Pet p[] = shop.search("狗");    

                for (int i=0;i&lt;p.length ;i++ )    

                        System.out.println(p[i].getInfo());    

<a href="http://redking.blog.51cto.com/attachment/200902/9/27212_1234192372erqv.png"></a>

出現空指向錯誤哈~主要原因在于count的值沒有改變,則其他的元素都是null,我們加入count++就可以了哈~

                                        // count的值沒有改變,則其他的元素都是null    

                                        count++;    

現在我們就查出了帶狗的資訊哈~~~

<a href="http://redking.blog.51cto.com/attachment/200902/9/27212_1234192375QuP2.png"></a>

如果我們要查找帶卡的寵物呢?

                Pet p[] = shop.search("卡");    

<a href="http://redking.blog.51cto.com/attachment/200902/9/27212_1234192377SvZy.png"></a>

思想總結出來: 

  一切就是操作接口,本程式如果可以清楚的掌握,并且可以寫出類似的題目,則基本的概念就算具備了。

###############################################################

本文轉自redking51CTO部落格,原文連結:http://blog.51cto.com/redking/129703,如需轉載請自行聯系原作者