九 java面向对象程序设计(this关键字)
九 java面向对象程序设计(this关键字)
/**
* 面向对象之四
* this关键字总结
*/
/*this关键字的第一种用法*/
//在方法中调用同类中的方法,这时的this可以省略.
class ThisPointer
{
public void function1()
{
System.out.println("function1 is calling...");
}
public void function2()
{
System.out.println("function2 is calling...");
this.function1();//调用同类中的function1;
//this是指,这个对象的function方法.
}
public void function3()
{
System.out.println("function3 is calling...");
function2();//相当于this.function2().传入的是同一个对象,所以this可以省略.
}
}
/*this关键字的第二种用法*/
//指明类中与方法参数同名的成员变量.
class Pointer
{
private int i;
private String str;
public void function(int i,String str)
{
this.i = i;
this.str = str;
//通过this,将function方法中同名的i,str和成员变量予以区分.
System.out.println("i = " + i + " , " + "str = " + str);
}
}
/*this关键字的第三种用法*/
//作为一个对象,参与到类的方法中
class Container
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
private Components com;
public void addComponents()//在容器中添加部件,
{
//com = new Components(new Container());
//虽然这样写没问题.但表示,在新创建一个部件的同时,又创建一个容器,然后将新部件加到新容器上.
com = new Components(this);
//用this表示这个容器类的对象,所以我们每次产生新部件是,都是加入到这个容器对象上的.
System.out.println(" is loading...");
}
}
class Components
{
private Container c;
public Components(Container c)
{
this.c = c;//部件类的构造方法指明
相关文档:
类的初始化和对象初始化是 JVM 管理的类型生命周期中非常重要的两个环节,Google 了一遍网络,有关类装载机制的文章倒是不少,然而类初始化和对象初始化的文章并不多,特别是从字节码和 JVM 层次来分析的文章更是鲜有所见。
本文主要对类和对象初始化全过程进行分析,通过一个实际问题引入,将源代码转换成 JVM 字节码后, ......
一、编辑Java源文件
=============================================
Hello.java
=============================================
package test;
public class Hello
{
static
{
try
{
//此处即为本地方法所在链接库名
&n ......
A flexible layout configurable with pattern string.
The goal of this class is to format
a LoggingEvent
and return the results as a String. The results depend on the conversion
pattern
.
The conversion pattern is closely related to the conversion pattern of the
printf function in C ......
通常,我们为了避免内存溢出等问题,需要设置环境变量
JAVA_OPTS -Xms256M -Xmx512M 等,【对于服务器,一般都设置成一样的】
但是有的时候可能这样的设置还会不行(比如,当Server应用程序加载较多类时,即jvm加载类时,永久域中的对象急剧增加,从而使jvm不断调整永久域大小,为了避免调整),你可以使 ......
有JAVA中,有时候需要根据条件来生成批处理sqls语句等,需要动态生成数组。方法:
List<String> list=new ArrayList<String>();
if(true){
list.add("insert.....");
list.add("update....");
}else{
list.add("insert....");
}
//这句是关 ......