package day3;
import static net.mindview.util.Print.*;
/**
*
* @author duhongyu 2018年11月26日 成员初始化
*
*/
public class InitialValues {
boolean t;
char c;
byte b;
short s;
int i;
long l;
float f;
double d;
InitialValues reference;
void printInitialValues() {
print("Data type Initial value");
print("boolean:" + t);
print("char:" + c);
print("byte:" + b);
print("short:" + s);
print("int:" + i);
print("long:" + l);
print("float:" + f);
print("double:" + d);
print("reference:" + reference);
}
public static void main(String[] args) {
InitialValues iv = new InitialValues();
iv.printInitialValues();
new InitialValues().printInitialValues();
}
}
package day3;
/**
*
* @author duhongyu 2018年11月26日
* 终结条件 ----finalize()可能的使用方式
*
*/
class Book {
boolean checkedOut = false;
Book(boolean checkOut) {
checkedOut = checkOut;
}
void checkIn() {
checkedOut = false;
}
protected void finalize() {
if (checkedOut)
System.out.println("Error : checked out");
// normally ,you'll also do this
// super.finalize(); // Call the base-class version
}
}
public class TerminationCondition {
public static void main(String[] args) {
Book novel = new Book(true);
// Proper cleanup
novel.checkIn();
//当存在书本为被签入,
// drop the reference, forge to clean up
new Book(true);
// force garbage collection & finalization
System.gc();
}
}
package day3;
import static net.mindview.util.Print.*;
/**
*
* @author duhongyu 2018年11月26日
* 在构造器中调用构造器
*
*/
public class Flower {
int petalCount = 0;
String s = "initial value";
Flower(int petals){
petalCount = petals;
print("Constructor w / int arg only,petalCount = "+petalCount);
}
Flower(String ss){
print("Constructor w/ String arg only,s +"+ss);
s = ss;
}
Flower(String ss,int petals){
this(petals);
//! this(s);
this.s = s; //Another use of "this"
print("String & int args");
s = ss;
}
Flower(){
this("hi",47);
print("default constructor(no args_");
}
void printPetalCount() {
//! this(11); //Not inside non_constructor
print("petalCout = "+petalCount+" S = "+s);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Flower x = new Flower();
x.printPetalCount();
}
}