🍬 博主介绍
👨🎓 博主介绍:大家好,我是 hacker-routing ,很高兴认识大家~
✨主攻领域:【渗透领域】【应急响应】 【python】 【VulnHub靶场复现】【面试分析】
🎉点赞➕评论➕收藏 == 养成习惯(一键三连)😋
🎉欢迎关注💗一起学习👍一起讨论⭐️一起进步📝文末有彩蛋
🙏作者水平有限,欢迎各位大佬指点,相互学习进步!
目录
构造方法的访问特点
怎末调用父类构造方法的?
练习
编辑
this、super使用总结
练习
带有继承结构的标准Javabean类
代码
构造方法的访问特点
- 父类中的构造方法不会被子类继承
- 子类中所以的构造方法默认先访问父类中的无参构造,再执行自己
怎末调用父类构造方法的?
- 子类构造方法的第一行语句默认都是:super(),不写也存在,且必须在第一行
- 如果想调用父类有参构造,必须手写super进行调用
练习
父类:person
package demon4;
public class person {
String name;
int age;
public person() {
}
public person(String name, int age) {
this.name = name;
this.age = age;
}
}
子类:student
package demon4;
public class student extends person{
public student(){
//子类构造方法中隐藏的super()去访问父类的无参构造
super();
System.out.println("子类的无参构造");
}
//子类要调用父类中的构造方法需要手动调用
public student(String name,int age){
super(name,age);
}
}
测试类:test
package demon4;
public class test {
public static void main(String[] args) {
//创建学生对象
student s = new student("zhangsan",23);
System.out.println(s.name + "," + s.age);
}
}
this、super使用总结
- this:理解为一个变量,表示当前方法调用者的地址值
- super:代表父类存储空间
练习
带有继承结构的标准Javabean类
1.经理
成员变量:工号,姓名,工资,管理奖金
成员方法:工作(管理其他人),吃饭(吃米饭)
2.厨师
成员变量:工号,姓名,工资
成员方法:工作(炒菜),吃饭(吃米饭)
代码
父类:Employee
package demon5;
public class Employee {
private String id;
private String name;
private double salary;
public Employee() {
}
public Employee(String id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//成员方法:
//工作
public void work(){
System.out.println("员工在工作");
}
//吃饭
public void eat(){
System.out.println("吃米饭");
}
}
子类:Manager
package demon5;
public class Manager extends Employee{
private double bouns;
//空参构造
public Manager() {
}
//父类 + 子类 全部参数的构造
public Manager(String id, String name, double salary, double bouns) {
super(id, name, salary);
this.bouns = bouns;
}
public double getBouns() {
return bouns;
}
public void setBouns(double bouns) {
this.bouns = bouns;
}
//方法重写
@Override
public void work() {
System.out.println("管理员工");
}
}
子类:Cook
package demon5;
public class Cook extends Employee{
public Cook() {
}
public Cook(String id, String name, double salary) {
super(id, name, salary);
}
@Override
public void work() {
System.out.println("厨师炒菜");
}
}
测试类:test
package demon5;
public class test {
public static void main(String[] args) {
//创建对象并调用
Manager m = new Manager("001","routing",16000,8000);
System.out.println(m.getId() + "," + m.getName() + "," + m.getSalary() + "," + m.getBouns());
m.work();
m.eat();
Cook c = new Cook();
c.setId("002");
c.setName("rout");
c.setSalary(9000);
System.out.println(c.getId() + "," + c.getName() + "," + c.getSalary());
c.work();
c.eat();
}
}