A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 嘉嘉 中级黑马   /  2015-10-1 09:15  /  295 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Student {
        // 把构造方法私有,是为了不让外界随意创建
        private Student() {
        }

        // 类本身要创建一个对象。
        // 由于静态只能访问静态,所以这里加静态
        //为了不让外界通过类名直接访问s成员变量,就加私有
        private static Student s = new Student();

        // 提供公共的方式让别人使用
        // 为了让外界能够直接通过类名访问该方法,需要对该方法加静态
        public static Student getStudent() {
                return s;
        }



public class StudentTest {
        public static void main(String[] args) {
                Student s1 = Student.getStudent();
                Student s2 = Student.getStudent();
                System.out.println(s1 == s2);// true

                s1.show();
                s2.show();

               
        }
}

        public void show() {
                System.out.println("hello");
        }
}

评分

参与人数 3黑马币 +28 收起 理由
蚊子先生 + 10 很给力!
xk513718201 + 8 赞一个!
大家伙 + 10 很给力!

查看全部评分

1 个回复

倒序浏览
楼主这分得的。。。。。。有好兄弟啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马