有一段时间没有更新博客了,现在把笔记补上,用了大概一周半的时间把面向对象的高级部分看完了,说实话,这部分确实很难理解,难度比较大,也是重点部分。下面用一张思维导图来总结一下吧。
其中对于怎么区别覆写和重载是很容易弄混的,所以要加以区分:
重载:overloading,定义:方法名称相同,参数的类型或个属不同,对权限没有要求。 范围:发生在一个类中
覆写:overriding 定义:方法名称,参数的类型,返回值类型全部相同,被覆写的方法不能拥有更严格的权限 范围:发生在继承类中
几个比较有意思的例子:
1.实例化工厂模板:
interface Fruit{ public void eat(); } class Apple implements Fruit{ public void eat(){ System.out.println("**吃苹果"); } } class Orange implements Fruit{ public void eat(){ System.out.println("**吃橘子"); } } class Factory{ public static Fruit getInstance(String className){ Fruit f=null; if("apple".equals(className)){ f=new Apple(); } if("orange".equals(className)){ f=new Orange(); } return f; } } public class InterfaceCaseDemo05{ public static void main(String args[]){ Fruit f = Factory.getInstance(args[0]) ; // 实例化接口 if(f!=null){ // 判断是否取得实例 f.eat() ; } } }在这个模板中,工厂类那部分显得特别重要,这样在下边判断取得实例。以至于不用一个一个的判断。还有其他的设计模式,比如适配器设计模式等等,这样的设计模式省去了许多繁杂的工作。
2.宠物商店:
interface Pet{ // 定义宠物接口 public String getName() ; public String getColor() ; public int getAge() ; } class Cat implements Pet{ // 猫是宠物,实现接口 private String name ; // 宠物名字 private String color ; // 宠物颜色 private int age ; // 宠物年龄 public Cat(String name,String color,int age){ this.setName(name) ; this.setColor(color) ; this.setAge(age) ; } public void setName(String name){ this.name = name ; } public void setColor(String color){ this.color = color; } public void setAge(int age){ this.age = age ; } public String getName(){ return this.name ; } public String getColor(){ return this.color ; } public int getAge(){ return this.age ; } }; class Dog implements Pet{ // 狗是宠物,实现接口 private String name ; // 宠物名字 private String color ; // 宠物颜色 private int age ; // 宠物年龄 public Dog(String name,String color,int age){ this.setName(name) ; this.setColor(color) ; this.setAge(age) ; } public void setName(String name){ this.name = name ; } public void setColor(String color){ this.color = color; } public void setAge(int age){ this.age = age ; } public String getName(){ return this.name ; } public String getColor(){ return this.color ; } public int getAge(){ return this.age ; } }; class PetShop{ // 宠物商店 private Pet[] pets ; // 保存一组宠物 private int foot ; public PetShop(int len){ if(len>0){ this.pets = new Pet[len] ; // 开辟数组大小 }else{ this.pets = new Pet[1] ; // 至少开辟一个空间 } } public boolean add(Pet pet){ // 增加的是一个宠物 if(this.foot<this.pets.length){ this.pets[this.foot] = pet ; // 增加宠物 this.foot ++ ; return true ; }else{ return false ; } } public Pet[] search(String keyWord){ // 应该确定有多少个宠物符合要求 Pet p[] = null ; int count = 0 ; // 记录下会有多少个宠物符合查询结果 for(int i=0;i<this.pets.length;i++){ if(this.pets[i]!=null){ // 表示此位置有宠物 if(this.pets[i].getName().indexOf(keyWord)!=-1 ||this.pets[i].getColor().indexOf(keyWord)!=-1){ count++ ; // 修改查找到的记录数 } } } p = new Pet[count] ; // 开辟指定的大小空间 int f = 0 ; // 增加元素的位置标记 for(int i=0;i<this.pets.length;i++){ if(this.pets[i]!=null){ // 表示此位置有宠物 if(this.pets[i].getName().indexOf(keyWord)!=-1 ||this.pets[i].getColor().indexOf(keyWord)!=-1){ p[f] = this.pets[i] ; f++ ; } } } return p ; } }; public class PetShopDemo{ public static void main(String args[]){ PetShop ps = new PetShop(5) ; // 五个宠物 ps.add(new Cat("白猫","白色的",2)) ; // 增加宠物,成功 ps.add(new Cat("黑猫","黑色的",3)) ; // 增加宠物,成功 ps.add(new Cat("花猫","花色的",3)) ; // 增加宠物,成功 ps.add(new Dog("拉步拉多","黄色的",3)) ; // 增加宠物,成功 ps.add(new Dog("金毛","金色的",2)) ; // 增加宠物,成功 ps.add(new Dog("黄狗","黑色的",2)) ; // 增加宠物,失败 print(ps.search("黑")) ; } public static void print(Pet p[]){ for(int i=0;i<p.length;i++){ if(p[i]!=null){ System.out.println(p[i].getName() + "," + p[i].getColor() +"," + p[i].getAge()) ; } } } };此例子代码比较多,但是很容易理解,就是利用了接口的知识,比较关键的定义宠物商店的类,开辟数组空间用来保存宠物,还有就是构造的增加宠物的方法。
以后的学习相对于来说要简单些了,继续加油!