java 面向对象(对象调用成员过程):方法区先加载
时间:2014-04-25 22:29 来源: 我爱IT技术网 作者:山风
面向对象(对象调用成员过程)
方法区先加载
一、非静态方法
所属于this(称为指向或引用),非静态前面可能省略了"this."
1)被调用者(setName方法)在栈内存中开辟空间。(因为setName方法有局部变量name)
2)在堆内存新建Person对象。
3)将调用者(对象p)的内存地址赋给this,this指向对象p,this代表p,故this.Name="李四"
4)释放栈中该方法开辟的空间。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
二、静态方法
所属于类,静态前面可能省略了"类名."
1)被调用者(showCountry方法)在栈内存中开辟空间。
2)类名.showCountry,并访问方法区
3)同理
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class Person
{
private String name="李四";
private int age;
private static String country="cn";
//构造函数
Person(String name,int age)
{
this.name=name;
this.age=age;
}
//构造代码块
{
System.out.println(name+","+age);
}
public void setName(String name)
{
this.name=name;
}
public void speak()
{
System.out.println(this.name+","+this.age);
}
public static void showCountry()
{
System.out.println("country="+Person.country);
Person.method(); //成员方法可相互调用
}
public static void method()
{
System.out.println("method run");
}
}
class Test
{
public static void main(String[] args)
{
Person p=new Person("张三",20);
p.setName("李四");
}
}
本文来源 我爱IT技术网 http://www.52ij.com/jishu/5605.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
