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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© mingjing56 中级黑马   /  2015-4-9 22:33  /  537 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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+"岁");
        }
}

3 个回复

倒序浏览
使用private私有,隐藏对象的属性和实现细节,仅对外提供公共访问方式
回复 使用道具 举报
谢谢,提醒
回复 使用道具 举报
邓士林 来自手机 高级黑马 2015-4-10 22:32:41
板凳
顶一个,面相对象才入门
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马