27 lut 2010

Polimoryzm


public class Base {

public static String _static="base_static";
public String _inst="base_inst";

public String publicMethod(){
return "base_public_method";
}
private String privateMethod(){
return "base_public_method";
}
}

public class Subclass extends Base{

public static String _static="subclass_static";
public String _inst="subclass_inst";

public String publicMethod(){
return "subclasss_public_method";
}
}

@Test
public void testprzyslaniania(){

Base base = new Base();
prn(base._inst); //base_inst
prn(base._static);//base_static
prn(base.publicMethod());//base_public_method

Subclass sub = new Subclass();
prn(sub._inst);//subclass_inst
prn(sub._static);//subclass_static
prn(sub.publicMethod());//subclasss_public_method

Base base_ref= sub;
prn(base_ref._inst);//base_inst
prn(base_ref._static);//base_static
prn(base_ref.publicMethod());//subclasss_public_method

Base base_base_cast=(Base)sub;
prn(base_base_cast._inst);//base_inst
prn(base_base_cast._static);//base_static
prn(base_base_cast.publicMethod());//subclasss_public_method

/**
* Wnioski:
* + dynamicznie (po typie obiektu) są tlko metody
* + zmienne static / inst = są wybierane po typie referencji!
*/
}

Brak komentarzy:

Prześlij komentarz