//pomocnicza funkcja
private void testObjectTab(Object[] obTab){
for(Object ob: obTab)
prn(ob);
}
@Test
public void Tab3Init(){
int[]test = new int[5];
for(int x:test)
prn(x);
//wynik: 0,0,0,0,0
}
// @Test
public void InitTab2Test(){
testObjectTab(new Object[]{
new Integer(1),
new Float(1.1f),
new Double(1.1)}
);
//wynik: 1 , 1.1 ,1.1
}
// @Test
public void multiTab(){
int[][] mTab ={
{1,2,3},
{4,5,6}
};
for(int[] row:mTab)
for(int cell:row)
prn(cell);
//wynik: 1,2,3,4,5,6
assertEquals(mTab.length, 2); //pass!!!!!!!!!!!
}
// @Test
public void InitTest() {
int tab[];
int[] tab2;
int tab3[] = {1, 2};
int tab4[] = {1, 2, 3, 4, 5,}; //patrz przecinek! =>Ok
int tab4copy[] = tab4;
for (int x : tab4copy) {
x++; //nie zmienia nic w tablicy!
}
for (int x = 0; x < tab4copy.length; x++) {
tab4copy[x]++;
}
//sprawdzamy czy zmieniło się w 2 tablicach ( tab4copy i tab4)
for (int x : tab4) {
prn(x); //2,3,4,5,6
}
assertEquals(tab4, tab4copy); //pass
//test inicjalizacji
//!int tab5[3]; nie wolno bebe
int tab5[] = new int[0];
prn(tab5.length); //0
int tab6[] = new int[(char) 1];
int tab7[] = new int[(byte) 1];
//! int tab7[] = new int[(long)1]; zonk max może być Z
int tab9[] = new int[(int) 1.1];
int tab10[] = new int[(char) 1];
//pozyższe się udaje bo promowanie do inta
//ciekawostka
//! float f=1.1; bo 1.1 to double!
//tablioce obiektów
String[] s = {"ok", "", new String("ok")}; //ok
// String tabS[]={"ok",tabS[0]}; //variable might be not inicjalized!
Integer[] intTab = {1, 1, 3, new Integer(1)};
//int a Integer
//!int[] tabInt= new Integer[3]; zonk
//!Integer[] tabInteger= tab4; zonk
}
20 lut 2010
Tablica
19 lut 2010
inicjalizacja
@Test
public void testInicjalizacji(){
Init it = new Init();
}
public class Init {
static String ss1;
String sm1;
String sm2;
static {
ss1=new String("'=ok");
prn("Klauzula static");
}
{
sm1=new String("ok");
sm2=new String ("ok");
prn("Klauzula instancyjna{}");
}
public Init() {
prn("Konstruktor");
}
}
Wynik:
Klauzula static
Klauzula instancyjna{}
Konstruktor
Przeciążanie metod
@Test
public void testPrzeciazanieMetod(){
f(5); //int
f(3/2); //int
short s=12;
f(s); //short
f(s-1); //int
f(1-1.0f); //float
f(s-1.0f); //float
byte b=10;
f(b/2); //int
f(b*b); //int
}
public void f(float x){ prn("float");}
public void f(byte x){ prn("byte"); }
public void f(short x){prn("short");}
public void f(int x){prn("int");}
Działania arytmetyczne
@Test
public void testDzialan(){
int x=-3%2;
assertEquals(-1,x ); //pass
x=-3/2;
assertEquals(-1, x); //pass
float y=1.1f;
y++;
assertTrue(y==2.1f); //pass
int i=1;
float f=i; //automatyczna promocja
f=3/2;
prn(f); //1.0
assertTrue(f==1); //pass
x=-1<<1; //przesunięcie ze znakiem
assertTrue(x==-2); //pass
x=4>>>1; //przesuniecie w lewo bez zbaku
assertEquals(x,2); //pass
}
Try,catch,finally
@Test
public void testTry(){
//!try{ } //musi być catch or finally
//! catch(Exception e){ } //musi być try
//!finally{} musi być try przed
try{ }finally{} //ok
try{}catch(Exception e){} //ok
}
18 lut 2010
ArrayList []
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayListarray = new ArrayList ();
array[0]="Hello world";
System.out.println(array[0]);
}
}
Nie ma operatora [] w listach!!
Static
// A.java
public class A {
private final int a = 10;
public static String b = "ClassA";
protected long c;
}
// B.java
public class B extends A {
public static String b = "ClassB";
private int d = 1;
public static void test() {
...
}
}
Z metod statycznych ma się tylko dostęp do zmiennych statycznych!
Pytanie: Do jakich zmiennych mam dostęp w B.test() ??
Subskrybuj:
Posty (Atom)