黑马程序员技术交流社区

标题: 函数引用问题搞不明白 [打印本页]

作者: wudongzhe    时间: 2012-11-24 15:05
标题: 函数引用问题搞不明白
本帖最后由 eatdefecat 于 2012-11-24 15:06 编辑

class Hs
{
        public static void main(String[] args)
        {
        show(x);
        System.out.println("x="+x);      //我想输出下面函数的值 不知道怎么写法      
        }
        
        public static int show(int x)
        {
        return x=4;
        }
}

作者: wudongzhe    时间: 2012-11-24 15:12
不是在函数下面输出  我就想知道怎么引用函数
作者: 潘天功    时间: 2012-11-24 15:27
你可以这样写:
public class Hs  
{
        public static void main(String[] args)
        {
                System.out.println(Demo.show(4));
        }
}
class Demo
{
    public static int show(int x){
             return x ;
       }
}
因为你的show方法是静态static修饰的,所以可以直接通过类名直接调用
作者: wudongzhe    时间: 2012-11-24 16:11
。。你书写的这我还没学到了 看不懂 我不是为了求输出4这个结果 如果要求这样简单写法也行
class Hs
{
        public static void main(String[] args)
        {
        int x=show(4);
        System.out.println("x="+x);      
        }
        
        public static int show(int x)
        {
        return x;
        }
}
我想知道定义一个函数怎么运用的
比如说这句  public static int show(int x)
        {
        return x;
        }
都是未知的   如果 写成return x=4; 到上面怎么引用

作者: wudongzhe    时间: 2012-11-24 16:12
我现在就是这里绕不过来。。
作者: 郑丹丹    时间: 2012-11-24 18:38
本帖最后由 dan8612 于 2012-11-24 18:58 编辑
  1. class Hs
  2. {
  3. public static void main(String[] args)
  4. {
  5. show(x);//改为show(4);
  6. System.out.println("x="+x); //我想输出下面函数的值 不知道怎么写法
  7. }

  8. public static int show(int x)
  9. {
  10. return x=4;//改为return x;
  11. }
  12. }

复制代码

作者: 潘天功    时间: 2012-11-24 18:45
eatdefecat 发表于 2012-11-24 16:11
。。你书写的这我还没学到了 看不懂 我不是为了求输出4这个结果 如果要求这样简单写法也行
class Hs
{

写成return x=4是不对的、因为你前面定义的是一个int类型数据接收、返回的必须是int类型的,而return x=4返回一个表达式。
作者: 陈山    时间: 2012-11-24 19:57
根据你写的show方法的代码来看,不管传的参数是什么值,返回都是不变的值,那就是4,所以你不妨这么写:class Hs
{
        public static void main(String[] args)
        {
                System.out.println("x="+show(0));      //我想输出下面函数的值 不知道怎么写法      
        }
        
        public static int show(int x)
        {
        return x=4;
        }
}
当然你也可以把show(0)当中的0换成任意的int型数值。

作者: 殷士剑    时间: 2012-11-24 20:22
问题:在main方法中的局部变量 x 跟在 show方法中的局部变量 x 会相互影响吗?
从这一题中不难得出一个结论:
方法中的局部变量只在各自的方法体内有效,不会影响到其他方法中的同名变量。

你在main方法中没有定义 x 的类型,初始化 x,所以,编译时就不会通过。
作者: wudongzhe    时间: 2012-11-24 23:30
潘天功 发表于 2012-11-24 18:45
写成return x=4是不对的、因为你前面定义的是一个int类型数据接收、返回的必须是int类型的,而return x=4 ...

懂了 函数返回的都是表达式?
作者: wudongzhe    时间: 2012-11-24 23:31
eatdefecat 发表于 2012-11-24 23:30
懂了 函数返回的都是表达式?

知道了 我写的是表达式所以不能返回:handshake
作者: 潘天功    时间: 2012-11-24 23:32
eatdefecat 发表于 2012-11-24 23:30
懂了 函数返回的都是表达式?

返回的基本数据类型(四种八类)、也可能是空的,什么也不返回用void 就行
作者: wudongzhe    时间: 2012-11-24 23:33
殷士剑 发表于 2012-11-24 20:22
问题:在main方法中的局部变量 x 跟在 show方法中的局部变量 x 会相互影响吗?
从这一题中不难得出一个结论 ...

人才 看我的代码就知道 这个题目的问题了  视频肯定熟读了。。
作者: wudongzhe    时间: 2012-11-24 23:35
潘天功 发表于 2012-11-24 23:32
返回的基本数据类型(四种八类)、也可能是空的,什么也不返回用void 就行 ...

嗯 谢了 有了豁然开朗的感觉
作者: wudongzhe    时间: 2012-11-25 11:09
eatdefecat 发表于 2012-11-24 23:35
嗯 谢了 有了豁然开朗的感觉


public class han
{
        public static void main(String[] args)
        {
                int c = jia(100);    //这里的100写成num就出错 但这里随便写数字 这行c赋值都是5怎么回事?               
                int b=10;
                c=c+b;
        System.out.println(c);
        }

        
        public static int jia(int num)
        {
        return         num=5;
        }
}
我这里的 num=5 也是表达式吧  像那样的是返回int类型
作者: wudongzhe    时间: 2012-11-25 11:10
潘天功 发表于 2012-11-24 23:32
返回的基本数据类型(四种八类)、也可能是空的,什么也不返回用void 就行 ...


public class han
{
        public static void main(String[] args)
        {
                int c = jia(100);    //这里的100写成num就出错 但这里随便写数字 这行c赋值都是5怎么回事?                
                int b=10;
                c=c+b;
        System.out.println(c);
        }

        
        public static int jia(int num)
        {
        return         num=5;
        }
}
我这里的 num=5 也是表达式吧  像那样的是返回int类型

作者: 潘天功    时间: 2012-11-25 11:50
eatdefecat 发表于 2012-11-25 11:10
public class han
{
        public static void main(String[] args)

你这里只能返回num、即return num
在主方法调用时你可以把num进行赋值,随便传一个数就行,但不能再把num写上了
因为你调用的是jia()方法、方法中已经定义好了你想要的功能。你只要把你想打印的输入就行
作者: super_Xiong    时间: 2012-11-25 19:20
eatdefecat 发表于 2012-11-24 16:11
。。你书写的这我还没学到了 看不懂 我不是为了求输出4这个结果 如果要求这样简单写法也行
class Hs
{

首先你定义的int x=show(4);中的x和show 方法里的参数int x不是一个x ,这个必须清楚,首先是将4传到show方法里赋给show (int x)里面的x,然后show方法结束后返回一个int 类型的值,再赋个主函数里面的x。
作者: super_Xiong    时间: 2012-11-25 19:22
eatdefecat 发表于 2012-11-24 23:30
懂了 函数返回的都是表达式?

你定义的函数是返回一个int类型的值,不返回表达式
作者: 王斌    时间: 2012-11-26 16:38
class Hs
{
        public static void main(String[] args)
        {
      
        System.out.println("x="+ show(4));      //我想输出下面函数的值 不知道怎么写法      
        }
        
        public static int show(int x)
        {
        return x;
        }
}


这样就可以了。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2