码迷,mamicode.com
首页 > 编程语言 > 详细

java多态动手动脑

时间:2016-11-18 22:58:05      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:java   demo   转化   out   手动   ++   相同   log   images   

实验任务一:多态实现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系统\n选择语言:1 中文\t2 英文");

             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 取款\n2 存款\n3 转账\n4 退卡");

                   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\n2 cunkuan\n3 zhuanzhang\n4 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父类中的对象进行自加,对子类没有影响,调用子类方法,对象,输出子类的值。

java多态动手动脑

标签:java   demo   转化   out   手动   ++   相同   log   images   

原文地址:http://www.cnblogs.com/jokerr/p/6079107.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!