Java interface Programming and polymorphism Example learning __ algorithm

Source: Internet
Author: User

First of all assume a scene, such as we are selling mobile phones, if customers come to pick a mobile phone, need to take a different type of mobile phone to demonstrate the function

For simplicity, we can make an interface that connects to the phone at one end and controls the various functions of the demo phone on the other end.

Mobile phone has text messaging, phone two basic functions, we can set the interface to these two features

Public interface Cellphone
{public
	void phone (long num);

	public void SMS (long num, String content);


The following general mobile phone can use this interface

public class Nokia implements Cellphone
{
	@Override public
	void Phone (long number)
	{
		Util.print (" Nokia phone ");
	}

	@Override public
	void SMS (long, String content)
	{
		util.print ("Nokia SMS");
	}

public class BlackBerry implements Cellphone
{
	@Override public
	void Phone (long number)
	{
		Util.print ("BlackBerry phone");

	@Override public
	void SMS (long, String content)
	{
		util.print ("BlackBerry sms");
	}


You can also play games on the iphone.

Public class The Iphone implements Cellphone
{
	@Override public
	void phone (long num)
	{
		Util.print (" Iphone phone ");
	}

	@Override public
	void SMS (long num, String content)
	{
		util.print ("Iphone sms");
	}
	
	public void Play ()
	{
		util.print (' Iphone play ');
	}


So, we can use the interface to demonstrate the function of the phone when we operate.

Cellphone cell = new Nokia ();
Cell.phone (250);


At this time, customers also want us to demonstrate the cottage machine, but the interface does not match, we need to make an adapter to convert

public class Shanzhaiji
{public
	void Dadianhua ()
	{
		util.print ("Shanzhaiji Dadianhua Jiushiniu") ;
	}
	
	public void Faduanxin ()
	{
		util.print ("Shanzhaiji faduanxin jiushiniu");
	}


public class Shanzhaiadapter implements Cellphone
{
	private Shanzhaiji Shanzhai;

	Public Shanzhaiadapter (Shanzhaiji Shanzhai)
	{
		This.shanzhai = Shanzhai;
	}

	@Override public
	void phone (long num)
	{
		Shanzhai.dadianhua ();
	}

	@Override public
	void SMS (long num, String content)
	{
		shanzhai.faduanxin ();
	}
}


Through the interface conversion, we can also demonstrate the cottage machine

Shanzhaiadapter adapter = new Shanzhaiadapter (new Shanzhaiji ());
Adapter.phone (0);



Related Article

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.