I. Type conversions
classmammal{}classDogextendsMammal {}classCatextendsmammal{} Public classtestcast{ Public Static voidMain (String args[]) {mammal m; Dog D=NewDog (); Cat C=NewCat (); M=E; //d=m; ErrorD=(Dog) m; //D=c; Error//c= (Cat) m; }}
Summary: Subclasses can be assigned to the parent class, changing the method of the parent class, but not changing the variables in the parent class.
Two. Inheritance of classes
Public classParentchildtest { Public Static voidMain (string[] args) {Parent parent=NewParent (); Parent.printvalue (); Child Child=NewChild (); Child.printvalue (); Parent=Child ; Parent.printvalue (); Parent.myvalue++; Parent.printvalue (); ((child) parent). MyValue++; Parent.printvalue (); }}classparent{ Public intmyvalue=100; Public voidPrintvalue () {System.out.println ("Parent.printvalue (), myvalue=" +myvalue); }}classChildextendsparent{ Public intmyvalue=200; Public voidPrintvalue () {System.out.println ("Child.printvalue (), myvalue=" +myvalue); }}
Operation Result:
Conclusion: Subclasses can overwrite a parent class, but the value of a variable in the parent class is not changed, access to a variable in the parent class is available by Super, and the quilt class is overridden. When the parent class is overridden, the variables in the parent class are changed while the variables in the parent class are manipulated, but the output still outputs variables that overwrite the child classes of the parent class. An operation that can be coerced to a child class before the parent class operation, i.e. (child) parent.myvalue++, will change to overwrite the subclass of the parent class
Experimental Task One:
//ATM//langlangbai,2016.11.18ImportJava.util.Scanner; Public classATM1 {PrivateString account; PrivateString Mima; PrivateString name; PrivateString Date; Private DoubleYuE; ATM1 (String a,string b,string c,string D,Doublee) { Account=A; Mima=b; Name=C; Date=D; YuE=e; } voidSetyue (Doublem) {YuE=m; } voidSetmima (String a) {Mima=A; } String GetName () {returnname;} String Getmima () {returnMima;} DoubleGetyue () {returnYuE;} Static voidMeno1 () {System.out.println ("1, deposit \n2, withdrawal \n3, transfer remittance \n4, change password \n5, check balance \n0, exit"); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner in1=NewScanner (system.in); Scanner in2=NewScanner (system.in); Scanner in3=NewScanner (system.in); Scanner in4=NewScanner (system.in); Scanner In5=NewScanner (system.in); Atm1[] a=NewAtm1[20]; intNum=1; intJ; a[0] =NewATM1 ("12345678912", "123456", "White Wave", "2016\11\17", 1000); System.out.println ("Please insert card: \ n Card Successful"); System.out.println ("Please enter your password:"); String mi=In1.next (); for(inti=0;i<num;i++) { if(mi. Equals (A[i].getmima ())) {System.out.println ("Password is correct"); Meno1 (); DoubleQian_num; J=In2.nextint (); while(j!=0) { if(j==1) {System.out.println ("Please put in the banknotes:"); Qian_num=in3.nextdouble (); A[i].setyue (A[i].getyue ()+qian_num); System.out.println ("Saving for Success"); } if(j==2) {System.out.println ("Please enter the amount to be withdrawn: 1, 100 2, 500 3, 1000 4, 1500 5, 2000 6, 5000 7, other 8, return card"); intK; 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 ("Please enter the account you want to transfer:"); String A1; A1=In5.next (); System.out.println ("Please enter the amount you want to transfer:"); String A2; A2=In5.next (); System.out.println ("Successful transfer!" :"); } if(j==4) {System.out.println ("Please enter the modified password:"); String A1; A1=In5.next (); A[i].setmima (A1); } if(j==5) {System.out.println ("Balance:" +A[i].getyue ()); } meno1 (); J=in2.nextint ();}} Else if(i = =num) System.out.println ("The password is wrong and the card is locked. "); } in1.close (); In2.close (); In3.close (); In4.close (); In5.close (); }}
Run:
Java language Multi-state after-class archive job