Arn*_*Das -1 java variables methods
让我们来看一个像这样的简单程序:
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[])
 {
 }   
}
虽然我可以通过这种方式纠正它:
  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它?
您可以使用两个参数声明b方法,如下例:
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[])
    {
    }   
}