一般修饰:类成员变量和成员方法
protected修饰的成员可以被三种类所引用
1.该类自身
2.与他在同一个包中的其他类
3.在其他包中的该类的子类
包:包将各种类组织在一起,使得重新功能清楚,结构分明(类似文件夹组织文件)
举例:类的继承,创建个人类Person,再以该类为父类创建一个学生子类Student
class Person{
private String name;
private int age;
public Person(){
System.out.println("调用了个人类的构造方法Person()");
}
public void setNameAge(String name,int age){
this.name = name;
this.age = age;
}
public void show(){
System.out.println("姓名"+name+"年龄"+age);
}
}
class Student extends Person{
private String department;
public Student(){
System.out.println("调用了学生类的构造方法Student()");
}
public void setDepartment(String department) {
this.department = department;
System.out.println("我是" + department + "的学生");
}
}
public class app8_1 {
public static void main(String[] args) {
Student stu = new Student();
stu.setNameAge("张三",18);
stu.show();
stu.setDepartment("计算机系");
}
}
执行子类的构造方法之前,会先调用父类中没有参数的构造方法,帮助继承自父类的成员做初始化操作
继承中的构造方法
问题:父类中有多个构造方法时,如何调用父类中的某个特定的构造方法呢?
例:以Person作为父类,创建学生子类Student,并在子类调用父类里指定的构造方法
class Person{
private String name;
private int age;
public Person(){
System.out.println("调用了个人类的构造方法Person()");
}
public Person(String name,int age){
System.out.println("调用了Person类的有参构造方法");
this.name = name;
this.age = age;
}
public void show(){
System.out.println("姓名"+name+"年龄"+age);
}
}
class Student extends Person{
private String department;
public Student(){
System.out.println("调用了学生类的构造方法Student()");
}
public Student(String name,int age,String department){
super(name,age);
this.department=department;
System.out.println("我是"+department+"的学生");
System.out.println("调用了学生类的有参构造方法" +
"Student(String name,int age,String department)");
}
}
public class app8_1 {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student("李小四",23,"信息系");
stu1.show();
stu2.show();
}
}
子类访问父类特定的构造方法
super(参数列表)
写在子类构造方法第一行
据参数个数与类型,执行父类相应的构造方法
如无super(),将会执行父类中没有参数的构造方法如上面的代码
super的其他用法
访问父类的变量和成员方法
格式:super.变量名;super.方法名
注意:
(1)super不能访问在子类中添加的成员
(2)在之类里,super无法访问父类中的私有成员
一般可访问情况:父类成员声明protected
class Person{
protected String name;
protected int age;
public Person(){}//在定义了有参的构造方法后,系统不会主动定义这个,所以必须我们自己定义一个
public Person(String name,int age){
this.name = name;
this.age = age;
}
public void show(){
System.out.println("姓名"+name+"年龄"+age);
}
}
class Student extends Person{
private String department;
public Student(String name,String department){
this.department=department;
this.name = name;//因为protected权限所以可以在子类中访问它
super.age = 25;//直接写出age也可以
super.show();
System.out.println("系别"+department);
}
}
public class app8_1 {
public static void main(String[] args) {
Student stu = new Student("李小四","信息系");
}
}
执行子类的构造方法之前,需要先调用父类中的构造方法
可以使用super()调用父类构造方法,如无,则调用父类无参构造方法
如果父类定义了构造方法,系统将不会提供默认构造方法
编程训练
class PersonB
{ String name;
int age;
public PersonB( )
{ System.out.println("PersonB( )被调用"); }
public PersonB(String newName)
{ name=newName;
System.out.println("PersonB(String newName)被调用");
}
public void introduce( )
{ System.out.println("我是"+name+",今年"+age+"岁"); }
}
class StudentB extends PersonB
{ 【代码1】//创建StudentB类的无参构造方法,能显示"StudentB( )被调用"
public StudentB(String newName,int newAge)
{【代码2】//调用父类的public PersonB(String newName)构造方法,传入newName参数
【代码3】 //将newAge赋值给age属性
}
}
class Test
{ public static void main(String[] args)
{ StudentB s1=new StudentB();
StudentB s2=new StudentB("张三",19);
【代码4】 //调用s2的introduce( )方法
}
}
答案
class PersonB
{ String name;
int age;
public PersonB( ) {
System.out.println("PersonB( )被调用");
}
public PersonB(String newName) {
name=newName;
System.out.println("PersonB(String newName)被调用");
}
public void introduce( ) {
System.out.println("我是"+name+",今年"+age+"岁");
}
}
class StudentB extends PersonB
{
StudentB(){}//创建StudentB类的无参构造方法,能显示"StudentB( )被调用"
public StudentB(String newName,int newAge) {
super(newName);//调用父类的public PersonB(String newName)构造方法,传入newName参数
age=newAge; //将newAge赋值给age属性
}
}
class Test
{
public static void main(String[] args) {
StudentB s1=new StudentB();
StudentB s2=new StudentB("张三",19);
s2.introduce(); //调用s2的introduce( )方法
}
}