java编程思想 -初始化

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();
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值