2013-08-01 9 views
9

मैं प्रोग्रामिंग में नया हूं और मैं जानना चाहता हूं कि ऑब्जेक्ट को तुरंत चालू करने में मैं गलत कहां गया। नीचे दिए गए कोड है:जावा में ऑब्जेक्ट को तुरंत कैसे चालू करें?

public class Testing{ 
    private int Sample(int c) 
    { 
     int a = 1; 
     int b = 2; 
     c = a + b; 
     return c; 
    } 
    public static void main(String []args) 
    { 
     Sample myTest = new Sample(); 
     System.out.println(c); 
    } 
} 
+0

परिवर्तनीय 'सी' 'नमूना' विधि के दायरे में पहुंच योग्य है। आपने इस दायरे से इसे एक्सेस करने का प्रयास किया। –

उत्तर

15

अपने कोड में कोई Sample वर्ग नहीं है। जिसे आपने घोषित किया है वह एक निजी विधि है।

// private method which takes an int as parameter and returns another int 
private int Sample(int c) 
{ 
    int a = 1; 
    int b = 2; 
    c = a + b; 
    return c; 
} 
वर्तमान टुकड़ा के साथ

, आप Testing वर्ग का दृष्टांत और Sample पद्धति के उपयोग करने की जरूरत है। ध्यान दें कि आपकी कक्षा परिभाषा कीवर्ड कक्षा से पहले है, इस मामले में class Testing

public class Testing{ 
    private int Sample(int c) 
    { 
    int a = 1; 
    int b = 2; 
    c = a + b; 
    return c; 
} 
    public static void main(String []args) 
{ 
    Testing t = new Testing(); // instantiate a Testing class object 
    int result = t.Sample(1); // use the instance t to invoke a method on it 
    System.out.println(result); 
} 
} 

लेकिन वह वास्तव में कोई मतलब नहीं है, अपने Sample विधि हमेशा 3 देता है।

आप इस तरह कुछ करने के लिए कोशिश कर रहे हैं:

class Sample { 
int a; 
int b; 

Sample(int a, int b) { 
    this.a = a; 
    this.b = b; 
} 

public int sum() { 
    return a + b; 
} 
} 

public class Testing { 
public static void main(String[] args) { 
    Sample myTest = new Sample(1, 2); 
    int sum = myTest.sum(); 
    System.out.println(sum); 
} 
} 
+2

आप जानते हैं, आप सही हैं, यह समझ में नहीं आता है, लेकिन हे, मैं सीख रहा हूँ! – user2640722

+1

वास्तव में, हाँ! यही वही है जो मैं करना चाहता हूं! अद्भुत सॉस! – user2640722

1

आप new कीवर्ड के साथ सही ढंग से instantiating, लेकिन अपने calss नाम और विधि प्रेरक गलत है

Testing myTest = new Testing(); 
    int result =myTest.Sample(1); //pass any integer value 
    System.out.println(result); 
1

नमूना एक वर्ग नहीं है, यह बस एक विधि है। आप इसके उदाहरण नहीं बना सकते हैं। आप केवल इसे चलाने -

int sample = Sample(3); 

आप एक वर्ग होने के लिए नमूने के लिए चाहते हैं, तो एक वर्ग के रूप में यह परिभाषित करते हैं।

आपके मामले में, विधि स्थिर नहीं है, इसलिए आप इसे सीधे स्टेटिक विधि मेन से एक्सेस नहीं कर सकते हैं। इसे स्थिर बनाएं ताकि आप इसे एक्सेस कर सकें। या सिर्फ परीक्षण का एक नया उदाहरण बना सकते हैं और इसका इस्तेमाल करते हैं -

Testing testing = new Testing(); 
int sample = testing.Sample(3); 
1

नमूना विधि पूर्णांक देता है, तो परिणाम मिलता है और इसे कहीं भी उपयोग करें।

public static void main(String []args) 
{ 
    int myTest = Sample(4555);//input may be any int else 
    System.out.println(myTest); 
} 
1

इस तरह आपको यह करना चाहिए।

public class Testing{ 
public int Sample(int c) 
{ 
    int a = 1; 
    int b = 2; 
    c = a + b; 
    return c; 
} 
public static void main(String []args) 
{ 
    // Creating an Instance of Testing Class 
    Testing myTest = new Testing(); 
    int c =0; 
    // Invoking the Sample() function of the Testing Class 
    System.out.println(myTest.Sample(c)); 
} 
2

मुझे संदेह है कि आप वास्तव में एक वस्तु बनाना चाहते हैं।

अपने कोड स्निपेट से, मैं समझता हूं कि आप Sample नामक 'विधि' को चलाने के लिए चाहते हैं जो दो संख्याओं को जोड़ता है। और जावा में आपको विधियों को तुरंत चालू करने की आवश्यकता नहीं है। ऑब्जेक्ट्स class के उदाहरण हैं। एक विधि सिर्फ एक व्यवहार है जो इस वर्ग में है।

आपकी आवश्यकता के लिए, जब आप संकलित कोड चलाते हैं तो आपको किसी भी चीज को स्पष्ट रूप से तत्काल करने की आवश्यकता नहीं है जब जावा स्वचालित रूप से आपकी कक्षा का एक उदाहरण बनाता है और इसे निष्पादित करने के लिए main() विधि को देखता है।

शायद आप बस अनुसरण करते हैं:

public class Testing{ 
    private int sample(int a, int b) { 
     return a + b; 
    } 
    public static void main(String[] args) { 
     int c = sample(1, 2); 
     System.out.println(c); 
    } 
} 

नोट: मैं sample को Sample बदल के रूप में यह आम तौर पर अभ्यास एक अपर-केस पत्र के साथ लोअर केस और वर्ग के नाम के साथ एक विधि नाम शुरू करने के लिए स्वीकार कर लिया है, तो Testing उस मोर्चे पर सही है।

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