加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JAVA中实例变量与类变量的区别,实例方法和类方法的区别

发布时间:2020-12-14 06:38:14 所属栏目:Java 来源:网络整理
导读:转自: h2 style="font-size:21px;line-height:1.5;font-family:Verdana,Arial,Helvetica,sans-serif;" 实例变量 ol style="font-family:Verdana,sans-serif;font-size:13px;" li style="list-style:decimal;text-align:left;" 实例变量声明在一个类中,但在

转自:

<h2 style="font-size:21px;line-height:1.5;font-family:Verdana,Arial,Helvetica,sans-serif;">
实例变量
<ol style="font-family:Verdana,sans-serif;font-size:13px;">
<li style="list-style:decimal;text-align:left;">
实例变量声明在一个类中,但在方法、构造方法和语句块之外;
<li style="list-style:decimal;text-align:left;">
当一个对象被实例化之后,每个实例变量的值就跟着确定;
<li style="list-style:decimal;text-align:left;">
实例变量在对象创建的时候创建,在对象被销毁的时候销毁;
<li style="list-style:decimal;text-align:left;">
实例变量的值应该至少被一个方法、构造方法或者语句块引用,使得外部能够通过这些方式获取实例变量信息;
<li style="list-style:decimal;text-align:left;">
实例变量对于类中的方法、构造方法或者语句块是可见的。一般情况下应该把实例变量设为私有。通过使用访问修饰符可以使实例变量对子类可见;
<li style="list-style:decimal;text-align:left;">
实例变量具有默认值。数值型变量的默认值是0,布尔型变量的默认值是false,引用类型变量的默认值是null。变量的值可以在声明时指定,也可以在构造方法中指定;
<li style="list-style:decimal;text-align:left;">
实例变量可以直接通过变量名访问。但在静态方法以及其他类中,就应该使用完全限定名:ObejectReference.VariableName。

  1. ClassName.VariableName的方式访问。
 TiXing(</span><span style="color:rgb(0,255);line-height:1.5 !important;"&gt;float</span> x,<span style="color:rgb(0,255);line-height:1.5 !important;"&gt;float</span> y,255);line-height:1.5 !important;"&gt;float</span><span style="line-height:1.5 !important;"&gt; z){
    up</span>=<span style="line-height:1.5 !important;"&gt;x;
    height</span>=<span style="line-height:1.5 !important;"&gt;y;
    down</span>=<span style="line-height:1.5 !important;"&gt;z;
}

}

<span style="color:rgb(0,255);line-height:1.5 !important;">public <span style="color:rgb(0,255);line-height:1.5 !important;">class<span style="line-height:1.5 !important;"> ep3_9{
<span style="color:rgb(0,255);line-height:1.5 !important;">void<span style="line-height:1.5 !important;"> main(String args[]){
TiXing one=<span style="color:rgb(0,255);line-height:1.5 !important;">new TiXing(1,2,3<span style="line-height:1.5 !important;">);
System.out.println("one's down is:"+<span style="line-height:1.5 !important;">one.down);
TiXing two=<span style="color:rgb(0,255);line-height:1.5 !important;">new TiXing(4,5,6<span style="line-height:1.5 !important;">);
System.out.println("one's down is:"+<span style="line-height:1.5 !important;">one.down);
System.out.println("two's down is:"+<span style="line-height:1.5 !important;">two.down);

    System.out.println(</span>"TiXing's down is:"+<span style="line-height:1.5 !important;"&gt;TiXing.down);
}

}
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy" style="line-height:1.5 !important;"><a title="复制代码" style="color:rgb(7,179);text-decoration:underline;border:none !important;">

<img src="https://www.52php.cn/res/2019/01-10/21/51e409b11aa51c150090697429a953ed.gif" alt="复制代码" style="border:none !important;">

<p style="line-height:1.5;font-size:13px;font-family:Verdana,sans-serif;">
<img src="" alt="" style="border:0px;">


<p style="line-height:1.5;font-size:13px;font-family:Verdana,sans-serif;">
?


<p style="line-height:1.5;font-size:13px;font-family:Verdana,sans-serif;">
<span style="line-height:1.5;font-size:16px;"><span style="line-height:1.5;text-decoration:underline;">实例方法和类方法对实例变量和类变量的访问


<p style="line-height:1.5;font-size:13px;font-family:Verdana,sans-serif;">
<span style="line-height:1.5;font-size:14px;">实例方法可以对当前对象的实例变量进行操作,也可以对类变量进行操作。实例方法由实例对象调用。


<p style="line-height:1.5;font-size:13px;font-family:Verdana,sans-serif;">
<span style="line-height:1.5;font-size:14px;">类方法不能访问实例变量,只能访问类变量。类方法由类名或者实例对象调用。类方法中不能出现this或者super关键字


<div class="cnblogs_code" style="border:1px solid rgb(204,255);line-height:1.5 !important;">class<span style="line-height:1.5 !important;"> TiXing{
<span style="color:rgb(0,255);line-height:1.5 !important;">private <span style="color:rgb(0,height;
<span style="color:rgb(0,255);line-height:1.5 !important;">float<span style="line-height:1.5 !important;"> z){
up=<span style="line-height:1.5 !important;">x;
height=<span style="line-height:1.5 !important;">y;
down=<span style="line-height:1.5 !important;">z;
}
<span style="color:rgb(0,255);line-height:1.5 !important;">void<span style="line-height:1.5 !important;"> display(){
System.out.println("up is:"+up+"height is:"+height+"down is:"+<span style="line-height:1.5 !important;">down);
}
<span style="color:rgb(0,255);line-height:1.5 !important;">void change(<span style="color:rgb(0,255);line-height:1.5 !important;">float<span style="line-height:1.5 !important;"> number){
down=<span style="line-height:1.5 !important;">number;
<span style="color:rgb(0,128,0);line-height:1.5 !important;">//<span style="color:rgb(0,0);line-height:1.5 !important;">System.out.println("height:"+height);<span style="color:rgb(0,0);line-height:1.5 !important;">出错
<span style="line-height:1.5 !important;"> }
}

<span style="color:rgb(0,3<span style="line-height:1.5 !important;">);
one.display();
TiXing two=<span style="color:rgb(0,6<span style="line-height:1.5 !important;">);
one.display();
two.display();

    </span><span style="color:rgb(0,0);line-height:1.5 !important;"&gt;TiXing.display();</span><span style="color:rgb(0,0);line-height:1.5 !important;"&gt;出错</span>
    one.change(101<span style="line-height:1.5 !important;"&gt;);
    one.display();
    two.change(</span>102<span style="line-height:1.5 !important;"&gt;);
    two.display();    
}

}
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy" style="line-height:1.5 !important;"><a title="复制代码" style="color:rgb(7,179);text-decoration:underline;border:none !important;">

<img src="https://www.52php.cn/res/2019/01-10/21/51e409b11aa51c150090697429a953ed.gif" alt="复制代码" style="border:none !important;">

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读