2012-11-21 14 views
31

instanceof पर उदाहरण कैसे काम करेगा, यह जांचने के लिए उपयोग किया जा सकता है कि कोई ऑब्जेक्ट किसी दिए गए वर्ग का प्रत्यक्ष या अवरोही उदाहरण है या नहीं। instanceof इंटरफेस के साथ भी इस्तेमाल किया जा सकता है भले ही इंटरफेस कक्षाओं की तरह तत्काल नहीं किया जा सके। क्या कोई बता सकता है कि instanceof कैसे काम करता है?इंटरफ़ेस

उत्तर

44

सबसे पहले, हम कक्षाओं है कि एक विशेष interface एक interface reference variable इस तरह में लागू करता है की instances स्टोर कर सकते हैं।

package com.test; 

public class Test implements Testeable { 

    public static void main(String[] args) { 

     Testeable testeable = new Test(); 

     // OR 

     Test test = new Test(); 

     if (testeable instanceof Testeable) 
      System.out.println("instanceof succeeded"); 
     if (test instanceof Testeable) 
      System.out.println("instanceof succeeded"); 
    } 
} 

interface Testeable { 

} 

यानी, किसी भी क्रम उदाहरण लागू करता है कि एक विशेष इंटरफ़ेस instanceof परीक्षण

संपादित

और आउटपुट

instanceof succeeded 
instanceof succeeded 

@RohitJain पारित करेंगे

आप इस

Runnable runnable = new Runnable() { 

    public void run() { 
     System.out.println("inside run"); 
    } 
}; 

तरह गुमनाम इनर क्लासों का उपयोग करके इंटरफेस के उदाहरण बना सकते हैं और आप परीक्षण उदाहरण प्रकार इंटरफ़ेस की है, इस

System.out.println(runnable instanceof Runnable); 

तरह instanceof ऑपरेटर का उपयोग और परिणाम 'सही' है

+0

यदि संभवतः आपकी शर्तों में आप 'उदाहरण का परीक्षण' का मतलब है। –

+0

नहीं .. प्रश्न 'इंटरफेस' के खिलाफ 'exampleof' का उपयोग करने के बारे में था। आरटी? – sunil

+0

@ सुनील .. हाँ यह सच है। लेकिन यदि आप एक इंटरफेस के साथ 'exampleof' की जांच करते हैं, तो आपको हमेशा झूठा परिणाम मिल जाएगा। चूंकि आप किसी इंटरफ़ेस का उदाहरण नहीं बना सकते हैं, यह जांचना अर्थपूर्ण नहीं है कि इसके संदर्भ में कोई संदर्भ बिंदु है या नहीं। मुझे उम्मीद है कि आपको वह कहना है जो मैं कहना चाहता हूं। –

-1

उदाहरण ऑपरेटर आपको बताएगा कि पहला तर्क एक ऐसा ऑब्जेक्ट है जो दूसरे तर्क को लागू करता है। समझ में नहीं आता कि इससे कोई फर्क नहीं पड़ता कि आप सीधे इंटरफ़ेस को तुरंत चालू नहीं कर सकते हैं।

Integer num = 1; 
if (num instanceof Number) { 
    System.out.println("An integer is a number!"); 
} 

आपको बस इतना ही चाहिए।

+0

आपका उदाहरण प्रश्न का बिंदु गुम है - 'इंटीजर' _extends_ (लागू नहीं) 'संख्या'। –

5

आप एक instance के खिलाफ एक reference के instanceof जांच करते हैं, और यह instance के प्रकार है कि विशेष रूप reference की ओर इशारा करते है की जाँच करता है।

अब जब से तुम जो class (के रूप में ही अवधारणा है, subclass instance को Super class reference इशारा) को लागू करने का एक उदाहरण के लिए कहते हैं एक interface के संदर्भ, बना सकते हैं। तो, आप instanceof पर जांच कर सकते हैं।

उदाहरण के लिए: - सभी की

public interface MyInterface { 
} 

class ImplClass implements MyInterface { 

