课后作业一:接口多态:使用接口代替抽象基类
一、源代码
package zoo4; import java.util.Vector; public class Zoo2 { public static void main(String args[]) { Feeder f = new Feeder("小李"); Vector<Animal> ans = new Vector<Animal>();//可随时向其中加入或移除对象 //饲养员小李喂养一只狮子 ans.add(new Lion()); //饲养员小李喂养十只猴子 for (int i = 0; i < 10; i++) { ans.add(new Monkey()); } //饲养员小李喂养5只鸽子 for (int i = 0; i < 5; i++) { ans.add(new Pigeon()); } f.feedAnimals(ans); } } class Feeder { public String name; Feeder(String name) { this.name = name; } public void feedAnimals(Vector<Animal> ans) { for (Animal an : ans) { an.eat(); } } } interface Animal { public void eat(); } class Lion implements Animal { public void eat() { System.out.println("我不吃肉谁敢吃肉!"); } } class Monkey implements Animal { public void eat() { System.out.println("我什么都吃,尤其喜欢香蕉。"); } } class Pigeon implements Animal { public void eat() { System.out.println("我要减肥,所以每天只吃一点大米。"); } }
二、实验运行截图
课后作业二:用多态的方法模拟ATM操作流程
1.源程序:
package Work; //接口多态:使用接口代替抽象基类 import java.util.Scanner; import java.util.Vector; public class ATM{ static AccountY stu[]=new Account[500]; public static void main(String[] args) { //主界面(5个用户) stu[0]=new Account("20153149","流刘雨鑫","2016/11/16","12346",100); stu[1]=new Account("20153150","刘","2016/11/15","li",2000); stu[2]=new Account("20153151","啦啦子","2016/11/15","muzi",3000); stu[3]=new Account("20153149","咦米","2016/11/15","mimi",5000); stu[4]=new Account("20153148","哦哦暑","2016/11/15","xiaxiao",40); String key; Scanner scan=new Scanner(System.in); Scanner scan1=new Scanner(System.in); Scanner scan2=new Scanner(System.in); Scanner scan3=new Scanner(System.in); int num=0; int k=1; System.out.println("请输入密码:"); key=scan.nextLine(); int b=0; for(int i=0;i< 5;i++) { if(!key.equals(stu[i].getKey())) { b++;}} if(b>=5){ System.out.print("该卡已被锁定,无法操作"); } else for(int i=0;i< 5;i++) { if(key.equals(stu[i].getKey())) { while(k==1) { System.out.println(); System.out.print("1.存款 2.取款 3.转账汇款 4.修改密码 5.查询余额 6.退卡"); System.out.println(); System.out.print("请选择:"); num=scan1.nextInt(); if(num==6) { System.out.println("卡已成功退出!请拿卡!"); break; } switch(num) { case 1://存款 double m; System.out.print("请输入存入的金额:"); m=scan2.nextDouble(); stu[i].cun(m); break; case 2://取款 int numm; System.out.print("100元\t500元\t1000元\t1500元\t2000元\t5000元\t其他金额\n" + "1.退卡"); System.out.println(); System.out.print("请选择:"); numm=scan3.nextInt(); if(numm==1) break; else stu[i].qu(numm);break; case 3://转账汇款 String nummm; double cc; int n; System.out.print("请输入转账行号:"); nummm=scan.nextLine(); /*boolean b=true; for(int j=0;j<5;j++) {if(!nummm.equals(stu[j].getNum())) { b=false; }} if(b==false){ System.out.println("信息错误!"); }*/ for(int j=0;j<5;j++) if(nummm.equals(stu[j].getNum())) { System.out.print("请输入转账金额:"); cc=scan.nextDouble(); System.out.print("请确认(1确认,0取消)"); n=scan.nextInt(); if(n==1) { stu[i].qu(cc); stu[j].cun(cc); System.out.print("转账成功!"); } else if(n==0){ System.out.print("已取消!"); } else{ System.out.print("发生错误!"); } } break; case 4: String kk; System.out.print("请输入修改的密码:"); kk=scan.nextLine(); stu[i].setKey(kk); break; case 5: System.out.print("余额为:"+stu[i].getMoney()); break; case 6: break; } } }} } } abstract class AccountY { public abstract String getNum(); public abstract void setKey(String s); public abstract String getKey(); public abstract double getMoney(); public abstract void cun(double m); public abstract void qu(double m); } class Account extends AccountY{ private String num;//长度为11,由数字和字符组合 private String name;//用户的姓名 private String Date;//操作日期 private String key;//账户密码 private double money;//当前余额 public Account(String n,String a,String b,String c,double d){num=n;name=a;Date=b;key=c;money=d;} public String getNum(){return num;} public String getName(){return name;} public String getDate(){return Date;} public void setKey(String s){ key=s; } public String getKey(){return key;} public double getMoney(){return money;} public void cun(double m){ if(m<=200000) money+=m; else {System.out.println("超出限额!");} } public void qu(double m){ if(m<money) money-=m; else System.out.println("超出余额!"); } }
二、实验结果截图
验证:
①TestInstanceof.java
代码
public class TestInstanceof { public static void main(String[] args) { //声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类 //但hello变量的实际类型是String Object hello = "Hello"; //String是Object类的子类,所以返回true。 System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object)); //返回true。 System.out.println("字符串是否是String类的实例:" + (hello instanceof String)); //返回false。 System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math)); //String实现了Comparable接口,所以返回true。 System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable)); String a = "Hello"; //String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过 //System.out.println("字符串是否是Math类的实例:" + (a instanceof Math)); } }
运行结果截图:
②TestCast.java
代码如下:
class Mammal{} class Dog extends Mammal {} class Cat extends Mammal{} public class TestCast { public static void main(String args[]) { Mammal m; Dog d=new Dog(); Cat c=new Cat(); m=d; //d=m; d=(Dog)m; //d=c; //c=(Cat)m; } }
运行结果没有出错!
③Zoo.java
代码如下:
package zoo4; import java.util.Vector; public class Zoo { public static void main(String args[]) { Feeder f = new Feeder("小李"); Vector<Animal> ans = new Vector<Animal>();//可随时向其中加入或移除对象 //饲养员小李喂养一只狮子 ans.add(new Lion()); //饲养员小李喂养十只猴子 for (int i = 0; i < 10; i++) { ans.add(new Monkey()); } //饲养员小李喂养5只鸽子 for (int i = 0; i < 5; i++) { ans.add(new Pigeon()); } f.feedAnimals(ans); } } class Feeder { public String name; Feeder(String name) { this.name = name; } public void feedAnimals(Vector<Animal> ans) { for (Animal an : ans) { an.eat(); } } } abstract class Animal { public abstract void eat(); } class Lion extends Animal { public void eat() { System.out.println("我不吃肉谁敢吃肉!"); } } class Monkey extends Animal { public void eat() { System.out.println("我什么都吃,尤其喜欢香蕉。"); } } class Pigeon extends Animal { public void eat() { System.out.println("我要减肥,所以每天只吃一点大米。"); } }
运行结果截图: