2014-11-02 4 views
-1

두 인스턴스 메서드의 차이점은 무엇입니까?이 두 인스턴스 메서드의 차이점은 무엇입니까?

public class Food 
{ 
    public int apples; 
    public int oranges; 
    public int bananas; 

    // Constructor #1 
    public Food(int a, int o, int b) 
    { 
     apples = a; 
     oranges = o; 
     bananas = b; 
    } 

// Is this an instance 

public Food myFood = new Food(5, 8, 1); 

// Or this 

Food.myfood(5, 8, 1) 

내 경험 많은 친구는 후자가 인스턴스가 아니라 첫 번째 것이 아니라고 말했습니다.

+0

질문이 명확하지 않습니다. 네가 묻고있는 것을 짐작할 것이다. 첫 번째 인스턴스가 인스턴스를 만듭니다. 두 번째는 'Food' 클래스에서 정적 메서드를 호출하는 것으로 보이지만 (인스턴스를 만들지는 않음) 클래스에 해당 메서드가 없습니다. 클래스, 인스턴스 생성 및 정적 메서드에 대해 읽어 보시기 바랍니다. –

+0

5 개 국어에 익숙한 친구가 있으며 사다리가 어떻게 생겼다고 말했습니다. 나는 내가 옳았는지, 아니면 옳았는지를 단지 보았습니다. –

답변

0

나는 사람들이 질문을 읽지 않는 것처럼 느낍니다. 답변 : 두번째 잘못 값을 설정한다

Food myFood = new Food(3,4,5); 

첫번째는 (구) 식품의 새로운 인스턴스를 생성한다. f 후

Food myFood = new Food(3,4,5); 
Console.WriteLine(myfood.Oranges) // prints 4; 
myFood.Oranges = 3; // set the value of oranges for that object to 3 
Console.WriteLine(myFood.Oranges); // prints 3; 
+0

고맙습니다! 다행히 당신이 실제로 질문에 대답했습니다 :) –

+0

아무 문제 없습니다. 그리고 downvoted에 대해 걱정하지 마십시오. 거부하는 사람은 싫어할거야. 그들은 읽고 이해하지 못하므로 질문에 문제가 있다고 가정합니다. 그것은 아주 정당한 질문이었습니다. –

0

수업을 종료하는 것을 잊어 버렸다고 가정합니다.

public class Food 
{ 
    public int apples; 
    public int oranges; 
    public int bananas; 

    // Constructor #1 
    public Food(int a, int o, int b) 
    { 
     apples = a; 
     oranges = o; 
     bananas = b; 
    } 

} // <---- you were missing this I assume 

// If the following is in your main method, then myFood is an instance 
// of the Food class. 
// you can do myFood.apples = 4; // very bad btw, having public variables. 
public Food myFood = new Food(5, 8, 1); 

// Not even sure what this is ... because you don't have a myFood on Food. 
// If you would have a static property on your Food class (Yummy), then you 
// could do something like Food.Yummy = what_ever_the_type_is ... 
Food.myfood(5, 8, 1) 
관련 문제