上季内容回顾:
代理设计、适配器设计
抽象类和接口的区别
本季主要知识点:
本季以题目讲解为主,详细的讲解了抽象类和接口的实际应用及典型的实例分析。
练习题一
<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>
分析:
只要是宠物则肯定可以向宠物商店中加入
猫 --> 宠物
狗 --> 宠物
宠物商店存放宠物
<a href="http://redking.blog.51cto.com/attachment/200902/9/27212_1234192370oHuA.png"></a>
五种宠物?如果说不是五种,可能是更多种了,那该如何?
5种宠物 --> 5个接口对象 --> 对象数组
//宠物
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<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<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<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<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,如需转载请自行联系原作者