【发布时间】:2014-06-24 12:03:20
【问题描述】:
在这里,我想将受保护的变量rollno 访问到我的另一个包package2。
所以伙计们,我把这两个程序都放了:
所以在这里当我运行Check.java 它会抛出一个错误not defined @ Protected1
这里唯一的目标是从包测试访问受保护的变量到package2
package test;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Protected1
{
static protected int rollno;
static public String name;
static private int mobileno;
public void Protected1()
{
System.out.println("lets start doing the Assignment for Protected variable assigning in another package");
}
public static void main(String a[])
{
/*protected int rollno;
public String name;
private int mobileno;*/
Scanner br=new Scanner(System.in);
//Protected1 st=new Protected1();
try
{
System.out.println("Enter the name:");
name=br.nextLine();
System.out.println("Enter the rollno:");
rollno=br.nextInt();
System.out.println("Enter the mobile number:");
mobileno=br.nextInt();
System.out.println("lets just print it out:");
System.out.println("name is \t"+name);
System.out.println("rollno is \t"+rollno);
System.out.println("mobileno is \t"+mobileno);
}
catch(Exception e)
{
System.out.println("Exception caught");
e.getMessage();
}
}
}
package package2
import java.io.*;
import java.util.*;
import test.* ;
public class Check extends Protected1
{
public int Check()
{
System.out.println("hello guys ..lets try");
return 0;
}
public static void main(String a[])
{
Scanner dr=new Scanner(System.in);
Protected1 as=new Protected1();
int roll=as.rollno;
System.out.println("type welcome");
String input=dr.nextLine();
System.out.println("now we have to assess the protected variable from Protected 1 i.e"+roll);
}
}
【问题讨论】:
标签: java visibility packages access-specifier