    public static void main(String[] args) { 
     MyInterface obj = new ImplClass(); 

     System.out.println(obj instanceof ImplClass); // Will print true. 
    } 
} 
+3

क्या? बेशक '(ओबीजे उदाहरण ImplClass) == सच', obj वर्ग ISplClass है! सवाल यह है, '(ओबीजे उदाहरण MyInterface) == सच'? मुझे लगता है कि यह एक टाइपो है। स्पष्टीकरण थोड़ा उलझन में बीटीडब्ल्यू है। –

2

- सभी उदाहरणों में से सबसे पहले यह तुलना करने के लिए प्रयोग किया जाता है कि ऑब्जेक्ट रेफरेंस वैरिएबल ऑब्जेक्ट को पकड़ रहा है या नहीं।

उदाहरण के लिए:

public void getObj(Animal a){  // a is an Object Reference Variable of type Animal 

    if(a instanceof Dog){ 


     } 

} 

-interface के मामले में, class जो लागू करता यह instanceof के साथ प्रयोग किया जा सकता है।

उदाहरण के लिए:

public interface Brush{ 

    public void paint(); 
} 

public class Strokes implements Brush{ 

     public void paint(){ 

      System.out.println("I am painting"); 

    } 


} 

public class Test{ 


    public static void main(String[] args){ 

      Brush b = new Strokes(); 

     if(b instanceof Strokes){ 


      b.paint(); 
     } 
    } 

} 
2
:

• If S is an ordinary (nonarray) class, then: 
    • If T is a class type, then S must be the same class as T, or S must be a subclass of T; 
    • If T is an interface type, then S must implement interface T. 
• If S is an interface type, then: 
    • If T is a class type, then T must be Object. 
    • If T is an interface type, then T must be the same interface as S or a superinterface of S. 
• If S is a class representing the array type SC[], that is, an array of components of type SC, then: 
    • If T is a class type, then T must be Object. 
    • If T is an interface type, then T must be one of the interfaces implemented by arrays (JLS §4.10.3). 
• If T is an array type TC[], that is, an array of components of type TC, then one of the following must be true: 
     - TC and SC are the same primitive type. 
     - TC and SC are reference types, and type SC can be cast to TC by these run-time rules 

कृपया स्पष्ट विचार है करने के लिए इस लिंक पर जाएं

public class Programmers { 

    public static boolean hasReallife(Programmer programmer) { 
     return programmer instanceof Reallife; ══════════════════╗ 
    }               ║ 
                   ║ 
}                ║ 
                   ▼ 
public class ReallifeProgrammer extends Programmer implements Reallife { 

    public ReallifeProgrammer() { 
     diseases.get("Obesity").heal(); 
     diseases.get("Perfectionism").heal(); 
     diseases.get("Agoraphobia").heal(); 
    } 

    @Override 
    public void goOut() { 
     house.getPC().shutDown(); 
     wife.argue(); 
    } 

    @Override 
    public void doSports() { 
     goOut(); 
     BigWideWorld.getGym("McFit").visit(); 
    } 

    @Override 
    public void meetFriends() { 
     goOut(); 
     BigWideWorld.searchFriend().meet(); 
    } 

} 
+0

अच्छा असीसी कला! – winklerrr

0

मुझे पता है कि यह कई अच्छे उत्तरों के साथ एक बहुत ही पुराना सवाल है। मैं बस इस ऑपरेटर को समझने के लिए सबसे आसान (कम से कम यह मेरे लिए सबसे आसान) इंगित करना चाहता हूं।

o instanceof t तो रिटर्न true, तो t castedObj = (t) o;ClassCastException फेंक नहीं होगा, और castedObjnull नहीं होगा।

यदि आप castedObj से फ़ील्ड या विधियों तक पहुंचना चाहते हैं तो यह महत्वपूर्ण/उपयोगी है - आप जानते हैं कि instanceof चेक करके, आपको बाद में कभी समस्या नहीं होगी।

केवल नकारात्मक पक्ष यह है कि इसका उपयोग जेनरिक के बिना प्रकारों के लिए किया जा सकता है।

संबंधित मुद्दे