zoukankan      html  css  js  c++  java
  • Java课堂 动手动脑6

    .下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么?

    m=d;d=m;d=(Dog)m;d=c;c=(Cat)m;

    先进行自我判断,

    1、代码:

    class Mammal{}
    class Dog extends Mammal {}
    class Cat extends Mammal{}
    public class TestCast
    {
        public static void main(String args[])
        {
            Mammal m;
            Dog d=new Dog();
            Cat c=new Cat();
            
            //m=c;c=(Cat)m;
            
            //d=m;错误
            m=d;
            d=(Dog)m;
            //d=c;错误的
        }
    }

    2判断在代码里。

    
    

    3总结:子类能赋给父类,改变父类的方法,但不改变父类中的变量。父类赋值给子类,需要确定是给的那个子类,否则会出错。

    
    

    二.运行以下测试代码

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

    结论:子类能覆盖父类,但是父类中的变量的值是不改变的,访问父类中的变量时可用super来访问,反之则一直被子类覆盖。父类被覆盖时,对父类中的变量进行操作时,父类中的变量改变,但输出时仍输出覆盖父类的子类的变量。可以对父类操作前强制转化为对子类的操作,即(childParent.myValue++,这时改变的将是覆盖父类的子类

    三、ATM

    import java.util.Scanner;
    class Account
    {
        private String code    =null;   //信用卡号
         protected String key=null;   //客户密码
         protected double money   =0.0;   //卡里金额
         public Account(String code,String key,double money)
         {
          this.code=code;
          this.key=key;
          this.money=money;
         }
         String get_Code() {
              return code;
             }
         String get_Key() {
              return key;
             }
         double get_Money() {
              return money;
             }
         void set_Balance(double mon) {
              money -= mon;
             }
    }
    class make extends Account
    {
        public make(String code,String key,double money) {
            super(code,key,money);
        }     
         void Welcome()
         {
            Scanner a=new Scanner(System.in);
            System.out.println("你已进入,请输入密码");
            String key1=a.next();
            System.out.println(key);
            if(key1.equals(key))
            {
                System.out.println("密码正确,您已进入
    ");
            }
            else 
            {    
                System.out.println("您的密码错误,系统已退出!");
                System.exit(1);
            }      
          
         }
        void cunkuan()
        {
            Scanner a=new Scanner(System.in);
            System.out.println("请输入存款金额");
            double money1=a.nextDouble();
            this.money=money+money1;
            System.out.println("您以成功为你的账户存款"+this.money);
        }
        void qukuan()
        {
            Scanner a=new Scanner(System.in);
            System.out.println("取款选项
    100
    200
    500
    1000
    1500
    2000
    5000
    其他金额
    退卡
    ");
            System.out.println("请输入您需要的取款选项:");
            String aa=a.next();
            if(aa.equals("100"))
            {
                this.money=money-100;
                System.out.println("您以成功取款"+100+"请在取钱口取走你大现金。");
            }
            if(aa.equals("200"))
            {
                this.money=money-200;
                System.out.println("您以成功取款"+200+"请在取钱口取走你大现金。");
            }
            if(aa.equals("500"))
            {
                this.money=money-500;
                System.out.println("您以成功取款"+500+"请在取钱口取走你大现金。");
            }
            if(aa.equals("1000"))
            {
                this.money=money-1000;
                System.out.println("您以成功取款"+1000+"请在取钱口取走你大现金。");
            }
            if(aa.equals("1500"))
            {
                this.money=money-1500;
                System.out.println("您以成功取款"+1500+"请在取钱口取走你大现金。");
            }
            if(aa.equals("2000"))
            {
                this.money=money-2000;
                System.out.println("您以成功取款"+2000+"请在取钱口取走你大现金。");
            }
            if(aa.equals("5000"))
            {
                this.money=money-5000;
                System.out.println("您以成功取款"+5000+"请在取钱口取走你大现金。");
            }
            if(aa.equals("其他金额"))
            {
                System.out.println("请输入取款金额:");
                double r=a.nextDouble();
                this.money=money-r;
                System.out.println("您以成功取款"+r+"请在取钱口取走你大现金。");
            }
            if(aa.equals("退卡"))
            {
                System.out.println("请取走您的卡!");
                System.exit(1);
            }
            else
            {
                System.out.println("请取走您的卡!");
                System.exit(1);
            }
        }
        void zhuanZhang()
        {
            System.out.println("请输入转账金额");
            Scanner a=new Scanner(System.in);
            int aa=a.nextInt();
            if(aa<20000)
            {
                this.money=money-aa;
                System.out.println("转账操作成功!");
            }
            else{System.out.println("余额不足,转账失败!");}     
        }
        void caidan()
        {
            System.out.println("【存款---1】");
            System.out.println("【取款---2】");
            System.out.println("【余额---5】");
            System.out.println("【转账---3】");
            System.out.println("【退卡---4】");
            System.out.println("---------");
            System.out.println("请输入你的操作:");
            System.out.println("请选择你需要的服务:");
            Scanner a=new Scanner(System.in);
            int num=a.nextInt();
            switch(num) {
              case 1: cunkuan(); break;
              case 2: qukuan(); break;
              case 3: zhuanZhang(); break;
              case 4: System.exit(1); System.out.println("系统已退出!请取走您的卡!"); break;
              }
        }
    }
    public class ATM {
        public static void main(String[] args) {
            make peo=new make("1212","12345",20000);
            peo.Welcome();
            peo.caidan();
        }
    }

    总结:此程序于学生信息管理相似。改成多态不是很困难,通过查阅资料和我的努力暂时就做到了这里,而且编写这样的程序使我对多态有了进一步的了解,所以我一定会做完它的。

  • 相关阅读:
    Roce ofed 环境搭建与测试
    Ubuntu 1804 搭建NFS服务器
    Redhat 8.0.0 安装与网络配置
    Centos 8.1 安装与网络配置
    SUSE 15.1 系统安装
    VSpare ESXi 7.0 基本使用(模板、iso、SRIOV)
    VSpare ESXi 7.0 服务器安装
    open SUSE leap 15.1 安装图解
    KVM虚拟机网卡连接网桥
    GitHub Action一键部署配置,值得拥有
  • 原文地址:https://www.cnblogs.com/conquer-vv/p/6079568.html
Copyright © 2011-2022 走看看