zoukankan      html  css  js  c++  java
  • 多态

    测试代码如下代码:

     

    public class ParentChildTest {

          public static void main(String[] args) {

                 Parent parent=new Parent();

                 parent.printValue();

                 Child child=new Child();

                 child.printValue();

                

                 parent=child;

                 parent.printValue();

                

                 parent.myValue++;

                 parent.printValue();

                

                 ((Child)parent).myValue++;

                 parent.printValue();

                

          }

    }

     

    class Parent{

          public int myValue=100;

          public void printValue() {

                 System.out.println("Parent.printValue(),myValue="+myValue);

          }

    }

    class Child extends Parent{

          public int myValue=200;

          public void printValue() {

                 System.out.println("Child.printValue(),myValue="+myValue);

          }

    }

     

    测试前猜测结果:

    我觉得应该输出以下内容:

    100

    200

    200

    201

    201

    结果却输出以下内容:

    Parent.printValue(),myValue=100

    Child.printValue(),myValue=200

    Child.printValue(),myValue=200

    Child.printValue(),myValue=200

    Child.printValue(),myValue=201

    让我意外的只有第三个结果,为什么会是200,在程序中,明明将子类对象赋给了父类的对象parent,parent的myvalue应该是200才对,为什么对他进行操作去没有改变他的值呢?

    原来parent虽然应用了子类的对象,但是parent.myvalue任然是父类的成员测试如下:

    Parent.printValue(),myValue=100

    Child.printValue(),myValue=200

    Child.printValue(),myValue=200

    parent.myValue = 101

    Child.printValue(),myValue=200

    Child.printValue(),myValue=201

     

     

    用多态方法模拟ATM自动取款机

    结果如下:

    import java.util.Vector;

    import java.io.*;

    public class ATM {

          public static void main(String[] args)throws IOException{

                 BufferedReader input = new BufferedReader(new

                               InputStreamReader(System.in));

                 Account account = new Account();

                 ATM atm = new ATM();

                 do{//输入银行卡密码,如果密码错误,则需要重新输入。

                        System.out.println("请输入您的银行卡或者输入银行账户");

                        String userid = input.readLine();

                        System.out.println("输入您的密码");

                        String userpassword = input.readLine();

                        account = atm.login(userid, userpassword);

                        if(account==null)

                               System.out.println("账户或密码错误");

                 }while(account == null);

                

                 boolean flag = true;

                 int option;

                 do{

                        BufferedReader input1 = new BufferedReader(new

                                      InputStreamReader(System.in));

                        System.out.println("1:取款      2:转账   3:查询   4:修改密码  5:退卡");

                        try{

                               String s = input1.readLine();

                               option = Integer.parseInt(s);

                        }catch(IOException e){

                               System.out.println("输入异常!");

                               e.printStackTrace();

                               option = 0;

                        }

                        if(option == 1){

                               account.withdrawl();

                        }else if(option == 2){

                               System.out.println("输入转入用户");

                               String userid = input1.readLine();

                               System.out.println("确认转入用户");

                               String userid2 = input1.readLine();

                               if(!userid.equals(userid2))

                                      System.out.println("两次输入用户不同");

                               //account.transfor(a, e)

                        }else if(option==3){

                               account.inquiry();

                        }else if(option==4){

                               account.motifyPin();

                        }else if(option==5){

                               flag = false;

                        }else

                               System.out.println("无此操作");

                              

                 }while(flag);

          }

         

          public ATM() {

                 // TODO Auto-generated constructor stub

                 accountGroup = new Vector<Account>();

                 accountGroup.add(new Account("100000000000",

                        "毛松林","1996/12/07","000000",(int)(Math.random()*100000)));

                 accountGroup.add(new Account("200000000000",

                               "张三","1996/4/8","111111",(int)(Math.random()*1000)));

          }

          public Account login(String userid,String password)           //确认账户

          {

                 for(Account a:accountGroup){

                        if(a.getID().equals(userid) && a.getPassword().equals(password))

                               return a;

                 }

                 return null;

          }

          private Vector<Account> accountGroup;

    }


    结果截图:

  • 相关阅读:
    6.简易计算器
    5.用户密码管理
    4.方法重载
    3.对象数组做参数
    2.迷你DVD管理系统
    1.二维数组计算班级成绩
    31.向数组中插入一个元素
    30.使用Arrays类的各种方法
    Java开发中的23种设计模式详解(转)
    个人代码归档
  • 原文地址:https://www.cnblogs.com/maosonglin/p/6079039.html
Copyright © 2011-2022 走看看