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();
        }
    }
}
相关推荐
秋饼5 分钟前
Computer Use Agent Java 落地实战:感知-行动循环、执行器与安全围栏生产级实现
java·ai·技术分享·后端开发
冻柠檬飞冰走茶2 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊2 小时前
LeetCode2161~2180题目解析
java·leetcode
FRing032 小时前
vue2干货总结7(自定义指令、插槽、单页应用程序、路由的使用(VueRouter))
开发语言·前端·vue.js·vue2
雪落漂泊2 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
波力海苔夹心脆6753 小时前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
不会算法的笨小孩3 小时前
异常的使用以及会出现的问题
开发语言·c++·c++11·异常
文丑颜不良啊4 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发4 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
天天被压力4 小时前
【A股分钟级高频数据完全手册 #07】北交所历史分钟K线接口:京市Pro版分时批量
java·人工智能·python