Java 6th Chapter-Interface and Polymorphism Experiment 2 (puppy's state) __java

Source: Internet
Author: User

Experimental purposes:

The idea of interface programming, which is object-oriented programming, is to design an important class, not to be oriented to specific classes, but to interface, that is, the important data in the design class is the variable of the interface declaration rather than the object of the concrete class declaration.

Dog.java

public class Dog {
    dogstate state;//declaring interface variable public
     void Cry () {
        state.showstate ();//calling function inside the interface
    }
    public void Setstste (Dogstate s) {
        state=s;
    }
}

Dogstate.java

Public interface Dogstate {public
   abstract void showstate ();
}

Meetenemystate.java

The public class Meetenemystate implements dogstate{//the class that implements the interface

    @Override public
    void Showstate () {
         System.out.println ("I met the enemy barking." ");
    }
    
}

Meetfriendstate.java

The public class Meetfriendstate implements dogstate{//the class that implements the interface

    @Override public
    void Showstate () {
        System.out.println ("Welcome to meet friends shaking their tails.") ");
    }
    
}

Softstate.java

The public class Softlystate implements dogstate{//the class that implements the interface

    @Override public
    void Showstate () {
        System.out.println ("Listen to the master in front of the master." ");
    }
    
}

Test,java

public class Test {public
    static void Main (String[]args) {
        Dog aqi=new Dog ();
        Aqi.setstste (New Softlystate ());/set its state
        aqi.cry ();//The Showstate ()
        inside the interface is called through the method inside the dog class; Aqi.setstste (New Meetenemystate ());
        Aqi.cry ();
        Aqi.setstste (New Meetfriendstate ());
        Aqi.cry ();
        
    }

Run Result:










Summarize:

The class that implements the interface must implement the method inside the interface, in order to define the interface variable, the method in the interface can be realized according to different parameters.

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.