Java封装、继承、多态

1.封装

核心理念:【合理隐藏,合理暴露】

java 复制代码
package com.popo.fengzhuang;

public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    // Added age validation
    public void setAge(int age) {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative.");
        }
        this.age = age;
    }
}

2.继承

子类能继承父类的非私有成员,子类对象由子类和父类共同完成

java 复制代码
package com.popo.jicheng;

public class People {
    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;
    }
}
java 复制代码
package com.popo.jicheng;

public class Student extends People {
    private String studentId;

    public String getStudentId() {
        return studentId;
    }
    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }
}
java 复制代码
package com.popo.jicheng;

public class extendDemo1 {
    public static void main(String[] args) {
        // 子类对象由子类和父类共同完成,所以也有name和age属性
        Student student = new Student();
        student.setName("张三");
        student.setAge(18);
        student.setStudentId("001");
        System.out.println(student.getName() + " " + student.getAge() + " " + student.getStudentId());
    }
}

3.权限修饰符

4.多态

把子类对象赋值给父类变量,存在方法重写

java 复制代码
package com.popo.duotai;

public class People {
    String name = "People";

    public void run(){
        System.out.println("People is running");
    }
}
java 复制代码
package com.popo.duotai;

public class Student extends People {
    String name = "Student";

    @Override
    public void run() {
        System.out.println("Student is running");
    }
}
java 复制代码
package com.popo.duotai;

public class Teacher extends People {
    String name = "Teacher";

    @Override
    public void run() {
        System.out.println("Teacher is running");
    }
}
java 复制代码
package com.popo.duotai;

public class DuotaiDemo1 {
    public static void main(String[] args) {
        // 只讨论对象和行为的多态,不考虑变量的多态
        People p1 = new Teacher();
        // 编译看左边,运行看右边
        p1.run(); // 输出: Teacher is running
        System.out.println(p1.name); // 输出: People

        People p2 = new Student();
        p2.run(); // 输出: Student is running
        System.out.println(p2.name); // 输出: People
    }
}

如果要使用子类独有的方法,可以使用强制类型转换:子类型 变量 = (子类型)(new 父对象)

使用instanceof可以判断父对象是否与子类型对应

java 复制代码
package com.popo.duotai;

public class DuotaiDemo2 {
    public static void main(String[] args) {
        People p1 = new Teacher();

        People p2 = new Student();
        
        goRun(p1); // 输出: Teacher is running
        goRun(p2); // 输出: Student is running
    }

    public static void goRun(People p) {
        if (p instanceof Teacher) {
            Teacher t = (Teacher) p;
            t.run();
        } else if (p instanceof Student) {
            Student s = (Student) p;
            s.run();
        } else {
            p.run();
        }
    }
}
相关推荐
جيون داد ناالام ميづ35 分钟前
七、MyBatis 源码剖析:核心流程拆解(初始化 + SQL 执行)
java·sql·mybatis
ClkLog-开源埋点用户分析1 小时前
【产品说明】【埋点分析】ClkLog 商业版 Java 运行环境安全更新说明
java·开发语言
秋饼1 小时前
Spring Framework 7 内置弹性实战:@Retryable 语义翻转、虚拟线程流量放大与 LLM 网关治理
java·ai·技术分享·后端开发
RuoyiOffice1 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office
yuzhiboyouye2 小时前
《阿里巴巴 Java 开发手册》的 MySQL 核心规范
java·开发语言·mysql
郝学胜-神的一滴2 小时前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
geovindu2 小时前
python: KNeighborsClassifier K
开发语言·后端·python·算法
秋饼3 小时前
Spring AI 2.0 渐进式工具披露实战:ToolSearchToolCallingAdvisor 源码机制、四种 ToolIndex 选型与多租户踩坑
java·ai·技术分享·后端开发
时间的情敌4 小时前
Three.js 射线拾取进阶:hover 高亮、点击弹窗、多物体过滤、穿透拾取
开发语言·javascript·three.js
Amumu121384 小时前
TypeScript学习: 基本类型(一)
开发语言·前端·typescript