面向对象高级07 抽象类和接口的实际应用.docx
- 文档编号:17161397
- 上传时间:2023-07-22
- 格式:DOCX
- 页数:27
- 大小:232.26KB
面向对象高级07 抽象类和接口的实际应用.docx
《面向对象高级07 抽象类和接口的实际应用.docx》由会员分享,可在线阅读,更多相关《面向对象高级07 抽象类和接口的实际应用.docx(27页珍藏版)》请在冰点文库上搜索。
面向对象高级07抽象类和接口的实际应用
19.面向对象高级(07)
本季以题目讲解为主,详细的讲解了抽象类和接口的实际应用及典型的实例分析。
blog:
[零基础学JAVA]JavaSE面向对象部分-19.面向对象高级(07)2009-02-09
上季内容回顾:
代理设计、适配器设计
抽象类和接口的区别
本季主要知识点:
本季以题目讲解为主,详细的讲解了抽象类和接口的实际应用及典型的实例分析。
练习题一
abstract class Employee
{
private Stringname;
private int age;
private Stringsex;
public Employee(){}
public Employee(Stringname,int age,Stringsex)
{
this.setName(name);
this.setAge(age);
this.setSex(sex);
}
public void setName(Stringname)
{
this.name=name;
}
public void setAge(int age)
{
this.age=age;
}
public void setSex(Stringsex)
{
this.sex=sex;
}
public StringgetName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public StringgetSex()
{
return this.sex;
}
//显示数据
public abstract StringgetInfo();
}
class Manager extends Employee
{
//职务
private Stringjob;
//年薪
private float income;
public Manager(){}
public Manager(Stringname,int age,Stringsex,Stringjob,float income)
{
super(name,age,sex);
this.setJob(job);
this.setIncome(income);
}
public void setJob(Stringjob)
{
this.job=job;
}
public void setIncome(float income)
{
this.income=income;
}
public StringgetJob()
{
return this.job;
}
public float getIncome()
{
return this.income;
}
public StringgetInfo()
{
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 Stringdept;
private float salary;
public Worker(){}
public Worker(Stringname,int age,Stringsex,Stringdept,float salary)
{
super(name,age,sex);
this.setDept(dept);
this.setSalary(salary);
}
public void setDept(Stringdept)
{
this.dept=dept;
}
public void setSalary(float salary)
{
this.salary=salary;
}
public StringgetDept()
{
return this.dept;
}
public float getSalary()
{
return this.salary;
}
public StringgetInfo()
{
return "职员信息:
"+"\n"
+"\t|-姓名:
"+super.getName()+"\n"
+"\t|-年龄:
"+super.getAge()+"\n"
+"\t|-性别:
"+super.getSex()+"\n"
+"\t|-部门:
"+this.getDept()+"\n"
+"\t|-月薪:
"+this.getSalary();
}
}
public class Demo01
{
public static void main(Stringargs[])
{
Employeew= new Worker("张三",30,"男","技术部",5000.0f);
Employeem= new Manager("李四",35,"女","经理",10000.0f);
System.out.println(w.getInfo());
System.out.println(m.getInfo());
}
}
练习题二(重点来咯~~~)
分析:
只要是宠物则肯定可以向宠物商店中加入
猫 -->宠物
狗 -->宠物
宠物商店存放宠物
五种宠物?
如果说不是五种,可能是更多种了,那该如何?
5种宠物-->5个接口对象--> 对象数组
//宠物
interface Pet
{
}
//宠物商店
class PetShop
{
}
//小狗
class Dog implements Pet
{
}
//小猫
class Cat implements Pet
{
}
宠物信息:
·名字
·年龄
·颜色
·价钱
还应该具备一个返回全部信息的方法。
我们继续看下面哈~
//宠物
interface Pet
{
//返回宠物的名字
public StringgetName();
//返回宠物的年龄
public int getAge();
//返回宠物的颜色
public StringgetColor();
//返回宠物的价钱
public float getPrice();
//返回宠物的全套信息
public StringgetInfo();
}
//宠物商店
class PetShop
{
//必须有一个对象数组可以保存全部的宠物
private Petp[]= null;
//必须定义一个当前已经加到了多少个宠物
private int foot=0;
//对象数组的大小,可以由程序运行时动态分配
//len表示对象数组的长度
public PetShop(int len)
{
//动态得开辟了对象数组空间
this.p= new Pet[len];
}
//增加宠物
//假设说宠物商店里面的宠物已经够多了,那还可以继续增加吗?
public boolean add(Petp)
{
if (this.foot { //还有地方加入宠物 this.p[foot]=p; //宠物数量增加 this.foot++; return true; } else { return false; } } //查找宠物信息的方法 public Pet[]search(StringkeyWord) { //要返回的对象数组 Petpet[]= null; //最后要根据count的内容开辟对象数组pet,把此数组返回 int count=0; //pet的大小是否确定呢? //先求出符合关键字的全部宠物信息 //循环验证 for (int i=0;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 { //判断返回的信息是否有指定的关键字存在 if(this.p[i].getInfo().indexOf(keyWord)! =-1) { //如果不等于-1,表示已经查找到了 //表示向返回的对象数组中加入内容 pet[count]=this.p[i]; } } //表示全部符合要求的对象数组 return pet; } else { //表示没有查询到内容 return null; } } } //小狗 class Dog implements Pet { private Stringname; private int age; private Stringcolor; private float price; //加入两个构造方法 public Dog(){} public Dog(Stringname,int age,Stringcolor,float price) { this.setName(name); this.setAge(age); this.setColor(color); this.setPrice(price); } public void setName(Stringname) { this.name=name; } public void setAge(int age) { this.age=age; } public void setColor(Stringcolor) { this.color=color; } public void setPrice(float price) { this.price=price; } public StringgetName() { return this.name; } public int getAge() { return this.age; } public StringgetColor() { return this.color; } public float getPrice() { return this.price; } public StringgetInfo() { return "狗的信息: "+"\n" +"\t|-狗的名字: "+this.name+"\n" +"\t|-狗的颜色: "+this.color+"\n" +"\t|-狗的年龄: "+this.age+"\n" +"\t|-狗的价格: "+this.price+"\n"; } } //小猫 class Cat implements Pet { private Stringname; private int age; private Stringcolor; private float price; //加入两个构造方法 public Cat(){} public Cat(Stringname,int age,Stringcolor,float price)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象高级07 抽象类和接口的实际应用 面向 对象 高级 07 抽象 接口 实际 应用
![提示](https://static.bingdoc.com/images/bang_tan.gif)