黑马程序员技术交流社区

标题: 创建对象 [打印本页]

作者: mingjing56    时间: 2015-4-9 22:33
标题: 创建对象
class StudentDemo
{
        public static void main(String[] args)
        {
                Student stu = new Student();
                stu.name ="李芳";
                stu.setAge(-20);
                stu.getAge();
                stu.introduce ();
        }
}
class Student
{
        private int age ;
        String name ;
        public void setAge(int stuage)
        {
                if (stuage<0)
                {
                        System.out.println("年龄不合法");
                }
                else
                        age=stuage;
        }
        public int getAge()
        {
                return age;
        }
        void introduce()
       {
                System.out.println("她的名字是"+name+",今年"+age+"岁");
        }
}


作者: 黑马杜浔    时间: 2015-4-9 23:27
使用private私有,隐藏对象的属性和实现细节,仅对外提供公共访问方式
作者: mingjing56    时间: 2015-4-10 22:22
谢谢,提醒
作者: 邓士林    时间: 2015-4-10 22:32
顶一个,面相对象才入门




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2