1:学生、老师继承 + super 访问父类属性、构造、重写方法
需求:
1. 写父类 **Person**(常用人类父类)
- 成员属性:姓名 name,年龄 age
- 无参构造、有参构造
- 普通方法:`showInfo\(\)` 打印个人信息
2. 子类 **Student 学生** 继承 Person
- 新增属性:学号 id
- 和父类重名定义 age 变量
- 无参、有参构造用 `super` 调用父类构造
重写 `showInfo\(\)`,里面用:
- 直接访问本类 age
- `super\.age` 访问父类 age
- `super\.showInfo\(\)` 调用父类方法
3. 测试类创建学生对象,输出全部信息。
public class Person {
String name;
int age;
public Person(){
}
public Person(String name, int age) {
this.name = name;
this.age = 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;
}
public void showInfo(){
System.out.println("姓名:"+getName()+", 年龄:"+getAge());
}
}
public class Student extends Person{
String id;
int age;
public Student() {
}
public Student(String name, int age, String id, int age1) {
super(name, age);
this.id = id;
this.age = age1;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public void showInfo(){
System.out.println(this.age);
System.out.println(super.age);
super.showInfo();
System.out.println("学号:"+getId());
}
}
public class Tested {
public static void main(String[] args) {
Student d =new Student("张三",18,"123",20);
d.showInfo();
}
}
2:员工类 \+ static 静态变量做对象计数器
需求
1. 定义 **Employee 员工类**
- 普通成员:name,salary
- **static 静态变量**:`static int total = 0;` 统计创建员工总数
- 构造方法:每 new 一个员工,`total\+\+`
- 静态方法:`static void showTotal\(\)` 输出总员工数
2. main 里创建 3 个员工对象,调用静态方法打印总人数。
考点:**static 变量共享、静态方法、对象计数**
public class Employee {
String name;
int salary;
static int total=0;
public Employee(){
total++;
}
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
total++;
}
public static void showTotal(){
System.out.println("总员工人数:"+total);
}
public void showInfo(){
System.out.println("员工的姓名:"+name);
System.out.println("员工的工资:"+salary);
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee d=new Employee("张三",2000);
Employee d1=new Employee("李四",3000);
Employee d2=new Employee("王五",4000);
d.showInfo();
d1.showInfo();
d2.showInfo();
Employee.showTotal();
}
}
3:动物继承体系 \+ super 构造重载
需求
1. 父类 **Animal 动物**
- 属性:类型 type
- 无参构造、有参构造
2. 子类 **Dog 狗**、**Cat 猫** 都继承 Animal
- 子类构造第一行必须用 `super\(\)` / `super\(参数\)` 调用父类构造
3. 分别创建狗、猫对象,观察构造调用顺序。
考点:**super 必须在子类构造第一行**
public class Animal {
String type;
public Animal(){
}
public Animal(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
public class Dog extends Animal{
public Dog() {
}
public Dog(String type) {
super(type);
}
}
public class Cat extends Animal{
public Cat() {
}
public Cat(String type) {
super(type);
}
}
public class AnimalTest {
public static void main(String[] args) {
Cat d=new Cat("吃小鱼");
System.out.println("小猫爱:"+d.getType());
Dog d2=new Dog("吃骨头");
System.out.println("小狗爱:"+d2.getType());
}
}
4:static 静态代码块 \+ 工具类(常用工具类写法)
需求
1. 写 **ArrayUtil 数组工具类**(常用工具类)
- 私有构造:不让 new 对象
- 静态代码块:打印「数组工具类加载完成」
- 静态方法:`static void printArr\(int\[\] arr\)` 遍历打印数组
2. main 中直接用**类名调用**静态方法,不用 new 对象。
考点:**static 工具类、私有构造、静态代码块**
import java.util.Arrays;
public class ArrayUtil {
private ArrayUtil(){
}
static {
System.out.println("数组工具类加载完成");
}
static void printArr(int[]arr){
if (arr == null) {
System.out.println("数组为空");
return;
}
System.out.println(Arrays.toString(arr));
}
}
public class ArrayUtilTest {
public static void main(String[] args) {
int [] a={1,2,3,4};
ArrayUtil.printArr(a);
}
}
5:综合大题(static \+ super 混搭,考试高频)
需求
1. 父类 **Teacher 老师**
- 普通属性:name
- 静态属性:`static String school = 一中
- 方法:`work\(\)` 打印老师工作
2. 子类 **MathTeacher 数学老师** 继承 Teacher
- 重写 work \(\) 方法
- 方法内部用 `super\.work\(\)` 调用父类工作
- 直接用类名访问静态学校名称
3. 创建子类对象,调用 work \(\),同时修改静态学校属性,看所有对象是否同步变化。
public class Teacher {
String name;
static String school="一中";
public Teacher() {
}
public Teacher(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static String getSchool() {
return school;
}
public static void setSchool(String school) {
Teacher.school = school;
}
public void work(){
System.out.println(getName()+"老师工作");
}
}
public class MathTeacher extends Teacher{
public MathTeacher() {
}
public MathTeacher(String name) {
super(name);
}
@Override
public void work(){
super.work();
System.out.println("在"+Teacher.school+"工作");
}
}
public class TeacherTest {
public static void main(String[] args) {
MathTeacher d=new MathTeacher("李四");
d.work();
Teacher.school="二中";
d.work();
}
}

976

被折叠的 条评论
为什么被折叠?



