• java多态动手动脑


    实验任务一:多态实现ATM工作原理

    1)源代码

    package demo;

     

    import java.util.Scanner;

     

    class A{

           String name;

           String date;

           String mima;

           int yu;

           String kahao;

           public A(String name,String date,String mima,int yu,String kahao)

           {

                  this.name=name;

                  this.date=date;

                  this.mima=mima;

                  this.yu=yu;

                  this.kahao=kahao;

           }

           public A(){}

           public void way(){}

    }

    class Qukuan extends A{

           public Qukuan(String str,String str1,String str2,int m,String str3){

                  name=str;

                  date=str1;

                  mima=str2;

                  yu=m;

                  kahao=str3;

           }

           public void way(){

                  System.out.println("100 200 1000 1500 2000 5000  1.其它金额   2. 退卡3.返回操作");

                  Scanner in=new Scanner(System.in);

                  int p=in.nextInt();

                  A a=new A(name,date,mima,yu,kahao);

                  if(p==100)

                  {

                         a.yu-=100;

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

                  }

                  else if(p==200)

                  {

                         a.yu-=200;

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

                  }

                  else if(p==500)

                  {

                         a.yu-=500;

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

                  }

                  else if(p==1000)

                  {

                         a.yu-=1000;

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

                  }

                  else if(p==1500)

                  {

                         a.yu-=1500;

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

                  }

                  else if(p==2000)

                  {

                         a.yu-=2000;

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

                  }

                  else if(p==5000)

                  {

                         a.yu-=5000;

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

                  }

                  else if(p==1)

                  {

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

                         int m=in.nextInt();

                         a.yu-=m;

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

                  }

                  else if(p==2){

                         return;

                  }

                  else if(p==3){

                         way();

                  }

           }

    }

    class Cunkuan extends A{

           public Cunkuan(String str,String str1,String str2,int m,String str3){

                  name=str;

                  date=str1;

                  mima=str2;

                  yu=m;

                  kahao=str3;

           }

           public void way(){

                  Scanner in=new Scanner(System.in);

                  System.out.println("请输入存款金额:");

                  int n=in.nextInt();

                  A a=new A(name,date,mima,yu,kahao);

                  a.yu+=n;

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

           }

    }

    class Zhuanzhang extends A{

           String kahao;

           public Zhuanzhang(String str,String str1,String str2,int m,String str3){

                  name=str;

                  date=str1;

                  mima=str2;

                  yu=m;

                  kahao=str3;

           }

           public void way(){

                  A a=new A(name,date,mima,yu,kahao);

                  Scanner in=new Scanner(System.in);

                  System.out.println("请输入转账的卡号:");

                  String s=in.next();

                  A b=new A(name,date,mima,yu,s);

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

                  int w=in.nextInt();

                  a.yu-=w;

                  b.yu+=w;

                  System.out.println("转账成功");

           }

    }

    public class ATM {

        public static void main(String args[]){

               System.out.println("ATM系统 选择语言:1 中文 2 英文");

                 Scanner in=new Scanner(System.in);

                 int q=in.nextInt();

                Qukuan a1=new Qukuan("zjx","2016.11.15","321165",6000,"45612354745");

                  Cunkuan a2=new Cunkuan("zjx","2016.11.15","321165",6000,"45612354745");

                  Zhuanzhang a3=new Zhuanzhang("zjx","2016.11.15","321165",6000,"45612354745");

                 if(q==1){

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

                       String str=in.next();

                       if(a1.mima.equals(str)){

                       System.out.println("1 取款 2 存款 3 转账 4 退卡");

                       int t=in.nextInt();

                       if(t==1)

                              a1.way();

                       else if(t==2)

                              a2.way();

                       else if(t==3)

                              a3.way();

                       else

                              return;

                       }

                       else

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

                 }

                 else if(q==2)

                 {

                       System.out.println("Please input password:");

                       String str=in.next();

                       A a=new A("zjx","2016.11.15",str,6000,"654562346212");

                       if(a.mima==str){

                       System.out.println("1 qukuan 2 cunkuan 3 zhuanzhang 4 tuika");

                       int t=in.nextInt();

                       if(t==1)

                              a1.way();

                       else if(t==2)

                              a2.way();

                       else if(t==3)

                              a3.way();

                       }

                       else

                              System.out.println("Password is not true.");

                 }

        }

    }

    2)程序截图:

     

        

     

    实验任务二:课堂动手动脑

    1)源代码:

    package demo;

    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);

        }

    }

    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();

                 }

    }

    2)实验结果截图:

                       

    3)程序分析:为何会得到这样的输出?

    (1)父类分配空间,调用它的构造方法。

    (2)子类分配空间,调用它自己的构造方法。

    (3)子类赋值给父类,父类的子类有相同的方法,父类调用子类方法,并引用子类的对象,输出子类的值。

    (4)父类中的变量值进行自加,但是只是父类中的值自加,调用的仍然是子类的方法,输出子类的值。

    (5)将parent对象强制转化为Child,指向子类中的变量,调用子类方法并输出子类的值。

    4)程序修改:

    package demo;

    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);

        }

    }

    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();

     

                 ((Child)parent).myValue++;

                 parent.printValue();

     

                  parent.myValue++;

                  parent.printValue();

                 }

    }

    截图:

    分析:先将parent对象强制转化为Child,指向子类中的变量,输出子类的值。然后parent父类中的对象进行自加,对子类没有影响,调用子类方法,对象,输出子类的值。

  • 相关阅读:
    window下cmd命令行的个人使用——集录
    前端之 —— node.js摸爬打滚之路(一)
    GIT常用命令以及作用【备忘】
    如何将Eclipse中的开源项目使用到Android Studio中
    2015读书目录
    Android4.0+锁屏程序开发——设置锁屏页面篇
    Android4.0+锁屏程序开发——按键屏蔽篇
    【光速使用开源框架系列】数据库框架OrmLite
    【光速使用开源框架系列】图片加载框架ImageLoader
    Android输入法部分遮挡UI的问题(与EditText框相切)
  • 原文地址:https://www.cnblogs.com/jokerr/p/6079107.html
Copyright © 2020-2023  润新知