java进阶- 继承 -super ,-方法重写 -权限修饰符在继承中 -构造方法的访问特点27
package com.inherit; public class day { // 提高代码的复用性 public static void main(String[] args) { zzz z = new zzz(); z.setName("张三"); z.setAge(23); System.out.println(z.getName()+z.getAge()); } } //父 class fff { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } //子 class zzz extends fff{ }
super:
package com.inherit;
public class day02 {
// 子父类中,出现了方法声明一模一样的方法(方法名,参数,返回值)
// 在创建子类对象,调用方法的时候,会优先使用子类的方法逻辑
// 这虽然是就近原则的现象,但其实是子类的方法,对父类的方法,进行了重写操作
public static void main(String[] args) {
zi z = new zi();
z.method();
z.show();
}
}
class fu {
// private String name;
int age = 10;
public void show(){
System.out.println("fu");
}
}
class zi extends fu{
int age = 120;
public void method() {
int age = 1200;
System.out.println(age);
System.out.println(this.age);
System.out.println(super.age);
}
public void show(){
System.out.println("zi");
}
}
package com.inherit;
public class overwrite {
// 方法重载(overload):在同一个类中,方法名相同,参数不同,与返回值无关
// 参数不同:类型不同,个数不同,顺序不同
// 方法重写(Override):在子父类当中,出现了方法声明一模一样的方法(方法名,参数,返回值)
// 目标1:能够独立识别出,方法是不是重写的方法
// 注解:@0 verride
// 目标2:方法重写的使用场景当子类需要父类的方法,但是觉得父类的方法逻辑不好〔修改丨增强)就可以对父类的方法进行重写
public static void main(String[] args) {
son s = new son();
s.method();
}
}
class father{
//父类中私有方法不能重写
public void method(){
System.out.println("抢");
System.out.println("结婚");
System.out.println("压寨夫人");
}
}
class son extends father{
//重写父类时,子类权限必须大于父类
@Override
public void method() {
super.method();
System.out.println("礼物");
}
}
package com.inherit; public class dai4 { public static void main(String[] args) { C c = new C(); c.methodA(); c.methodB(); c.methodC(); } } class A{ public void methodA() { System.out.println("a"); } } class B extends A{ public void methodB() { System.out.println("b"); } } class C extends B{ public void methodC() { System.out.println("c"); } }
构造方法的访问特点:
案例:
package com.inherit;
public class day6 {
public static void main(String[] args) {
teacher t = new teacher("张伟", 23);
t.teacher();
stu stu = new stu("李四", 20, 100);
stu.stu();
}
}
class human {
private String name;
private int age;
public human() {
}
public human(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
*
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
*
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
*
* @param age
*/
public void setAge(int age) {
this.age = age;
}
}
class teacher extends human {
public teacher() {
}
public teacher(String name, int age) {
super(name, age);
}
public void teacher() {
System.out.println("姓名为" + getName() + "的" + getAge() + "岁的老师为你讲课");
}
}
class stu extends teacher {
private double score;
public stu(double score) {
this.score = score;
}
public stu(String name, int age, double score) {
super(name, age);
this.score = score;
}
public void stu() {
System.out.println("姓名为" + super.getName() + "的" + super.getAge() + "岁的学生正在为自己" + score + "的成绩发愁");
}
}
案例: package com.staff; public class staff { private String name; private int age; private double salary; public staff() { } public staff(String name, int age, double salary) { this.name = name; this.age = age; this.salary = salary; } /** * 获取 * @return name */ public String getName() { return name; } /** * 设置 * @param name */ public void setName(String name) { this.name = name; } /** * 获取 * @return age */ public int getAge() { return age; } /** * 设置 * @param age */ public void setAge(int age) { this.age = age; } /** * 获取 * @return salary */ public double getSalary() { return salary; } /** * 设置 * @param salary */ public void setSalary(double salary) { this.salary = salary; } }
package com.staff; public class programmer extends staff { public programmer() { } public programmer(String name, int age, double salary) { super(name, age, salary); } public void programmer() { System.out.println("姓名为" + getName() + "的" + getAge() + "岁的工资为" + getSalary()); } } class manager extends programmer { private int money; public manager(int money) { this.money = money; } public manager(String name, int age, double salary, int money) { super(name, age, salary); this.money = money; } public void manager() { System.out.println("姓名为" + getName() + "的" + getAge() + "岁的工资为" + getSalary() + "还有" + money + "的奖金"); } }
package com.staff; public class test { public static void main(String[] args) { programmer pr = new programmer("张三",23,1500.00); manager pr2 = new manager("李四",26,18000,5000); pr.programmer(); pr2.manager(); } }
代码改变了我们,也改变了世界