• Java 多态


    一、多态.类型转换

     1 class Mammal{}
     2 class Dog extends Mammal {}
     3 class Cat extends Mammal{}
     4 
     5 public class TestCast
     6 {
     7     @SuppressWarnings("unused")
     8     public static void main(String args[])
     9     {
    10         Mammal m;
    11         Dog d=new Dog();
    12         Cat c=new Cat();
    13         m=d;
    14         //d=m;
    15         d=(Dog)m;
    16         //d=c;
    17         //c=(Cat)m;
    18 
    19     }
    20 }

    d=m; d=c; c=(Cat)m;这三句有错。再看另一段代码:

     1 public class ParentChildTest {
     2     public static void main(String[] args) {
     3         Parent parent=new Parent();
     4         parent.printValue();
     5         Child child=new Child();
     6         child.printValue();
     7         
     8         parent=child;
     9         parent.printValue();
    10         
    11         parent.myValue++;
    12         parent.printValue();
    13         
    14         ((Child)parent).myValue++;
    15         parent.printValue();
    16         
    17     }
    18 }
    19 
    20 class Parent{
    21     public int myValue=100;
    22     public void printValue() {
    23         System.out.println("Parent.printValue(),myValue="+myValue);
    24     }
    25 }
    26 class Child extends Parent{
    27     public int myValue=200;
    28     public void printValue() {
    29         System.out.println("Child.printValue(),myValue="+myValue);
    30     }
    31 }

      

      总结:当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调                 用子类型的方法,是父类型的,它就调用父类型的方法。 这个特性实际上就是面向对象“多态”特性的具体表现。如果子类与父类有相同的字段,则子类中的字段会代替或               隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。 如                 果子类被当作父类使用,则通过子类访问的字段是父类的!

    二、怎样判断对象是否可以转换

     1 public class TestInstanceof
     2 {
     3     @SuppressWarnings("unused")
     4     public static void main(String[] args) 
     5     {
     6         //声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类
     7         //但hello变量的实际类型是String
     8         Object hello = "Hello";
     9         //String是Object类的子类,所以返回true。
    10         System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));
    11         //返回true。
    12         System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
    13         //返回false。
    14         System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math));
    15         //String实现了Comparable接口,所以返回true。
    16         System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable));
    17         String a = "Hello";
    18         //String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过
    19         //System.out.println("字符串是否是Math类的实例:" + (a instanceof Math));
    20     }
    21 }

      

    三、TestPolymorphism.java

     1 class Parent01
     2         
     3 {
     4     public int value=100;
     5     public void Introduce(){
     6             System.out.println("I'm father");
     7     }
     8 }
     9 class Son extends Parent01
    10 {
    11     public int value=101;
    12     public void Introduce(){
    13         System.out.println("I'm son");
    14     }
    15 }
    16 class Daughter extends Parent01
    17 {
    18     public int value=102;
    19     public void Introduce(){
    20         System.out.println("I'm daughter");
    21     }
    22 }
    23 public class TestPolymorphism
    24 {
    25     public static void main(String args[]){
    26         Parent01 p=new Parent01();
    27         p.Introduce();
    28         System.out.println(p.value);
    29         p=new Son();
    30         p.Introduce();
    31         System.out.println(p.value);
    32         p=new Daughter();
    33         p.Introduce();
    34         System.out.println(p.value);
    35     }
    36 }

      

    四、多态的方法模拟ATM操作流程

      1 import java.util.*;
      2 public class ShowATM {
      3     @SuppressWarnings("resource")
      4     public static void main(String[] args) {
      5         Scanner in=new Scanner(System.in);
      6         ATM atm=new ATM();
      7         int choose=0,num=0;
      8         String pw="";
      9         next:while(true){
     10             System.out.println("是否进入账户(0否1是):");
     11             int kk=in.nextInt();
     12             if(kk==0) break;
     13             else if(kk!=1){
     14                 System.out.println("输入错误!");
     15                 continue;
     16             }
     17             System.out.println("输入账户密码:");
     18             pw=in.next();
     19             if(atm.ifPass(pw)){
     20                 while(true){
     21                     showFace();
     22                     choose=in.nextInt();
     23                     switch(choose){
     24                     case 1:
     25                         System.out.println("输入存款金额:");
     26                         num=in.nextInt();
     27                         atm.save(num);
     28                         System.out.println("存款成功!");
     29                         System.out.println("当前余额:"+atm.getRest()+"元");
     30                         break;
     31                     case 2:
     32                         System.out.println("请选择:");
     33                         int a[]={100,500,1000,1500,2000,5000};
     34                         for(int i=0;i<a.length;i++)
     35                             System.out.println((i+1)+"."+a[i]+"元");
     36                         System.out.println("7.其他");
     37                         int ch=in.nextInt();
     38                         if(ch>=1&&ch<=6){
     39                             if(atm.withdraw(a[ch-1]))
     40                                 System.out.println("取款成功!");
     41                             else
     42                                 System.out.println("余额不足!");
     43                         }
     44                         else if(ch==7){
     45                             System.out.println("请输入取款金额:");
     46                             num=in.nextInt();
     47                             if(atm.withdraw(num))
     48                                 System.out.println("取款成功!");
     49                             else 
     50                                 System.out.println("余额不足!");
     51                         }
     52                         else 
     53                             System.out.println("输入有误!");
     54                         System.out.println("当前余额:"+atm.getRest()+"元");
     55                         break;
     56                     case 3:
     57                         System.out.println("账户号:");
     58                         String s=in.next();
     59                         System.out.println("转账金额:");
     60                         int i=in.nextInt();
     61                         if(atm.transfer(s, i))
     62                             System.out.println("转账成功!");
     63                         else
     64                             System.out.println("转账失败!");
     65                         System.out.println("当前余额:"+atm.getRest()+"元");
     66                         break;
     67                     case 4:
     68                         System.out.println("输入六位数密码:");
     69                         String p=in.next();
     70                         atm.setPassword(p);
     71                         break;
     72                     case 5:
     73                         System.out.println("当前余额:"+atm.getRest()+"元");
     74                         break;
     75                     default:
     76                         continue next;
     77                     }
     78                 }
     79             }
     80             else
     81                 System.out.println("密码错误!");
     82         }
     83     }
     84     //显示菜单方法
     85     public static void showFace(){
     86         System.out.println("********************");
     87         System.out.println("    1.存款:");
     88         System.out.println("    2.取款:");
     89         System.out.println("    3.转账汇款:");
     90         System.out.println("    4.修改密码:");
     91         System.out.println("    5.查询余额:");
     92         System.out.println("    6.退卡:");
     93         System.out.println("********************");
     94         System.out.println("请选择:");
     95     }
     96 }
     97 
     98 class PersonalAccount{
     99     private String passWord="123456";//密码
    100     @SuppressWarnings("unused")
    101     private String number;//银行卡号
    102     private int money=0;
    103     public int getMoney(){return money;}//余额
    104     public void setPw(String s){passWord=s;}//设置密码
    105     public void addMoney(int x){money+=x;}//加钱
    106     public void minusMoney(int x){money-=x;}//减钱
    107     public boolean whetherPwTrue(String s){//密码是否正确
    108         if(s.equals(passWord))
    109             return true;
    110         else return false;
    111     }
    112 }
    113 abstract class PATM{
    114     public abstract boolean withdraw(int x);//取款
    115     public abstract void save(int x);//存款
    116     public abstract boolean transfer(String s,int x);//转账
    117     public abstract boolean ifPass(String s);//判断输入的密码是否正确
    118     public abstract int getRest();//查询余额
    119     public abstract void setPassword(String s);//设置密码
    120 }
    121 class ATM extends PATM{
    122     private String numbers[]={"6227000000000000071","6227000000000000072",
    123             "6227000000000000073","6227000000000000074"};//数据库中已有的账户卡号
    124     private PersonalAccount account=new PersonalAccount();
    125     public boolean withdraw(int x) {
    126         if(x>account.getMoney())
    127             return false;
    128         else{
    129             account.minusMoney(x);
    130             return true;
    131         }
    132     }
    133     public void save(int x) {
    134         account.addMoney(x);
    135     }
    136     public boolean transfer(String s, int x) {
    137         //转账
    138         //先判断转到账户号是否存在
    139         //再判断余额是否足够
    140         boolean flag=false;
    141         for(int i=0;i<numbers.length;i++)
    142             if(s.equals(numbers[i])) flag=true;
    143         if(x>account.getMoney()) flag=false;
    144         if(x<=account.getMoney()&&flag) account.minusMoney(x);;
    145         return flag;
    146     }
    147     public boolean ifPass(String s) {
    148         return account.whetherPwTrue(s);
    149     }
    150     public int getRest() {
    151         return account.getMoney();
    152     }
    153     public void setPassword(String s) {
    154         account.setPw(s);        
    155     }
    156 }

      

      

      

      

      

      

  • 相关阅读:
    6年工作总结反思
    中级美语 L013:Health Comes First 解析
    中级美语 L011:Power without Pollution 解析
    中级美语 L009:Be Thoughtful 解析
    中级美语 L007:Doctor Death 解析
    中级美语 L005:Bungee jumping 解析
    中级美语 L003:The City of Song 解析
    中级美语 L001:Rome Wasn't Built in a Day 解析
    初级美语 L147:Television Addiction 解析
    初级美语 L145:Billy's Goal in Life 解析
  • 原文地址:https://www.cnblogs.com/yifengyifeng/p/6071794.html
Copyright © 2020-2023  润新知