单例模式饿汉式

    xiaoxiao2025-03-03  6

    1.User类;

    public class User { //给定一个对象最终的; public static final User user=new User(); private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public static User getUser() { return user; }

    }

    2.TestUser类;

    public class TestUser { public static void main(String[] args) { User u1=User.getUser(); User u2=User.getUser(); u1.setName("tom"); u2.setName("link"); System.out.println(u1.getName()); System.out.println(u2.getName()); } }

    3.程序运行如下:

    link link

    转载请注明原文地址: https://ju.6miu.com/read-1296837.html
    最新回复(0)