这个代码中的问题是什么?为什么I,j的值不能用于showsub方法? [英] Whats the problem in this code? Why the values of I, j are not available to showsub method?

查看:83
本文介绍了这个代码中的问题是什么?为什么I,j的值不能用于showsub方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class  A 
{
int 我;
int j;
void show( int i, int j)
{
System.out.println( i = + i + + j = + j);
}
}

class B extends A
{
int k;
void showsub( int k)
{
System.out .println( i = + i + + j = + j + + K = + K);
}
}

class 继承
{
public static void main(字符串 args [])
{
一个obj1 = new A();
B obj2 = new B();
obj1.show( 3 5 );
obj2.show( 4 6 );
obj2.showsub( 7 );
}
}





我的尝试:



我试过这段代码,但无法理解为什么i,j的值不能用于showsub方法?

解决方案

< blockquote>因为这些变量对于类A是私有的,因此不能用于类B的对象。您需要将它们声明为public或protected。检查 C ++ Java参考手册中的存储类。


它们可用。但是,它们是零初始化的。你的程序正确输出:

 i = 3 j = 5 
i = 4 j = 6
i = 0 j = 0 k = 7


class A
{
  int i;
  int j;
  void show(int i,int j)
  {
    System.out.println("i="+i+" "+"j="+j);
  }
}

class B extends A
{
  int k;
  void showsub(int k)
  {
    System.out.println("i="+i+" "+"j="+j+" "+"k="+k);
  }
}

class inherit
{
  public static void main(String args[])
  {
    A obj1=new A();
    B obj2=new B();
    obj1.show(3,5);
    obj2.show(4,6);
    obj2.showsub(7);
  }
}



What I have tried:

I have tried this code, but could not understand why the values of i,j are not available to showsub method?

解决方案

Because those variables are private to class A and therefore not available to an object of class B. You need to declare them public or protected. Check your C++ Java reference manual for storage classes.


They do are available. However, they are zero-initialised. your program correctly outputs:

i=3 j=5
i=4 j=6
i=0 j=0 k=7


这篇关于这个代码中的问题是什么?为什么I,j的值不能用于showsub方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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