静态代码块

//Cartoon.java
//初始化及类的装载

class Art {
  Art() {
    System.out.println("Art constructor");
  }
  static
	{
	  System.out.println("art");
	}
}

class Drawing extends Art {
  Drawing() {
    System.out.println("Drawing constructor");
  }
   static
	{
	  System.out.println("Drawing");
	}
}

public class Cartoon extends Drawing {
  Art a=new Art();
  Drawing d=new Drawing();
  Cartoon() {
    System.out.println("Cartoon constructor");
  }
   static
	{
	  System.out.println("Cartoon");
	}
  public static void main(String[] args) {
        Cartoon x =null; 
		x=new Cartoon();
  }
} 

 

class Value
{
	static int c = 0;
	int value;
	static
	{
		c = 15;
		increase();
	}
	Value()
	{
		System.out.println("Value()");
		System.out.println(Static3.i);
		c = 3;
	}
	Value(int i)
	{

		System.out.println("Value(int i) i=" + i);
		c = i;
	}
	static void increase()
	{
		System.out.println("increase()");
		c++;
	}

	public void setValue(int i)
	{
		value = i;
	}
}
class Static2
{
	static int i;
	static Value v1, v2;
	Value v = new Value(1);
	public static void prt(String s)
	{
		System.out.println(s);
	}
	static
	{
		System.out.println("ok");
	}
	static
	{
		prt("Static block");
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
		v1 = new Value(5);
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
		v2 = new Value(6);
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
	}
	public static void main(String[] args)
	{
		prt("main");
		Static2 sta = new Static2();
		Static2 ct = new Static2(); 
		prt("ct.v.c=" + ct.v.c);
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
		v1.increase();
		prt("ct.v.c=" + ct.v.c);
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
	}
	static
	{
		System.out.println("ok");
	}
}

 

/**
 * Static3 关于static变量和static方法
 */
class Value
{
	// 整型类成员变量
	static int c = 0;
	int value;
	static
	// 静态代码块
	{
		c = 15;
		increase();
	}
	// 构造方法
	Value()
	{
		System.out.println("Value()");
		System.out.println(Static3.i);
		c = 3;
	}

	// 构造方法
	Value(int i)
	{

		System.out.println("Value(int i) i=" + i);
		c = i;
	}

	// 类成员方法
	static void increase()
	{
		System.out.println("increase()");
		c++;
	}

	public void setValue(int i)
	{
		value = i;
	}
}

class Static3
{
	static int i;
	// 共有类方法,简化输出函数
	public static void prt(String s)
	{
		System.out.println(s);
	}
	// 成员变量v是Value类的实例的引用变量,是Static3类的实例成员变量
	Value v = new Value(1);
	// v1和v2是Static3类的类成员变量
	static Value v1, v2;
	// 静态代码块,
	/*
	 * 一个类中可以使用不包含在任何方法体中的静态代码块(static block), 当类被载入时,
	 静态代码块被执行; 静态代码块只执行一次;
	 * 静态代码块经常用来进行类属性的初始化
	 */
	static
	{
		System.out.println("ok");
	}
	static
	{
		prt("Static block");
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
		
		v1 = new Value(5);
		//v1.setValue(100);
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
		v2 = new Value(6);
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
	}

	public static void main(String[] args)
	{
		// d = 10;
		prt("main");
		Static3 sta = new Static3();
		// sta.d = 10;
		Static3 ct = new Static3(); // 创建一个Count类的实例,其引用是ct
		// Count类的实例的成员变量v的成员变量c
		prt("ct.v.c=" + ct.v.c);
		// Count类的成员方法main调用Count类的成员变量v1和v2
		// 注意:类方法只能调用类变量
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
		// Count类的成员变量v1的成员方法inc
		v1.increase();
		prt("ct.v.c=" + ct.v.c);
		prt("v1.c=" + v1.c + "  v2.c=" + v2.c);
	}
	static
	{
		System.out.println("ok");
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下载时请看下面说明,对写一个动态的内存池很有帮助。 这是一个用C++语言链表的方法实现的一个静态内存池代源码。原理就是先向系统申请一块大内存,然后把这一大块分隔成相等的很多小块,然后在这这些小块的首地址部份放一个结构体,结构体有一个值是用来标明这一小块是否使用。在这里存放你要放存的数据就是用结构体首地址加结构体自身长度就得到要放数据的首地址了.具体看代码的实现吧。我说一下动态内存池的写法。那是我给公司写的就不能上传了。结构体和静态内存池的这个差不多一样,只是增加了一个成员用来记录每一节点到大块内存的首地址在到本节点的一个尺寸长度值,做法也是先申请一块大内存。我先从释放说起吧,释放本节点时看自己的相邻节点是不是有释放掉的,如果有则合并掉他们成为一个块,如果碰到相邻的节点是另外的一个大块的话就不用合并了,原因他和自己所在的这一个大块内存上物理地址不是连续,这里一定要记住,释放过程算法怎么去写就看你的了。下面是分配写法要考虑的。在分配一小块内存给高层使用时,如果是分配在尾节点去分配的情况,那好办啊,尾节点如果不够分配了就直接从系统去申请一块大内存,节点连起来在分配,这里有可能会浪费掉一小块以结构体大小的一块内存,如果够分配就直接分配了。如果是在间节点去分配,这里就要将释放时合并的如果大于现在要分配的就拆开来用,如果拆开剩余的那一部份只有结构体大小就不用在拆开了。这些都是要考虑的东西,优化加快速度就看你自己了.可能看时不些不明白,看静态内存的写法后你就明白了.有时我也要下载其他人共享的东西,所以就一分吧.哈哈~~~~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值