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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ringfingers 中级黑马   /  2015-10-4 16:41  /  128 人查看  /  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");
        }
}

1 个回复

正序浏览
学习学习
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马