我们如何在不使用父类帮助的情况下将变量从一个方法传递到同一类中的另一个方法? [英] How can we pass variables from one method to another in the same class without taking the help of parent class?

查看:76
本文介绍了我们如何在不使用父类帮助的情况下将变量从一个方法传递到同一类中的另一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看一个像这样的简单程序:

let's take a simple program like this :

public class Dope
{
public void a()
{
   String t = "my";
  int k = 6;
}
public void b()
{
    System.out.println(t+" "+k);/*here it shows an error of not recognizing any variable*/
}
public static void main(String Ss[])
 {

 }   
}

尽管我可以通过这种方式纠正它:

although i can correct it by just resorting to this way :

  public class Dope
{
String t;
  int k ;
public void a()
{
    t = "my";
   k = 6;
}
public void b()
{
    System.out.println(t+" "+k);
}
 public static void main(String Ss[])
 {

 }   
}

但是我想知道以前的程序中是否有任何方法可以将method a中声明的变量传递给method b而无需父类的帮助?

but i wanted to know if there's any way in my former program to pass the variables declared in method a to method b without taking the help of parent class ?

推荐答案

您可以使用两个参数声明b方法,如下例:

You can declare b method with two parameters, as following example:

public class Dope
{
    public void a()
    {
        String t = "my";
        int k = 6;

        b(t, k);
    }

    public void b(String t, int k)
    {
        System.out.println(t+" "+k);
    }

    public static void main(String Ss[])
    {

    }   
}

这篇关于我们如何在不使用父类帮助的情况下将变量从一个方法传递到同一类中的另一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