Java三大法宝:封装、继承、多态——贫道吕洞宾的修仙秘籍

32 阅读3分钟

Java三大法宝:封装、继承、多态——贫道吕洞宾的修仙秘籍

作者:会编程的吕洞宾(一个在代码世界御剑飞行的程序员)

各位道友,今日贫道要传授Java修仙三大法宝:封装、继承、多态。掌握了这三样,你的代码就能"道法自然,天人合一"!

一、封装:修仙界的"内敛修为"

封装就像我修炼内丹,把重要的修为藏在体内,不让外人轻易窥探:

public class Cultivator {
    // 私有属性:修为不能外露
    private int cultivationLevel;
    private String secretTechnique;
    
    // 公开方法:对外展示的能力
    public void displayPower() {
        System.out.println("当前修为:" + getFormattedLevel());
    }
    
    // 私有方法:内部修炼心法
    private String getFormattedLevel() {
        return "金丹期" + cultivationLevel + "层";
    }
    
    // 设置器:控制修为提升
    public void setCultivationLevel(int level) {
        if (level > 0 && level <= 100) {
            this.cultivationLevel = level;
        }
    }
}

封装精髓:把数据藏起来,只通过安全的方法来操作,就像我把仙丹藏在葫芦里!

二、继承:修仙界的"师徒传承"

继承就像我收徒弟,徒弟继承师父的功法,还能青出于蓝:

// 师父类
public class Master {
    protected String name;
    
    public Master(String name) {
        this.name = name;
    }
    
    public void basicSpell() {
        System.out.println(name + "施展基础法术");
    }
}

// 徒弟类
public class Disciple extends Master {
    private String uniqueSkill;
    
    public Disciple(String name, String skill) {
        super(name);  // 调用师父的构造方法
        this.uniqueSkill = skill;
    }
    
    // 重写父类方法
    @Override
    public void basicSpell() {
        System.out.println(name + "施展改良版基础法术");
    }
    
    // 自有技能
    public void specialSkill() {
        System.out.println("施展独门绝技:" + uniqueSkill);
    }
}

三、多态:修仙界的"千变万化"

多态就像我既能御剑飞行,又能点石成金,同一个接口,不同表现:

public class ImmortalSystem {
    public static void main(String[] args) {
        // 多态的魅力:父类引用指向子类对象
        Master[] cultivators = {
            new Master("太上老君"),
            new Disciple("哪吒", "三头六臂"),
            new Disciple("杨戬", "七十二变")
        };
        
        for (Master cultivator : cultivators) {
            cultivator.basicSpell();  // 同样的方法,不同的表现
        }
    }
}

四、三大法宝合体:完整的修仙体系

来看看我是如何将三大法宝融会贯通的:

// 法宝基类(封装 + 为继承做准备)
public abstract class MagicTool {
    private String name;
    private int powerLevel;
    
    // 封装:通过方法访问属性
    public MagicTool(String name, int powerLevel) {
        this.name = name;
        setPowerLevel(powerLevel);
    }
    
    public void setPowerLevel(int level) {
        if (level >= 1 && level <= 10) {
            this.powerLevel = level;
        }
    }
    
    // 抽象方法:子类必须实现
    public abstract void use();
    
    // 最终方法:不能被子类修改
    public final void introduce() {
        System.out.println("法宝:" + name + ",威力:" + powerLevel);
    }
}

// 具体法宝(继承 + 多态)
class Sword extends MagicTool {
    public Sword() {
        super("青龙宝剑", 9);
    }
    
    @Override
    public void use() {
        System.out.println("剑光一闪,妖魔退散!");
    }
}

class Gourd extends MagicTool {
    public Gourd() {
        super("紫金葫芦", 10);
    }
    
    @Override
    public void use() {
        System.out.println("宝贝请转身!");
    }
}

五、实战演练:三大法宝的协同作战

public class BattleField {
    public static void main(String[] args) {
        // 多态数组:可以存放各种法宝
        MagicTool[] tools = new MagicTool[2];
        tools[0] = new Sword();    // 向上转型
        tools[1] = new Gourd();    // 向上转型
        
        for (MagicTool tool : tools) {
            tool.introduce();  // 继承的方法
            tool.use();        // 多态:各自的表现
            System.out.println("---");
        }
        
        // 向下转型:需要时恢复具体类型
        if (tools[1] instanceof Gourd) {
            Gourd gourd = (Gourd) tools[1];
            System.out.println("这是葫芦法宝的特殊用法");
        }
    }
}

六、修仙心法总结

  1. 封装:藏器于身,待时而动
    • 私有属性保护数据安全
    • 公共方法提供安全接口
  2. 继承:师法自然,薪火相传
    • 代码复用,避免重复
    • 层次清晰,结构明确
  3. 多态:千变万化,不离其宗
    • 同一接口,不同实现
    • 灵活扩展,易于维护

七、道友须知

记住贫道的三句真言:

封装让代码更安全 继承让代码更简洁 多态让代码更灵活

三大法宝结合使用,就能写出"道法自然"的好代码。就像修仙一样,开始可能觉得拘束,但练到高深境界,自然就能融会贯通!


无量天尊!愿各位道友在编程修仙路上早日得道!