Java/Class/Fields — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:36, 1 июня 2010
Содержание
Accessing Outer Class Variables
public class MemberClass {
int counter = 0;
public class Counter {
int counter = 10;
public void increaseCount() {
counter++;
MemberClass.this.counter++;
}
public void displayCounts() {
System.out.println("Inner: " + counter);
System.out.println("Outer: " + MemberClass.this.counter);
}
}
public void go() {
Counter ct = new Counter();
ct.increaseCount();
ct.increaseCount();
ct.increaseCount();
ct.displayCounts();
}
public static void main(String args[]) {
MemberClass mc = new MemberClass();
mc.go();
}
}
Declaring Variables
/*
In this example, a String object is declared and initialized with a reference to the
String "blah" at the top of the main() method. An int named k is declared after the
String object but is not given a value until later in the program. Another int named
j is declared and intialized in the for loop. This variable will not be accessible
outside of the for loop.
*/
public class TestDeclare {
public static void main(String args[]) {
String str = "blah";
int k;
System.out.println(str);
k = 2;
for (int j = 0; j < 2; ++j) {
System.out.println("j is " + j);
}
}
}
Specifying initial values in a class definition
// : c04:StaticInitialization.java
// Specifying initial values in a class definition.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Table {
static Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
static Bowl b2 = new Bowl(2);
}
class Cupboard {
Bowl b3 = new Bowl(3);
static Bowl b4 = new Bowl(4);
Cupboard() {
System.out.println("Cupboard()");
b4.f(2);
}
void f3(int marker) {
System.out.println("f3(" + marker + ")");
}
static Bowl b5 = new Bowl(5);
}
public class StaticInitialization {
public static void main(String[] args) {
System.out.println("Creating new Cupboard() in main");
new Cupboard();
System.out.println("Creating new Cupboard() in main");
new Cupboard();
t2.f2(1);
t3.f3(1);
}
static Table t2 = new Table();
static Cupboard t3 = new Cupboard();
} ///:~
The full process of initialization
// : c06:Beetle.java
// The full process of initialization.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
class Insect {
private int i = 9;
protected int j;
Insect() {
System.out.println("i = " + i + ", j = " + j);
j = 39;
}
private static int x1 = print("static Insect.x1 initialized");
static int print(String s) {
System.out.println(s);
return 47;
}
}
public class Beetle extends Insect {
private int k = print("Beetle.k initialized");
public Beetle() {
System.out.println("k = " + k);
System.out.println("j = " + j);
}
private static int x2 = print("static Beetle.x2 initialized");
public static void main(String[] args) {
System.out.println("Beetle constructor");
Beetle b = new Beetle();
}
} ///:~