java-Object-oriented (ii)

Source: Internet
Author: User

I've been watching these days . Headfirst when, suddenly feel the object-oriented features, a little understanding of ambiguity, so in this review again, deepen the impression.

The previous blog (http://blog.csdn.net/u010539352/article/details/39500455) has been explained in 3 an object-oriented feature, the rest is explained below.

Polymorphism: If a person wants to keep a pet, want to keep a dog, want to keep a dog, to have a cat to keep a cat, how to achieve? That uses polymorphism, which can be randomly switched, without changing the code in the "People" class, with dynamics and flexibility. Here are some examples:

Class Animal {private String name;    Animal (String name) {this.name = name;}  public void Enjoy () {System.out.println ("barking ...");  }} class Cat extends Animal {private String eyescolor;  Cat (String n,string c) {super (n); eyescolor = C;}  public void Enjoy () {System.out.println ("cat barking ...");  }}class Dog extends Animal {private String furcolor;   Dog (String n,string c) {super (n); furcolor = C;}  public void Enjoy () {System.out.println ("barking dogs ...");    }}class Lady {private String name;     Private Animal Pet;    Defines a parent pet, which can be a dog pet, or a cat pet, with dynamic, most flexible Lady (String name,animal pet) {this.name = name; This.pet = pet; } public void Mypetenjoy () {Pet.enjoy ();}}        public class Test {public static void main (String args[]) {Cat c = new Cat ("CatName", "Blue");        Dog d = new Dog ("Dogname", "Black");        Lady L1 = new Lady ("L1", c);        Lady L2 = new lady ("L2", D);           L1.mypetenjoy (); Polymorphism, the actual new method of coming out of the object, so is the cat's enjoy () method L2.mypetEnjoy (); Dog's Enjoy () method}}
Show Results:

Explanation: During the runNewout of that object, we'll call whose method. During Operation weNewIt's a dog and a cat.enjoymethod, so the call is for the cat and the dogenjoymethod. Have a dynamic nature. So running out is the girl keeping the cat happy, so the cat'senjoyThe method was run, and the result: the cat cry... ..the girl keeps the dog happy, so the cat'senjoyThe method was run, the result: Barking dogs... ..Now if you add another bird, you don't have to change the original code, adding a class directly, extensibility.

This is summarized as follows: When calling a method using polymorphic mode, first check if there is a method in the parent class, if there is no compile error, if there is another method to call the subclass of that same name. So polymorphism must have 3 conditions: To have inheritance, to have overrides, a parent class reference to a subclass object.

as can be seen from the polymorphic example, the Enjoy method of the parent class is not necessarily implemented because the subclass has to be rewritten, and the parent class is somewhat redundant, so it uses abstractions.

Abstract: Using Abstract keyword adornments. Here are some examples:

Abstract class Animal {//abstraction type private String name;  Animal (String name) {this.name = name;}        public abstract void enjoy (); Abstract method, only the definition is not implemented.  } class Cat extends Animal {private String eyescolor;  Cat (String n,string c) {super (n); eyescolor = C;}  public void Enjoy () {System.out.println ("cat barking ...");  }}class Dog extends Animal {private String furcolor;   Dog (String n,string c) {super (n); furcolor = C;}  public void Enjoy () {System.out.println ("barking dogs ...");    }}class Lady {private String name;               Private Animal Pet;    Lady (String name,animal pet) {this.name = name; This.pet = pet; } public void Mypetenjoy () {Pet.enjoy ();}}        public class Test {public static void main (String args[]) {Cat c = new Cat ("CatName", "Blue");        Dog d = new Dog ("Dogname", "Black");        Lady L1 = new Lady ("L1", c);        Lady L2 = new lady ("L2", D);                   L1.mypetenjoy ();               L2.mypetenjoy (); }}

Explanation: It can be seen that with Abstract the class of the keyword is defined for the abstract class, and the method is defined for the abstract method. Only the definition is not implemented. Implemented in subclasses.

Therefore, abstract classes must be inherited, and abstract methods must be rewritten.

The above-mentioned inheritance, rewriting, abstraction, polymorphism, with these several combinations can achieve very perfect programming, but in the ointment isJavacan not achieve multiple inheritance, here we use the interface.

Interface: is a special abstract class that contains only the definitions of constants and methods, without the implementation of variables and methods. Several unrelated classes can implement the same interface. A class can implement multiple interfaces. Equivalent to multiple inheritance. Example:

Interface Animal {            //interface class public void call ();} Interface Animal1 {            //interface class public void run ();} Class Dog implements Animal {     ///Dog class implements the interface private string Name;dog (string name) {this.name = name;} public void Call () {             //Implements the interface method System.out.println ("barking ...");} Public String GetName () {return name;} public void Run () {               //Implements the interface method System.out.println ("Dog runs ...");} Class Cat implements Animal {        //Cat class implementation interface private String name; Cat (String name) {this.name = name;} public void Call () {               //Implements the interface method System.out.println ("Cat cry ...");} Public String GetName () {return name;} public void Run () {                 //Implements the interface method System.out.println ("Cat ran ...");} public class Interface {public    static void Main (String args[]) {        cat c = new Cat ("Cat");        Dog d = new Dog ("Dog");        C.call ();        C.run ();        D.call ();        D.run ();                        }}
Show Results:

Explanation: There is no implementation of the Write method in the interface class, but the normal class must override all methods of the interface.

Summary: Overrides must have inheritance.

polymorphism has to be inherited and rewritten.

abstractions must have inheritance and overrides. Use abstraction to achieve polymorphism.

The interface must have an implementation.

In this way, the idea is a lot clearer. There is no harm in summing up.


java-Object-oriented (ii)

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.