包的应用实例

  • 格式:doc
  • 大小:43.50 KB
  • 文档页数:5

//应用继承的实例_自定义学生类
//Student_Show.java
class Student
{ int stu_id;
void set_id(int id)
{ stu_id=id; }
void show_id()
{ System.out.println("The student_ID is:"
+stu_id);}
}
class UniversityStudent extends Student
{ int dep_number;
void set_dep(int dep_num)
{ dep_number=dep_num; }
void show_dep()
{ System.out.println("The dep_number is:"
+dep_number); }
}
public class Student_Show
{ public static void main(String args[])
{ UniversityStudent Lee=
new UniversityStudent();
Lee.set_id(12345678);
Lee.set_dep(124);
Lee.show_id();
Lee.show_dep();
}
}
运行结果:
E:\>java Student_Show
The student_ID is:12345678
The dep_number is:124
用包实现:
//文件名:Student.java
存放目录:E:\temp\pkg\ //假设temp为工作目录package pkg;
class Student
{ int stu_id;
void set_id(int id)
{ stu_id=id; }
void show_id()
{ System.out.println("The student_ID is:"+stu_id);}
}
//文件名:Student_Show.java
存放目录:位于p kg子目录的上一级工作目录E:\ temp
import pkg. Student;
class UniversityStudent extends Student
{ int dep_number;
void set_dep(int dep_num)
{ dep_number=dep_num; }
void show_dep()
{ System.out.println("The dep_number is:"+dep_number); }
}
class S tudent_Show
{ public static void main(String args[])
{ UniversityStudent Lee=new UniversityStudent();
Lee.set_id(12345678);
Lee.set_dep(124);
Lee.show_id();
Lee.show_dep();
}
}
编译结果:
E:\temp>javac Student_Show.java
Student_Show.java:1: p kg.Student is not public in pkg; cannot be accessed from outside package
import pkg. Student;
^
Student_Show.java:2: p kg.Student is not public in pkg; cannot be accessed from outside package
class UniversityStudent extends Student
^
Student_Show.java:2: Student() is not public in p kg.Student; cannot be accessed
from outside package
class UniversityStudent extends Student
^
Student_Show.java:13: cannot find symbol
symbol : method set_id(int)
location: class UniversityStudent
Lee.set_id(12345678);
^
Student_Show.java:15: cannot find symbol
symbol : method show_id()
location: class UniversityStudent
Lee.show_id();
^
5 errors
修改如下:
// 目录:E:\temp\pkg\Student.java
package p kg;
public class Student
{ int stu_id;
void set_id(int id)
{ stu_id=id; }
void show_id()
{ System.out.println("The student_ID is:"+stu_id);} }
编译结果:
E:\temp>javac Student_Show.java
Student_Show.java:15: cannot find symbol
symbol : method set_id(int)
location: class UniversityStudent
Lee.set_id(12345678);
^
Student_Show.java:17: cannot find symbol
symbol : method show_id()
location: class UniversityStudent
Lee.show_id();
^
2 errors
再次修改如下:
// 目录:E:\temp\pkg\Student.java
package p kg;
public class Student
{ int stu_id;
public void set_id(int id)
{ stu_id=id; }
public void show_id()
{ System.out.println("The student_ID is:"+stu_id);}
}
编译通过,运行结果:
E:\temp>java Student_Show
The student_ID is:12345678
The dep_number is:124
扩充编译:
修改如下:
// 目录:E:\temp\pkg\Student.java
package p kg;
private class Student
//protected class Student //Error: modifier protected not allowed here protected class S tudent
^
{ int stu_id;
public void set_id(int id)
{ stu_id=id; }
public void show_id()
{ System.out.println("The student_ID is:"+stu_id);}
}
E:\temp>javac Student_Show.java
.\pkg\Student.java:3: modifier private not allowed here
private class Student
^
Student_Show.java:4: p kg.Student is not public in pkg; cannot be accessed from outside package
class UniversityStudent extends Student
^
Student_Show.java:4: Student() is not public in pkg.Student; cannot be accessed from outside package
class UniversityStudent extends Student
^
3 errors
如果将成员方法set_id(int id) 改为protected访问属性,则出错
// 目录:F:\temp\pkg\Student.java
package p kg;
public class Student
{ int stu_id;
protected void set_id(int id)
{ stu_id=id; }
public void show_id()
{ System.out.println("The student_ID is:"+stu_id);}
}
E:\temp>javac Student_Show.java
Student_Show.java:15: set_id(int) has protected access in pkg.Student Lee.set_id(12345678);
^
1 error。