Java hands-on brain (polymorphic)

Source: Internet
Author: User

1. Run the following test code:

public class Parentchildtest {public static void main (string[] args) {parent parent=new parent ();p Arent.printvalue (); Child child=new Child (); Child.printvalue ();p arent=child;parent.printvalue ();//The method that calls the subclass, in which the variable parent.myvalue++ of the subclass is used; Parent.printvalue ();((child) parent). myvalue++;p Arent.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);}}

  

Results:

Analysis: The first two objects of the parent class of new, so called when the parent's constructor method and the parent's value value, the second new child's class object, So the constructor method of child is called when the constructor is called and the value of child. Then the third is to assign the child to his parent, and when the constructor method is called, it calls the subclass child's constructor, but when the subclass is constructed, value is the value of the child, So is the subclass child of the construction method plus value=200. The fourth one is the value of the parent plus one, but the parent is still the previous child assigned value, so the call is still the child's construction method, value is also the value of child, so it is still 200. In the last one, the parent is assigned to the child class, so the parent is also part of the children, so the call is constructed by calling the subclass, and the value is added one at a time, so the output is 201.

Summarize:

when a subclass has the same method as the parent class, and a parent class variable refers to a subclass object, which method is called, determined by the object's own "true" type, that is, the object is a subtype, it calls the method of the subtype, it is the parent type, and it invokes the method of the parent type.

This feature is actually a concrete representation of the object-oriented "polymorphic" feature.

if the child class has the same field as the parent class , the fields in the subclass are substituted or hidden from the fields of the parent class, and the fields in the subclass are accessed (not the fields in the parent class). If the subclass method does want to access a field that is hidden in the parent class with the same name, it can be accessed with the Super keyword.

If the subclass is used as a parent class , the field accessed through the subclass is the parent class!

2, using polymorphic method to simulate ATM operation process.

Import Java.util.Scanner; public class Showatm {@SuppressWarnings ("resource") public static void main (string[] args) {ATM atm=new at       M ();       SHOWATM s=new Showatm ();       S.showface ();     Atm.select ();         }//Show menu method public static void Showface () {System.out.println ("********************");         System.out.println ("1. Deposit:");         System.out.println ("2. Withdrawals:");         System.out.println ("3. Transfer remittance:");         System.out.println ("4. Change Password:");         System.out.println ("5. Enquiry balance:");         System.out.println ("********************");     SYSTEM.OUT.PRINTLN ("Please select:"); }}class personalaccount{String account; String name; String date; String mima;double Yue; PersonalAccount (String account,string name,string date,string mima,double yue) {this.account=account;this.name=name; This.date=date;this.mima=mima;this.yue=yue;} String Getaccount () {return account;} String Getname () {return name;} String Getdata () {return date;} String Getmima () {return miMa;} Double Getyue () {return yue;} void Setaccount (String account) {This.account=account;} void Setname (String name) {this.name=name;} void Setdate (String date) {this.date=date;} void Setmima (String mima) {This.mima=mima;} void Setyue (Double yue) {This.yue=yue;}} Abstract class aatm{public abstract void Qukuan ();//withdrawal public abstract void Cunkuan ();//Deposit public abstract Vo ID Zhuanzhang ();//Transfer public abstract void Mima ();//Change Password public abstract void Yue ();//Balance} class ATM extends aatm{S    Canner in = new Scanner (system.in);    PersonalAccount a=new personalaccount ("1234567890123", "Han", "20161115", "970318", 1000); public void Qukuan () {System.out.println ("Available withdrawal amount:"); System.out.println ("100 Yuan"); System.out.println ("500 Yuan"); System.out.println ("1000 Yuan"); System.out.println ("1500 Yuan"); System.out.println ("2000 Yuan"); System.out.println ("5000 Yuan"); System.out.println ("Other amounts"); System.out.println ("Please enter the amount you want to withdraw:");d ouble q=in.nextdouble (); if (a.yue-q<0) {System.out.println ("Insufficient balance! ");} Else{system.out.println ("TakeSuccess! ");    A.yue=a.yue-q;}} public void Cunkuan () {System.out.println ("Please enter Deposit Amount:");d ouble q1=in.nextdouble ();  A.YUE=A.YUE+Q1; public void Zhuanzhang () {System.out.println ("Enter the line number to transfer:"); String H=in.next (); System.out.println ("Whether the name of the person you want to transfer is xxx?0: Yes, 1: No"), int x=in.nextint (), if (x==0) {System.out.println ("Please enter the amount to be transferred:"); Double z=in.nextdouble (); SYSTEM.OUT.PRINTLN ("Transfer success! "); A.yue=a.yue-z; System.out.println ("Your balance is:" +a.yue);} if (x==1) {System.out.println ("card number is wrong!    ");}} public void Mima () {System.out.println ("Please enter the card number you want to modify:");    String S=in.next ();    if (A.getaccount () equals (S)) {System.out.println ("Please enter the original password:");    String S1=in.next ();    if (A.getmima () equals (S1)) {System.out.println ("Please enter a new password:");    String S2=in.next ();    A.setmima (S2); SYSTEM.OUT.PRINTLN ("Modified successfully!    "); }}} public void Yue () {System.out.println ("balance:" +a.yue)} void Select () {boolean p=true;while (P = = true) {Scanner in = new Scanner (system.in); System.out.println ("Enter the action to be performed:"); int m = In.nextint (); if (m = = 1) {Qukuan (); conTinue;} if (m = = 2) {Cunkuan (); continue;} if (m = = 3) {Zhuanzhang (); continue;} if (m = = 4) {Mima (); continue;}  if (m = = 5) {Yue (); continue; }else {System.out.println ("has exited the system!    ");  p = false;     } in.close (); }  } }

  

Results:

3.

which of the following statements will cause a compilation error ? Why ? which will cause a run-time error ? Why ?

M=d;

D=m;

D= (Dog) m;

D=c;

C= (Cat) m;

Run the following code:

Class Mammal{}class Dog extends mammal {}class Cat extends Mammal{}public class testcast{public static void Main (String ar Gs[]) {mammal M;dog d=new Dog (); Cat c=new Cat (); m=d;d=m;d= (Dog) m;d=c;c= (cat) m;}

  

D=m,d=c throws an error because d is the object of the dog , andm is The object of the mammal , so this is equivalent to directly to the parent class object to the subclass to use coercion type conversion,c,d is the same level, the same subclass, cannot be assigned to each other.

Java hands-on brain (polymorphic)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.