2013-05-22 16 views
24

Moje zadanie polega na utworzeniu programu ze zmienną instancji, ciągiem, który powinien być wprowadzony przez użytkownika. Ale nie wiem nawet, czym jest zmienna instancji. Co to jest zmienna instancji? Jak go utworzyć? Co to robi?Java - Co to jest zmienna instancji?

+2

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Maroun

Odpowiedz

52

zmienna instancji jest zmienna zadeklarowana wewnątrz klasy, ale poza metody: coś jak:

class IronMan{ 

    /** These are all instance variables **/ 
    public String realName; 
    public String[] superPowers; 
    public int age; 

    /** Getters/setters here **/ 
} 

teraz Ironman Klasa może być utworzona w innej klasie, aby użyć tych zmiennych, coś w stylu:

class Avengers{ 
     public static void main(String[] a){ 
       IronMan ironman = new IronMan(); 
       ironman.realName = "Tony Stark"; 
       // or 
       ironman.setAge(30); 
     } 

} 

W ten sposób używamy zmiennych instancji. Więcej zabawnych rzeczy na podstawach java here.

19

Zmienna instancji jest zmienną, która jest członkiem instancji klasy (tj. Powiązana z czymś utworzonym z new), podczas gdy zmienna klasy jest członkiem samej klasy.

Każde wystąpienie klasy będzie mieć własną kopię zmiennej instancji, podczas gdy jest tylko jedna z każdej zmiennej statycznej (lub klasy), związanej z samą klasą.

difference-between-a-class-variable-and-an-instance-variable

Klasa ta próba ilustruje różnicę

public class Test { 

    public static String classVariable="I am associated with the class"; 
    public String instanceVariable="I am associated with the instance"; 

    public void setText(String string){ 
     this.instanceVariable=string; 
    } 

    public static void setClassText(String string){ 
     classVariable=string; 
    } 

    public static void main(String[] args) { 
     Test test1=new Test(); 
     Test test2=new Test(); 

     //change test1's instance variable 
     test1.setText("Changed"); 
     System.out.println(test1.instanceVariable); //prints "Changed" 
     //test2 is unaffected 
     System.out.println(test2.instanceVariable);//prints "I am associated with the instance" 

     //change class variable (associated with the class itself) 
     Test.setClassText("Changed class text"); 
     System.out.println(Test.classVariable);//prints "Changed class text" 

     //can access static fields through an instance, but there still is only 1 
     //(not best practice to access static variables through instance) 
     System.out.println(test1.classVariable);//prints "Changed class text" 
     System.out.println(test2.classVariable);//prints "Changed class text" 
    } 
} 
+0

prawidłowe. Możesz także myśleć o zmiennej instancji jako 'polu' w obiekcie. Istotną koncepcją jest enkapsulacja (patrz: modyfikator dostępu "private", moduły pobierające i ustawiające ...) – vikingsteve

+0

Rzeczywiście, większość rzeczy ogłaszam publicznie dla łatwego dostępu, zazwyczaj jest to zły pomysł –