Java语言多态课后归档作业
一.类型转换
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=d; //d=m; 出错 d=(Dog)m; //d=c; 出错 //c=(Cat)m; } }
总结:子类能赋给父类,改变父类的方法,但不改变父类中的变量。
二.类的继承
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来访问,反之则一直被子类覆盖。父类被覆盖时,对父类中的变量进行操作时,父类中的变量改变,但输出时仍输出覆盖父类的子类的变量。可以对父类操作前强制转化为对子类的操作,即(child)Parent.myValue++,这时改变的将是覆盖父类的子类
实验任务一:
//ATM //LANGLANGBAI,2016.11.18 import java.util.Scanner; public class ATM1 { private String Account; private String mima; private String name; private String Date; private double YuE; ATM1(String a,String b,String c,String d,double e){ Account = a; mima = b; name = c; Date = d; YuE = e; } void setYuE(double m) { YuE = m; } void setMima(String a){ mima = a; } String getName(){return name;} String getMima(){return mima;} double getYue(){return YuE;} static void meno1(){ System.out.println("1、存款\n2、取款\n3、转账汇款\n4、修改密码\n5、查询余额\n0、退出"); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in1 = new Scanner(System.in); Scanner in2 = new Scanner(System.in); Scanner in3 = new Scanner(System.in); Scanner in4 = new Scanner(System.in); Scanner in5 = new Scanner(System.in); ATM1[] a = new ATM1[20]; int num=1; int j; a[0] = new ATM1("12345678912","123456","白浪浪","2016\11\17",1000); System.out.println("请插卡:\n插卡成功"); System.out.println("请输入密码:"); String mi = in1.next(); for(int i=0;i<num;i++) { if(mi .equals(a[i].getMima()) ) { System.out.println("密码 正确"); meno1(); double qian_num; j=in2.nextInt(); while(j!=0) { if(j==1) { System.out.println("请放入钞票:"); qian_num=in3.nextDouble(); a[i].setYuE(a[i].getYue()+qian_num); System.out.println("存钱成功"); } if(j==2) { System.out.println("请输入要进取款的金额:1、100 2、500 3、1000 4、1500 5、2000 6、5000 7、其他 8、退卡"); int k; k=in2.nextInt(); if(k==1) a[i].setYuE(a[i].getYue()-100); else if(k==2) a[i].setYuE(a[i].getYue()-500); else if(k==3) a[i].setYuE(a[i].getYue()-1000); else if(k==4) a[i].setYuE(a[i].getYue()-1500); else if(k==5) a[i].setYuE(a[i].getYue()-2000); else if(k==6) a[i].setYuE(a[i].getYue()-5000); else if(k==7) {qian_num = in4.nextDouble(); a[i].setYuE(a[i].getYue()-qian_num);} } if(j==3) { System.out.println("请输入要转账的账号 :"); String a1; a1 = in5.next(); System.out.println("请输入要转账的金额 :"); String a2; a2 = in5.next(); System.out.println("转账成功! :"); } if(j==4) { System.out.println("请输入修改后的密码 :"); String a1; a1 = in5.next(); a[i].setMima(a1); } if(j==5) { System.out.println("余额 :"+a[i].getYue()); } meno1(); j=in2.nextInt(); } } else if(i == num) System.out.println("密码错误,该卡锁定。"); } in1.close(); in2.close(); in3.close(); in4.close(); in5.close(); } }
运行截图: