Java 1.5 Tiger A Developers Notebook [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Java 1.5 Tiger A Developers Notebook [Electronic resources] - نسخه متنی

David Flanagan, Brett McLaughlin

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








3.8 Implementing Interfaces with Enums


Now that you're starting to add
methods to your enums (see the previous
lab on Adding Methods to an Enum for details if you're skipping
around), you may find that you want to define methods in an interface,
and implement that interface with one, two, or even more enums. You
may also want to have an enum implement an interface, and have
classes implement that same interface. All this is possible, and even quite
simple.


3.8.1 How do I do that?


Example 3-8 is a very simple interface that could describe enumerated
types that represent features on all sorts of instruments (not just guitars).


Example 3-8. Base interface for feature enums


package com.oreilly.tiger.ch03;
public interface Features {
/** Get the upcharge for this feature */
public float getUpcharge( );
/** Get the description for this feature */
public String getDescription( );
}

It's trivial to make GuitarFeatures implement this interface, as the methods
are already written:


public enum GuitarFeatures implements Features {

Now you can create BanjoFeatures, MandolinFeatures, and more, all
using the same interface as a starting point. This creates a nice sense of
uniformity among your enums, and is highly recommended.


/ 131