Android开发

这是我的第一天学习内容

第一章

逯景华的呆萌日常~

单例模式:饿汉式
public class User {

    public User() {
    }
    private static User user = new User();

    public static User getUser(){
        return user;
    }

    public static void main(String[] args) {
        User user = User.getUser();
        User user1 =User.getUser();
        if (user==user1) {
            System.out.println(123);
        }
    }
}
构造者模式:
public class Student {

    private String name;
    private int age;
    private String email;
    private String phone;

    private Student(){
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    public static class Bulider{

        private String name;
        private int age;
        private String email;
        private String phone;

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

        public Bulider setAge(int age){
            this.age = age;
            return this;
        }

        public Bulider setEmail(String email){
            this.email = email;
            return this;
        }

        public Bulider setPhone(String phone){
            this.phone = phone;
            return this;
        }

        public Student bulid(){
            Student student = new Student();
            student.name = this.name;
            student.age = this.age;
            student.email = this.email;
            student.phone = this.phone;
            return student;
        }
    }
    
    public static void main(String[] args) {
        Bulider bulider = new Bulider();
        bulider.setName("卤蛋").setAge(18).setEmail("3132744933@qq.com").setPhone("17802222322");
        Student bulid = bulider.bulid();
        System.out.println(bulid);
    }
}
发布了26 篇原创文章 · 获赞 1 · 访问量 346
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览