Java Learning-a fourth code model

Source: Internet
Author: User

Fourth Code Model: interface Application

In real life, you will often meet the following situations:

· There are many trees in a forest;

· There are many kinds of goods in the mall;

· There are a number of vehicles parked in a parking lot, for example: trucks, cars, motorcycles, bicycles.

Below simulates one of the above scenarios. Now there is a supermarket, in the supermarket to provide a variety of goods, now requires the realization of the shelves of goods sales and the function of the shelf, but also according to the keyword to find out the information of the product. This procedure only requires describing the structure of the class.

Example: Defining commodity standards

Interface Goods {//Commodity

Public String getName ();

Public double GetPrice ();

}

Example: defining supermarket classes

Class Supermarket {//supermarket

Private Link goods; All the Products

Public Supermarket () {

This.goods = new Link (); Prepare the space to store the goods

}

public void Insert (Goods g) {//Add product

This.goods.add (g); Save data to Linked list

}

public void Delete (Goods g) {//lower shelf

This.goods.remove (g); Delete data from a linked list

}

Public Link Search (String keyWord) {

Link result = new link ();

Object obj [] = This.goods.toArray ();

for (int x = 0; x < obj.length; x + +) {

Goods gd = (Goods) obj[x];

if (Gd.getname (). Contains (KeyWord)) {//Meet search criteria

Result.add (GD); Save Product Query Results

}

}

return result;

}

Public Link Getgoods () {//Get all items in the supermarket

return this.goods;

}

}

Example: defining commodity data

Class Cup implements Goods {

private String name;

private double price;

Public Cup () {}

Public cups (String name,double price) {

THIS.name = name;

This.price = Price;

}

Public String GetName () {

return this.name;

}

Public double GetPrice () {

return this.price;

}

public boolean equals (Object obj) {

if (obj = = null) {

return false;

}

if (this = = obj) {

return true;

}

if (! ( obj instanceof Cup) {

return false;

}

Cup cup = (cup) obj;

if (This.name.equals (cup.name) && This.price = = Cup.price) {

return true;

}

return false;

}

Public String toString () {

Return "" cup "name =" + THIS.name + ", Price:" + this.price;

}

}

Class Computer implements Goods {

private String name;

private double price;

Public computer () {}

Public computer (String name,double price) {

THIS.name = name;

This.price = Price;

}

Public String GetName () {

return this.name;

}

Public double GetPrice () {

return this.price;

}

public boolean equals (Object obj) {

if (obj = = null) {

return false;

}

if (this = = obj) {

return true;

}

if (! ( obj instanceof computer)) {

return false;

}

Computer com = (computer) obj;

if (This.name.equals (com.name) && This.price = = Com.price) {

return true;

}

return false;

}

Public String toString () {

Return "Computer" name = "+ THIS.name +", Price: "+ this.price;

}

}

At this time the computer and the Cup have no essential connection, but they belong to the commodity.

public class Testdemo {

public static void Main (String args[]) {

Supermarket market = new supermarket ();

Market.insert (New Cup ("Cartoon Cup", 10.0));

Market.insert ("Mug", 20.0));

Market.insert (New Cup ("Cold Cup", 32.0));

Market.insert (New Computer ("Limited edition cartoon Computer", 3200.0));

Market.insert (New computer ("Assault Cup memorial Computer", 99200.0));

Market.insert (New computer ("Computer not afraid of water", 39200.0));

Market.delete (New Cup ("Cartoon Cup", 10.0));

Object obj [] = Market.search ("cartoon"). ToArray ();

for (int x = 0; x < obj.length; x + +) {

System.out.println (Obj[x]);

}

}

}

This program is a typical interface-oriented programming, the different classes rely on the interface to connect.

Java Learning-a fourth code model

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.