面向对象(下)

2019-02-13  本文已影响0人  Kevin_Huang54

包装类

Integer i1 = 12;
Integer i2 = 12;
i1 == i2; //返回true
Integer i1 = 1234;
Integer i2 = 1234;
i1 == i2; //返回false

==和equals

s1 = "黄晓明"
s2 = "黄" + "晓明"
s3 = "黄", s4 ="晓明", s5 = s3 + s4
s6 = new String("黄晓明")
s1 == s2,输出true
s1 == s5,输出false
s1 == s6,输出false

单例模式

public class Roushan {
    //将本类对象设为私有成员变量
    private static Roushan instance;
    private int hp = 10000;
    private int damage = 100;

    //私有化构造器,使外界不能构造对象
    private Roushan() {
    }

    //公开方法获取唯一的对象
    public static Roushan getInstance() {
        if (null == instance) {
            instance = new Roushan();
        }
        return instance;
    }

    public static void main(String[] args) {
        //调用类方法获取单例对象
        Roushan roushan = Roushan.getInstance();
    }
}

final修饰符


抽象类


接口


内部类


枚举类

public enum Season {
    //所有实例必须在第一行列出,实际上等同于new若干个本类的public final static变量
    spring("春"), summer("夏"), autumn("秋"), winter("冬");

    private String name;
    //只能使用private构造器,即时不指定也默认是private
    Season(String name) {
        this.name = name;
    }

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

    public String getName() {
        return name;
    }
}
public enum Season implements Info{
    spring("春"){
        @Override
        public void info() {
            System.out.println("春暖花开");
        }
    }, summer("夏"){
        @Override
        public void info() {
            System.out.println("生如夏花");

        }
    }, autumn("秋") {
        @Override
        public void info() {
            System.out.println("风吹麦浪");
        }
    }, winter("冬") {
        @Override
        public void info() {
            System.out.println("银装素裹");
        }
    };
}
上一篇下一篇

猜你喜欢

热点阅读