收藏 分销(赏)

[零基础学JAVA]JavaSE面向对象部分-19.面向对象高级.doc

上传人:天**** 文档编号:11397982 上传时间:2025-07-21 格式:DOC 页数:23 大小:258.57KB 下载积分:10 金币
下载 相关
[零基础学JAVA]JavaSE面向对象部分-19.面向对象高级.doc_第1页
第1页 / 共23页
[零基础学JAVA]JavaSE面向对象部分-19.面向对象高级.doc_第2页
第2页 / 共23页


点击查看更多>>
资源描述
上季内容回顾: 代理设计、适配器设计 抽象类和接口的区别 本季主要知识点: 本季以题目讲解为主,详细的讲解了抽象类和接口的实际应用及典型的实例分析。 练习题一 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)             {                     super(name,age,sex);                     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;             }             public String getInfo()             {                     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(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());             }     } 练习题二(重点来咯~~~) 分析:   只要是宠物则肯定可以向宠物商店中加入    猫  --> 宠物    狗  --> 宠物   宠物商店存放宠物 五种宠物?如果说不是五种,可能是更多种了,那该如何?   5种宠物 --> 5个接口对象 --> 对象数组 //宠物     interface Pet     {     }     //宠物商店     class PetShop     {     }     //小狗     class Dog implements Pet     {     }     //小猫     class Cat implements Pet     {     } 宠物信息:  · 名字  · 年龄 · 颜色 · 价钱 还应该具备一个返回全部信息的方法。 我们继续看下面哈~ //宠物     interface Pet     {             //返回宠物的名字             public String getName();             //返回宠物的年龄             public int getAge();             //返回宠物的颜色             public String getColor();             //返回宠物的价钱             public float getPrice();             //返回宠物的全套信息             public String getInfo();     }     //宠物商店     class PetShop     {             //必须有一个对象数组可以保存全部的宠物             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;                     }                     else                     {                             //表示没有查询到内容                             return null;                     }             }     }     //小狗     class Dog implements Pet     {             private String name;             private int age;             private String color;             private float price;             //加入两个构造方法             public Dog(){}             public Dog(String name,int age,String color,float price)             {                     this.setName(name);                     this.setAge(age);                     this.setColor(color);                     this.setPrice(price);             }             public void setName(String name)             {                     this.name = name;             }             public void setAge(int age)             {                     this.age = age;             }             public void setColor(String color)             {                     this.color = color;             }             public void setPrice(float price)             {                     this.price = price;             }             public String getName()             {                     return this.name;             }             public int getAge()             {                     return this.age;             }             public String getColor()             {                     return this.color;             }             public float getPrice()             {                     return this.price;             }             public String getInfo()             {                     return "狗的信息:"+"\n"                                     +"\t|- 狗的名字: "+this.name+"\n"                                     +"\t|- 狗的颜色: "+this.color+"\n"                                     +"\t|- 狗的年龄: "+this.age+"\n"                                     +"\t|- 狗的价格: "+this.price+"\n";             }     }     //小猫     class Cat implements Pet     {             private String name;             private int age;             private String color;             private float price;             //加入两个构造方法             public Cat(){}             public Cat(String name,int age,String color,float price)             {                     this.setName(name);                     this.setAge(age);                     this.setColor(color);                     this.setPrice(price);             }             public void setName(String name)             {                     this.name = name;             }             public void setAge(int age)             {                     this.age = age;             }             public void setColor(String color)             {                     this.color = color;             }             public void setPrice(float price)             {                     this.price = price;             }             public String getName()             {                     return this.name;             }             public int getAge()             {                     return this.age;             }             public String getColor()             {                     return this.color;             }             public float getPrice()             {                     return this.price;             }             public String getInfo()             {                     return "猫的信息:"+"\n"                                     +"\t|- 猫的名字: "+this.name+"\n"                                     +"\t|- 猫的颜色: "+this.color+"\n"                                     +"\t|- 猫的年龄: "+this.age+"\n"                                     +"\t|- 猫的价格: "+this.price+"\n";             }     }     //编写主方法进行测试     public class Demo02     {             public static void main(String args[])             {                     //指定里面存放宠物的个数                     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());                     }             }     } 出现空指向错误哈~主要原因在于count的值没有改变,则其他的元素都是null,我们加入count++就可以了哈~ //宠物     interface Pet     {             //返回宠物的名字             public String getName();             //返回宠物的年龄             public int getAge();             //返回宠物的颜色             public String getColor();             //返回宠物的价钱             public float getPrice();             //返回宠物的全套信息             public String getInfo();     }     //宠物商店     class PetShop     {             //必须有一个对象数组可以保存全部的宠物             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
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服