Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » Java与OOP初步  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 static method怎么用,用在哪
四脚虫





发贴: 15
积分: 0
于 2004-11-12 23:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
java里static method怎么用
class TestStatic{

static int a[]=new int [10];
static int c[]=new int[10];
static //静态块
{
for(int i=0;i<10;i++){
a[i]=(int)( 100*Math.random());
}
}
void staticInterface(){ //访问静态块方法
for(int i=0;i<10;i++)
System.out.print(a[i]+" ");
}

static void staticMethod1(){ //静态方法1(同上,static没用)
for (int i=0;i<10;i++)
System.out.print(a[i]+ " ");
}

static void staticMethod2(){ //静态方法2
for(int i=0;i<10;i++){
c[i]=(int)(100*Math.random());
System.out.print(c[i]+" ");
}
}
}
public class StaticMainClass{
public static void main(String args[]){
System.out.println("以下两个对象结果相同(体现静态块作用)");
TestStatic obj1=new TestStatic();
obj1.staticInterface();
System.out.println();
TestStatic obj2=new TestStatic();
obj2.staticInterface();
System.out.println();

System.out.println("静态方法1,同上结果也应该一样:");
TestStatic.staticMethod1();
System.out.println();

System.out.println("静态方法2,两次结果不同:");
TestStatic.staticMethod2();
System.out.println();
TestStatic.staticMethod2();
}
}

请问静态方法怎么用,用在哪里,如上,静态方法2的写法是不是不合适,如'静态块'里的计算被对象通过方法调用三次结果返回一样,而直接调用'静态方法2'里的计算两次却返回结果不同


littledeer1974 edited on 2004-11-16 19:33

作者 Re:static method怎么用,用在哪 [Re:四脚虫]
robin519





发贴: 5
积分: 0
于 2004-11-13 00:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
静态,就是程序执行前java jvm就已经将内存分配好,里面的方法已经做好,就等着调用,每个静态的都只能应用一次。代码块1和 staticInterface都是输出数组a[],而代码2是输出数组c[],之前没有生成也没有与其一样的方法调用c[],和a[]肯定不一样。此方法能正常执行。没错


作者 Re:static method怎么用,用在哪 [Re:四脚虫]
四脚虫





发贴: 15
积分: 0
于 2004-11-13 13:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
非常感谢,这么快就得到答案,呵呵我明白了^_^


作者 Re:static method怎么用,用在哪 [Re:四脚虫]
四脚虫





发贴: 15
积分: 0
于 2004-11-13 13:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
还是不对,我最后几个print都是调用‘方法2’里的c数组,按道理应该是同样结果啊,而且我尝试用对象来调用,结果全都不一样,静态方法不是叫“类的方法”同哪个对象也应该没关系,因为在任何对象建立前就已经被执行了。
    System.out.println("静态方法2:");
     obj1.staticMethod2();
    System.out.println();
     TestStatic.staticMethod2();
    System.out.println();
     obj2.staticMethod2();
    System.out.println();
     TestStatic.staticMethod2();



作者 Re:static method怎么用,用在哪 [Re:四脚虫]
234aini





发贴: 43
积分: 0
于 2004-11-13 14:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
静态方法你每天都在用啊。
那个System里面的好多都是静态方法啊。:)


littledeer1974 edited on 2004-11-13 18:48

作者 Re:static method怎么用,用在哪 [Re:四脚虫]
四脚虫





发贴: 15
积分: 0
于 2004-11-13 15:41 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
是啊,所以我才越来越看不懂,比如public static void main(string args[]){}也是一个静态,但我这本书上却说,静态方法只能调用其他静态方法,并且只能访问静态变量,my god,main的静态可是什么都可以访问的啊,而且我这本书虽然基础但其他方面都讲得很不错就是在static method上才简单几句话


作者 Re:static method怎么用,用在哪 [Re:四脚虫]
qingbo777

轻薄



发贴: 106
积分: 0
于 2004-11-13 17:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
main的静态方法也不是什么都可以访问的.同其他静态方法一样,只能调用静态方法.只能对对象的形式调用,即:要调用某个对象的方法,首先要在main方法new一个对象.


总想让自己变的帅一点,
却发现离这个目标越来越远.
作者 Re:static method怎么用,用在哪 [Re:四脚虫]
四脚虫





发贴: 15
积分: 0
于 2004-11-14 09:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
但main可以调用public方法(我们创建一个方法时不加private或public时默认都是public吧)?还有4楼的问题,还是没人回答吗


作者 Re:static method怎么用,用在哪 [Re:四脚虫]
qingbo777

轻薄



发贴: 106
积分: 0
于 2004-11-14 14:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你给我举个例子说明main里可以调用public方法么????
四楼的问题:method2本身的结果就是随机的.你想让它怎么不一样啊???



总想让自己变的帅一点,
却发现离这个目标越来越远.
作者 Re:static method怎么用,用在哪 [Re:四脚虫]
四脚虫





发贴: 15
积分: 0
于 2004-11-15 00:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
class A{
  public void amethod(){
    System.out.println("A");
  }
}

public class InterfaceMainClass{
  public static void main(String args[]){
    A aobj = new A();
    aobj.amethod();
  }
}
这个不就是调用public方法了吗



作者 Re:static method怎么用,用在哪 [Re:四脚虫]
qingbo777

轻薄



发贴: 106
积分: 0
于 2004-11-15 11:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
正因为在main方法里生成了A类的实例aobj,所以才有aobj.amethod()的调用.
这不等于main方法可以调用public方法.aobj.amethod()是A在调用自身的方法而已.



总想让自己变的帅一点,
却发现离这个目标越来越远.
作者 Re:static method怎么用,用在哪 [Re:四脚虫]
四脚虫





发贴: 15
积分: 0
于 2004-11-16 19:26 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
呵呵谢谢qingbo,这两天又看了书后面几章,理解你说的意思了,调用方法和通过对象调用方法是两马事,静态方法确实只能调用其他静态方法,而动态方法确可以调用动态或静态方法,等等.......还有默认情况下,和public,private,protected中任何一种的访问控制都是不同的^_^


作者 Re:static method怎么用,用在哪 [Re:四脚虫]
redsonic





发贴: 1
积分: 0
于 2004-11-25 21:43 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
嗯,不带任何访问修饰(public,private,protected)的话就是package,能够被包内的类访问!



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923