今日所学:
1.this的用法补充以及super的用法
1>this()调用本类的构造函数
2>super()
[1]表示父类的对象 即父类的构造函数.
[2]可用于子类构造方法中调用父类的构造方法
2.继承的知识点补充:
1>子类不能继承父类的私有和被隐藏的变量和方法
注:[1]子类可用 super 访问父类被隐藏的变量和方法
[2]子类不能继承父类中有final修饰的方法
2>子类不能删除父类的变量和方法
3>子类可以重定义(即:覆盖)父类的方法
注:子类不能覆盖父类中有final修饰的方法
4>子类可以重载父类的静态函数(类方法)和非静态函数(实例方法)
5>子类可以增加自己的属性(即:变量)和方法(即:函数)
说明:有对java入门3.23所学知识点的补充,还有关于继承的新的知识点
在理解方面的难度有所增加。
以下为代码截图和代码文本
package cn;
/*this 可以作为函数调用本类的构造函数this() 对于继承来说 super 表示父类对象,super()就是父类构造函数
* 创建子类时 父类的构造函数是否执行 是 调用子类的无参构造函数 父类的无参构造函数默认被执行 这样就可以将父类的一些数据初始化
* 1. 子类继承父类的非私有成员变量 包括 实例变量和类变量
* 2. 子类继承父类除构造方法外的成员方法,父类的构造方法创造的是父类的对象,子类必须声明自己的构造方法 便于创建自己的实例对象
* 如果在子类构造方法中调用父类的构造方法可以使用 super()
* 3.子类不能继承父类的私有变量和方法,也不能继承父类被隐藏的成员变量和方法
* 4.子类不能删除父类的变量和方法
* 5.子类可以增加自己的属性(变量)和方法
* 6.子类可以重定义 父类的方法 ,即覆盖
* 7.子类如何访问父类的成员变量和被隐藏的方法,使用super
* 8.在子类中可以重载父类的静态函数(类方法)和非静态函数(实例方法)
* 9.子类不能继承和覆盖父类中有final修饰的方法
*/
public class TestExtend {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// Student st=new Student("Eric",20);
// System.out.println(st.getAge());
// System.out.println(st.getName());//子类Student中并没有getAge(),getName()[不是父类的构造函数]但是父类中有然后子类Student就继承了
// Student st1=new Student();
Teacher t=new Teacher();
System.out.println(t.id);
t.setName("tom");
t.setAge(100);
System.out.println(t.getName());
System.out.println(t.getAge());
Teacher t1=new Teacher("edison",28,"math");
t1.getInformation();
t1.setInformation("lily", 18);//系统会自动根据函数参数选择父类继承的方法 ,还是子类重新定义(即重载)的方法
t1.getInformation();
t1.setInformation("sary", 20,"art");
t1.getInformation();
}
}
class People{
public String name;//name 的访问权限为私有
public int age;
public int id=10;//验证变量隐藏和不被继承
public People(){
System.out.println("执行父类的无参构造函数");
}
public People(String name,int age){
this.name=name;
this.age=age;
}
public int getAge(){
System.out.println("执行父类的getAge()");
return age;
}
public void setAge(int age){
this.age=age;
}
public String getName(){
System.out.println("执行父类的方法 获取姓名");
return name;
}
public void setName(String name){
this.name=name;
}
public void setInformation(String name,int age){
this.name=name;
this.age=age;
}
public void getInformation(){
System.out.println(name+"年龄"+age);
}
}
class Student extends People{
String department;//院系
public Student(){
}
public Student(String name,int age){
// this.age=age;
//this.name=name;
//super(name,age);//调用父类的有参构造函数
super();//显式 调用父类无参构造函数
System.out.println("调用子类的有参构造函数");
}
}
class Teacher extends People{
String specialty;//专业
public int id=20;//此时子类的变量就将父类的同名变量隐藏了
public Teacher(){
}
public Teacher(String name,int age,String specialty){
super(name,age);
this.specialty=specialty;
}
public String getName(){//子类中有和父类一样的方法 则子类覆盖了父类的方法
System.out.println("执行子类的方法 获取姓名");
return name;
}
public int getAge(){//子类中有和父类一样的方法 则子类覆盖了父类的方法
System.out.println("执行子类的getAge");
int age=super.getAge();
return age;
}
public void setInformation(String name,int age,String specialty){//实现对父类同名函数的重载
//覆盖:子类函数和父类函数的名称和参数完全一致 重载: 函数的参数个数或参数类型至少有一个不同
this.name=name;
this.age=age;
this.specialty=specialty;
}
public void getInformation(){//覆盖
System.out.println(this.name+"年龄:"+this.age+"专业:"+this.specialty);
}
}