Java11月18日

动手动脑

实验一:

1. 下边的程序运行结果是什么?

2.你如何解释会得到这样的输出?

3.计算机是不会出错的,之所以得到这样的运行结果也是有原因的那么从这些运行结果中,你能总结出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);

       }

}

 

(1)实验结果运行图:

(2)这样结果输出的原因:

   答:首先前两个分别引用各自的类的变量,所以分别是100和200没有什么异议,第三个把子类的赋值给父类了,所以输出结果为200,第四个因为只是改变了弗雷德变量值,没有改变子类的变量值,所以仍为200,而此时父类的为101。最后对子类进行强制转换,所以变量值也发生了改变为201.

(3)当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。

这个特性实际上就是面向对象“多态”特性的具体表现。

实验二:多态方法模拟ATM程序

//ATM

//zhanxinwu,November ,15,2016.

 

import java.util.Scanner;

abstract class QuKuan

{

    abstract int qukuan();

}

class Account extends QuKuan

{    

       String ID;

       String name;

       String Date;

       int operate;

       String mima;

       double yue;

       Account()

       {

              ID="1234567891a";

              mima="123456";

              yue=0;

       }

       int qukuan()

       {

              int flag=0;

              double qu=0;

              System.out.println("欢迎选择取款:");

              System.out.println("1 100");

              System.out.println("2 500");

              System.out.println("3 1000");

              System.out.println("4 1500");

              System.out.println("5 2000");

              System.out.println("6 5000");

              System.out.println("7 其他金额");

              System.out.println("8 退卡");

              System.out.println("9 返回上一界面");

              System.out.print("请选取要取款的金额'操作数':");

              Scanner scanner=new Scanner(System.in);

              operate=scanner.nextInt();

              while(operate<1||operate>9)

              {

                     System.out.print("请重新选取要取款的金额‘操作数’:");

                     operate=scanner.nextInt();

              }

       switch(operate){

             case 1:qu=100;

                    

             case 2:qu=500;

      

             case 3:qu=1000;

             case 4:qu=1500;

             case 5:qu=2000;

             case 6:qu=5000;

                    

             case 7:{System.out.print("请输入要取款的金额:");

                   qu=scanner.nextDouble();

                     if(yue<qu)

                     {

                            System.out.println("余额不足!");

                     }

                     else

                     {

                            yue-=qu;

                            System.out.println("取款成功,余额为:"+yue);

                     }

             }

              }

             

              return flag;

       }

      

       void xiugaimima()

       {

 

              System.out.print("请输入要修改的取款账号所对应的密码:");

              Scanner scanner=new Scanner(System.in);

              String mi=scanner.next();

              mima=mi;

              System.out.println("修改后的取款账号所对应的密码为:"+mima);

       }

      

       void cunkuan()

       {

              System.out.print("请输入要存款的金额:");

              Scanner scanner=new Scanner(System.in);

              double yu=scanner.nextDouble();

              yue+=yu;

              System.out.println("存款成功,余额为:"+yue);

       }

      

       void chaxun()

       {

              System.out.println("余额为:"+yue);

       }

      

       void zhuanzhang()

       {

              System.out.print("请输入转入的行号:");

              Scanner scanner=new Scanner(System.in);

              String hang=scanner.next();

              System.out.print("请输入要转账的金额:");

              double yu=scanner.nextDouble();

              if(yue<yu)

              {

                     System.out.println("余额不足,请充值!!!");

              }

              else

              {

                     yue-=yu;

                     System.out.println("转账成功,余额为:"+yue+",已转入到"+hang+"中"+yu+"元");

              }

       }

}

 

public class ATMduo

{

       public static void main(String[] args)

       {

              int k;

              Scanner scanner=new Scanner(System.in);

              Account m=new Account();

              while(true)

              {

                    

                            System.out.print("请输入密码:");

                            String n=scanner.next();

                            if(n.equals(m.mima))

                            {

                                   while(true)

                                   {

                                          System.out.println("1 存款");

                                          System.out.println("2 取款");

                                          System.out.println("3 转账汇款");

                                          System.out.println("4 修改密码");

                                          System.out.println("5 查询余额");

                                          System.out.println("6 退出系统");

                                          System.out.print("请输入操作项:");

                                          k=scanner.nextInt();

 

                                          if(k==1) m.cunkuan();

                                          if(k==2)

                                          {

                                                 int g=m.qukuan();

                                                 if(g==1) break;

                                          }

                                          if(k==3) m.zhuanzhang();

                                          if(k==4) m.xiugaimima();

                                          if(k==5) m.chaxun();

                                          if(k==6) break;

                                   }

                            }

                            else System.out.println("该卡已被锁定,请联系银行工作人员!!!");

                    

              }

       }

}

 

posted @ 2016-11-18 22:50  Janson鑫  阅读(158)  评论(0编辑  收藏  举报