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

java动手动脑多态

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

标签:turn   intvalue   ret   ati   xtend   修改   demo   运算   输入密码   

实验任务一:阅读并运行一下代码

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)子类分配了空间,调用自己的构造方法,输出;

(3)子类赋值给父类,当父子类有一样的方法,父类变量引用一个子类对象,由于对象是子类,父类调用的是子类的方法,输出的是子类的值;

(4)父类中的变量进行自加运算,而且调用的还是子类的构造方法;

(5)((Child)parent).myValue++将parent对象强制转化成Child,所以指向的是Child类中的变量,输出的也是。

修改ParentChildTest.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();

 

((Child)parent).myValue++;

parent.printValue();

 

parent.myValue++;

parent.printValue();

 

}

}

 

class Parent{

public int myValue=200;

public void printValue() {

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

}

}

class Child extends Parent{

public int myValue=300;

public void printValue() {

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

}

}

2.模拟ATM操作系统:

package demo;

import java.util.Scanner;

 

class M{

       String name;

       String date;

       String password;

       int yu;

       String number;

       public M(String name,String date,String password,int yu,String number)

       {

              this.name=name;

              this.date=date;

              this.password=password;

              this.yu=yu;

              this.number=number;

       }

       public M(){}

       public void me(){}

}

class Qukuan extends M{

       public Object pasword;

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

              name=str;

              date=str1;

              password=str2;

              yu=m;

              number=str3;

       }

       public void me(){

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

              Scanner in=new Scanner(System.in);

              int p=in.nextInt();

              M a=new M(name,date,password,yu,number);

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

                     me();

              }

       }

}

class Cunkuan extends M{

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

              name=str;

              date=str1;

              password=str2;

              yu=m;

              number=str3;

       }

       public void me(){

              Scanner in=new Scanner(System.in);

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

              int n=in.nextInt();

              M a=new M(name,date,password,yu,number);

              a.yu+=n;

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

       }

}

class Zhuanzhang extends M{

       String kahao;

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

              name=str;

              date=str1;

              password=str2;

              yu=m;

              number=str3;

       }

       public void me(){

              M a=new M(name,date,password,yu,number);

              M B=new M("ylx","2016.11.18","123456",10000,"97093963011");

              Scanner in=new Scanner(System.in);

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

              String s=in.next();

              if(s.equals(number)){

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

              int w=in.nextInt();

              a.yu-=w;

              B.yu+=w;

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

              }

              else

                     System.out.println("卡号输入错误!");

       }

}

public class Atm {

    public static void main(String args[]){

             Scanner in=new Scanner(System.in);

            Qukuan a1=new Qukuan("ylx","2016.11.18","123456",10000,"97093963011");

              Cunkuan a2=new Cunkuan("Ylx","2016.11.17","123456",10000,"11401398081");

              Zhuanzhang a3=new Zhuanzhang("mdz","2016.11.16","123456",10000,"98299363711");

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

                   String str=in.next();

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

                   while(true){

                   System.out.println("1 取款\n2 存款\n3 转账\n4退出");

                   int w=in.nextInt();

                   if(w==1)

                          a1.me();

                   else if(w==2)

                          a2.me();

                   else if(w==3)

                          a3.me();

                   else

                          break;

                   }

                   }

                   else

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

    }

           

}

截图:

技术分享

技术分享

 

技术分享

 

 

 

java动手动脑多态

标签:turn   intvalue   ret   ati   xtend   修改   demo   运算   输入密码   

原文地址:http://www.cnblogs.com/ylx111/p/6079054.html

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