• java-08多态与异常处理


    1.运行以下代码:

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

    上述代码的特点是:子类和父类定义了一模一样的字段和方法。

    第一个是父类自身的对象,同时调用了父类自己的方法。第二个是子类自身的对象调用了子类自己的方法。

    第三个父类变量引用子类对象调用子类的方法。第四个引用了子类的对象,调用了子类的方法。

    第五个通过类型转换改变了子类的变量,用了子类的方法。

    当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。

    2.

    import java.util.Scanner;
    
    class Account{
        String account;
        String name;
        String date;
        int key;
        double money;
        
        public Account(String a,String b,String c,int d,double e)
        {
            account=a;
            name=b;
            date=c;
            key=d;
            money=e;
        }
        
    }
    
    public class Option {
        public static void main(String[] args) {
            Account aa=new Account("622888040311","zxw","2016.11.15",123456,1000);
            int a,b;
            System.out.println("请选择语言:1.中文 2.英文");
            Scanner in =new Scanner(System.in);
            a=in.nextInt();
            System.out.println("请输入密码:");
            Scanner in1 =new Scanner(System.in);
            b=in1.nextInt();
            if(b==aa.key){
                while(true){
                    
                int choose;
                System.out.println("请选择操作:1.存款 2.取款 3.转账汇款 4.修改密码 5.查询余额");
                Scanner in2 =new Scanner(System.in);
                choose=in2.nextInt();
                if(choose==1){
                    System.out.println("请输入存款金额:");
                    Scanner in3 =new Scanner(System.in);
                    double cun=in3.nextInt();
                    aa.money=aa.money+cun;
                    continue;
                }
                else if(choose==2){
                    System.out.println("请输入取款金额:");
                    Scanner in4=new Scanner(System.in);
                    double qu=in4.nextDouble();
                    if(aa.money<qu){
                        System.out.println("卡内余额不足");
                    }
                    aa.money=aa.money-qu;
                    continue;
                }
                else if(choose==3){
                    System.out.println("请输入要转账的卡号:");
                    Scanner in6=new Scanner(System.in);
                    String hang=in6.nextLine();
                    if(hang.equals(aa.account)){
                        System.out.println("请输入转账金额:");
                        Scanner in5=new Scanner(System.in);
                        double zhuan=in5.nextDouble();
                        aa.money=aa.money-zhuan;
                    }
                }
                else if(choose==4){
                    System.out.println("请输入原密码:");
                    Scanner in7=new Scanner(System.in);
                    int mi=in7.nextInt();
                    if(mi==aa.key){
                        System.out.println("请输入新密码:");
                        Scanner in8=new Scanner(System.in);
                        int mi2=in8.nextInt();
                        aa.key=mi2;
                    }
                }
                else if(choose==5){
                    System.out.println("现在的余额为:"+aa.money);
                }
                
                }
            }
            else
                System.out.println("密码错误,该卡已被锁定,无法操作");
    
        }
    
    }
  • 相关阅读:
    设计模式 5 —— 工厂模式
    Java 集合系列 14 hashCode
    Java 集合系列 13 WeakHashMap
    java 多线程——quartz 定时调度的例子
    memcached 学习 1—— memcached+spring配置
    vivado SDK之找不到"platform.h"
    ubuntu上第一个hello程序
    FPGA设计中的异步复位、同步释放思想
    异步fifo的Verilog实现
    zedboard上首个驱动实践——Led
  • 原文地址:https://www.cnblogs.com/lzxw/p/6079387.html
Copyright © 2020-2023  润新知