Java面向对象

谈一谈你对堆栈的理解


栈:临时存储空间
堆:存放对象


示例说明你对面向对象三大特性的理解


面向对象

面向:根据,基于
对象:真实存在的实体

类和对象

类:模子(一类东西的集合)
对象:根据模子做的实体(集合里的具体一个)

封装性

定义:封装一类对象的属性和方法
属性:有什么东西(例如生命本质)
方法:能做什么事情(例如生活行动)

继承性

定义:父类的属性和方法子类也有

实例

我们模拟一集柯南

  1. 案件发生了
  2. 有人看到了现场
  3. 柯南找到了真相

用面向对象思想

  1. 有什么东西(属性)
  2. 能做什么(方法)

分析

  1. 坏人:有名字,能做坏事,有目击能力
  2. 目击者:有名字,有目击能力
  3. 侦探:有名字,能破案,有目击能力

/**
 * 基类
 * 
 * @author Administrator
 *
 */
public abstract class People {
    private String name;

    public People(String name) {
        this.name = name;
        System.out.println("我是 --> " + this.getName());
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    /**
    * 目击现场
    */
    public void withnessScene() {
        System.out.println(this.getName() + " --> 发现了现场");
    }
}

/**
 * 坏人
 * @author Administrator
 *
 */
public class BadPeople extends People {

    public BadPeople(String name) {
        super(name);
    }

    public void doBadThing() {
        System.out.println(this.getName() + " --> 做了坏事,案件发生了");
    }

}

/**
 * 侦探
 * 
 * @author Administrator
 *
 */
public class DetectivePeople extends People {

    public DetectivePeople(String name) {
        super(name);
    }

    /**
     * 找到了真相
     */
    public void findTrue() {
        System.out.println(this.getName() + " --> 找到了真相");
    }
}

/**
 * 目击者
 * 
 * @author Administrator
 *
 */
public class WithnessPeople extends People {
    public WithnessPeople(String name) {
        super(name);
    }
}

public class Main {
    public static void main(String[] args) {
        BadPeople p = new BadPeople("小李(坏人)");
        WithnessPeople w = new WithnessPeople("小王(目击者)");
        DetectivePeople d = new DetectivePeople("柯南(侦探)");
        p.doBadThing();
        w.withnessScene();
        d.findTrue();
    }
}

运行结果

我是 –> 小李(坏人)
我是 –> 小王(目击者)
我是 –> 柯南(侦探)
小李(坏人) –> 做了坏事,案件发生了
小王(目击者) –> 发现了现场
柯南(侦探) –> 找到了真相


多态性

  1. 重写:重写父类的方法
  2. 重载:方法名相同,但是传入参数种类不同
例子

生物,人,老鼠

分析

有什么:类型,性别
做什么:吃

/**
 * 生物
 * @author Administrator
 *
 */
public class Biology {

    private String type;
    private String sex;

    public Biology(String type){
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    /**
     * 吃
     */
    public void eat(){
        System.out.println("生物 --> 吃东西");
    }
}
/**
 * 人
 * 
 * @author Administrator
 *
 */
public class People extends Biology {

    public People(String type) {
        super(type);

    }

    /**
     * 吃
     */
    @Override
    public void eat() {

        System.out.println(this.getType() + " --> 吃东西");
    }

}
/**
 * 老鼠
 * 
 * @author Administrator
 *
 */
public class Mouse extends Biology {

    public Mouse(String type) {
        super(type);

    }

    /**
     * 吃
     */
    @Override
    public void eat() {
        System.out.println(this.getType() + " --> 吃东西");
    }
}
public class Main {

    public static void main(String[] args) {
        Biology b = new People("人");
        b.eat();
        b = new Mouse("老鼠");
        b.eat();
    }

}

执行结果

人 –> 吃东西
老鼠 –> 吃东西


抽象与接口

实例
/**
 * 吃的接口
 * @author Administrator
 *
 */
public interface Eat {

    public void eat();

}
/**
 * 生物
 * @author Administrator
 *
 */
public abstract class Biology implements Eat{

    private String type;


    public Biology(){
        // 关键技巧
        this.type = init();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    /**
     * 实现吃的接口
     */
    @Override
    public void eat() {
        System.out.println("生物 --> 吃东西");
    }
    public abstract String init();
}
/**
 * 老鼠
 * 
 * @author Administrator
 *
 */
public class Mouse extends Biology {

    public Mouse() {
        super();
    }

    @Override
    public String init() {
        return "老鼠";
    }


    @Override
    public void eat() {
        System.out.println(this.getType() + " --> 吃东西");
    }

}
/**
 * 人
 * 
 * @author Administrator
 *
 */
public class People extends Biology {

    public People() {
        super();
    }

    @Override
    public String init() {
        return "人";
    }

    /**
     * 吃
     */
    @Override
    public void eat() {
        System.out.println(this.getType() + " --> 吃东西");
    }

}
public class Main {

    public static void main(String[] args) {
        Biology b = new People();
        b.eat();
        b = new Mouse();
        b.eat();
    }

}

执行结果

人 –> 吃东西
老鼠 –> 吃东西

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值