Dark Horse: Object-oriented 6

Source: Internet
Author: User

1. Polymorphism
Polymorphism definition: it can be understood as multiple manifestations of the existence of things.
First, the embodiment of Polymorphism
The reference of the parent class points to its own subclass object.
The parent class can also receive its own subclass objects.
Second, the premise of Polymorphism
It must be related to the class. Either inherit or implement.
There is usually another premise: coverage exists.
Third, benefits of Polymorphism
Improves program scalability.
Fourth, disadvantages of Polymorphism
Only the reference of the parent class can be used to access members in the parent class.
Fifth, application of Polymorphism
Encapsulate polymorphism in a tool class

Abstract class animal {abstract void eat ();} class cat extends animal {public void eat () {system. out. println ("fish");} public void catchmouse () {system. out. println ("rat") ;}} class dog extends animal {public void eat () {system. out. println ("eat bone") ;}} public class duotaidemo1 {public static void main (string [] ARGs) {function (new CAT ()); function (new dog ();} public static void function (animal a) {. eat ();}}

2. polymorphism --- Transformation
Downward transition: The subclass type is upgraded to the parent class.
Downward Transformation: The subclass type is first upgraded to the parent class, and then forcibly converted to its own type. If you want the subclass to have a special function, you can forcibly convert the reference of the parent class to the subclass type.
Do not convert a parent class object to a subclass type. What we can convert is that when the parent class reference points to its own subclass object, the parent class reference can be promoted to the parent class type, it can also be forcibly converted to the subclass type.
The beginning and end of polymorphism are all sub-class objects being changed.

Abstract class animal {abstract void eat ();} class cat extends animal {public void eat () {system. out. println ("fish");} public void catchmouse () {system. out. println ("rat") ;}} class dog extends animal {public void eat () {system. out. println ("eat bone");} public void kanjia () {system. out. println ("") ;}} public class duotaidemo1 {public static void main (string [] ARGs) {// The class is upgraded to the parent class animal = new cat (); Function (animal);} public static void function (animal) {animal. eat (); If (animal instanceof cat) {// downward transformation, forced to convert the reference of the parent class to the subclass type, used to call the unique functions of the subclass. Cat cat = (CAT) animal; CAT. catchmouse ();} else if (animal instanceof dog) {dog = (DOG) animal; dog. kanjia ();}}}

3. Application of Polymorphism
Encapsulate polymorphism in a tool class.

Abstract class student {public abstract void Study (); Public void sleep () {system. out. println ("Sleep") ;}} class dostudent {public void dosome (student Stu) {Stu. study (); Stu. sleep () ;}} class basestudent extends student {public void Study () {system. out. println ("Base Study");} public void sleep () {system. out. println ("sitting and sleeping") ;}} public class duotaidemo2 {public static void main (string [] ARGs) {dostudent DS = new dostudent (); DS. dosome (New basestudent ());}}

4. Example of a multi-state Motherboard

Interface PCI {public void open (); Public void close ();} class mainboard {public void run () {system. out. println ("mainboard Run");} public void getpci (pCI p) {P. open (); p. close () ;}} class shengka implements PCI {public void open () {system. out. println ("sound card running");} public void close () {system. out. println ("sound card ended") ;}} public class duotaidemo3 {public static void main (string [] ARGs) {mainboard M = new mainboard (); M. run (); M. getpci (New shengka ());}}

5. Object Class --- equals ()
Object: the direct or indirect parent class of all objects.
This class defines the functions of all objects.

class Demo{ private int num; Demo(int num){  this.num = num; } public boolean equals(Object obj){  if(!(obj instanceof Demo)){   return false;   }  Demo d = (Demo) obj;  return this.num==d.num; }}class Person{ }public class ObjectDemo01{  public static void main(String[] args){    Demo d1 = new Demo(4);    Demo d2 = new Demo(4);    Person p = new Person();    System.out.println(d1.equals(p));    System.out.println(d1.equals(d2));      }}

 

Summary:
1. Polymorphism: upward and downward transformations.
2. Object: equals (Object OBJ), tostring ().

 

 

 

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.