• 多态


     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)源代码:

      import java.util.Vector;
      public class zoo4 {
          public static void main(String[] args) {
               Feeder f = new Feeder("小李");
               Vector<Animal> ans = new Vector<Animal>();         
               ans.add(new Lion());          
              for (int i = 0; i < 10; i++) {
                  ans.add(new Monkey());
              }
              for (int i = 0; i < 5; i++) { 
                  ans.add(new Pigeon());
    }
    f.feedAnimals(ans); } } class Feeder { public String name; Feeder(String name) { this.name = name; } public void feedAnimals(Vector<Animal> ans) { for (Animal an : ans) { an.eat(); } } } interface Animal { public abstract void eat(); } class Lion implements Animal { public void eat() { System.out.println("狮子"); } } class Monkey implements Animal { public void eat() { System.out.println("猴子"); } } class Pigeon implements Animal { public void eat() { System.out.println("鸽子"); } }

     

     

    三.动手动脑

    1)源代码:

       import java.util.*;
      public class Ex1115 {
          public static void main(String[] args) {
               Account a = new Account("11111111111","dsdf","20161115","256398",23.6);
               a.jiemian();
          }
       }
      
      class Account
     {
          private static final String JOptionPane = null;
         private  static String Id;
          private static String name;
         private static String date;
        private static String mima;
         private static double extra;
         
          Account(String i,String n,String d,String m,double e)//构造函数
         {
           Id=i;
              name=n;
             date=d;
              mima=m;
              extra=e;
          }
          
          public void set_Id(String i){
              Id=i;
          }
          public void set_name(String n){
              name=n;;
         }
          public void set_date(String d){
              date=d;
          }
          public void set_mima(String m){
              mima=m;
          }
          public void set_extra(double e){
              extra=e;
          }
          
          public String get_Id(){
              return Id;
          }
          public String get_name(){
              return name;
          }
          public String get_date(){
              return date;
          }
          public String get_mima(){
              return mima;
          }
          public double get_extra(){
              return extra;     }
         
          
          public static void jiemian()//界面
          {
              System.out.println("请选择界面(1.中文2.英文):");
             Scanner sc=new Scanner(System.in);
              String num1=sc.nextLine();
              int num11=Integer.parseInt(num1);
             
              if(num11==1){
                  
                  Scanner sc1=new Scanner(System.in);
                  int num=Integer.parseInt(sc1.nextLine());
      
                  while(true)
                  {
                          System.out.println("请选择要进行的操作:");
                          System.out.println("1.取款");
                          System.out.println("2.存款");
                          System.out.println("3.转账");
                          System.out.println("4.修改密码");
                         System.out.println("5.查询余额");
                         System.out.println("6.退卡");
                          
                    if(num==1)
                       qukuan();
                    else if(num==2)
                          cunkuan();
                    else if(num==3)
                        zhuan();
                     else if(num==4)
                        xiugai();
                    else if(num==5)
                          chaxun();
                     else if(num==6){
                         System.out.println("退卡成功!");
                     }
                 }
             }
            
            else{
                 
           }
         
        }
         
         public static void cunkuan()//存款
         {
             System.out.println("请输入存款金额:");
            Scanner sc = new Scanner(System.in);
             int jine = Integer.parseInt(sc.nextLine());
            
             extra += jine;
            System.out.println("您现在的金额为:" + extra);
        }
         
         public static void qukuan()//取款
         {
             System.out.println("请输入取款金额");
             String output="";
            output += "100  "
                     + "
    500 "
                    + "
    1000  "
                     + "
    1500 "
                     + "
    2000 "
                     + "
    5000  "
                     + "
    其他金额(请输入0)"
                     + "
    返回操作(请输入-1) "
                     + "
    退卡(请输入-2)";
            System.out.println(output);
     
             Scanner sc=new Scanner(System.in);
             int num=Integer.parseInt(sc.nextLine());
            
             switch(num)
             {
             case 100:{
               
            }
                 extra -= 100;break;
             case 500:
                 extra -= 500;break;
             case 1000:
                 extra -= 1000;break;
             case 1500:
                extra -= 1500;break;
            case 2000:
                 extra -= 2000;break;
            case 5000:
                 extra -= 5000;break;
             case 0:
             {
                System.out.println("请输入取款金额:");
                 Scanner sc1=new Scanner(System.in);
                 int jine=Integer.parseInt(sc1.nextLine());
                extra -= jine;
                sc1.close();
                break;
            }
            case -1:
                 System.out.println("已返回操作界面");break;
             case -2:
                 System.out.println("退卡成功!");break;
             }
        }
         
         public static void zhuan()//转账汇款
         {
             System.out.println("请输入转账行号:");
            Scanner sc = new Scanner(System.in);
             String hanghao = sc.nextLine();
             
             Scanner sc1 = new Scanner(System.in);
             int jine = Integer.parseInt(sc1.nextLine());
             
            extra -= jine;
             
            System.out.println("转账成功!");
         }
         
        public static void xiugai()//修改密码
        {
            System.out.println("请输入要修改的密码:");
             Scanner sc1 = new Scanner(System.in);
            String mi = sc1.nextLine();
             mima = mi;
         }
         
         public static void chaxun()//查询余额
        {
            System.out.println("余额为:"+extra);
         }
        
    }
    结果截图:
     
  • 相关阅读:
    雨林木风操作系统有感
    Bitcoin P2P 虚拟货币原理详解
    SVN switch 用法详解
    Bitcoin P2P 货币:有史以来最危险的项目
    回旋线科普
    Mathematica 如何绘制双纵坐标轴的图像?
    c#数据库操作
    在自定义HttpHandler中无法使用Session
    VS2010安装因net framework4.0无法安装而失败的解决方法
    .net framework 4.0 0xc8000247错误解决
  • 原文地址:https://www.cnblogs.com/zhangbaohai/p/6079035.html
Copyright © 2020-2023  润新知