zoukankan      html  css  js  c++  java
  • 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)子类分配了空间,调用自己的构造方法,输出;

    (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 取款 2 存款 3 转账 4退出");

                       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("密码错误!");

        }

               

    }

    截图:

     

     

  • 相关阅读:
    Mysql 导入CSV数据 语句 导入时出现乱码的解决方案
    有用的 Mongo命令行 db.currentOp() db.collection.find().explain()
    MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法 -摘自网络
    MongoDB索引相关文章-摘自网络
    批量更新MongoDB的列。
    RabbitMQ 消费端 Client CPU 100%的解决办法
    php根据命令行参数生成配置文件
    php解释命令行的参数
    使用memcache对wordpress优化,提速
    python基础技巧综合训练题2
  • 原文地址:https://www.cnblogs.com/ylx111/p/6079054.html
Copyright © 2011-2022 走看看