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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李振在黑马 中级黑马   /  2015-9-19 20:17  /  472 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

函数的封装
//类的封装
class Demo {
        public static void main(String[] args) {
                //System.out.println("Hello World!");
                Person p1=new Person();
                p1.setAge(-24);
                p1.name="lifang";
            p1.speak();
                Person p2=new Person();
                p2.name="zhangsan";
                p2.speak();
               

        }
}
        class Person {
                private        int age;
                 String name;
         public int getAge(){
                    return age;
                }
            public void setAge(int a) {
                  if(a>0&&a<130){
                      age=a;
                }
                else
                        System.out.println("你输入的年龄不合法");
               }
               

                       
                void speak(){
                System.out.println("大家好,我叫"+name+"我今年"+age+"岁");
                }
        }

构造函数
构造函数,对象一建立,构造函数就开始运行(被调用)


        class Demo_1 {
        public static void main(String[] args) {
                Person p=new Person();

        }
}
class Person {
        public Person () {
                System.out.println("Hello");
        }

}
//这个是构造函数有参数
//构造函数(方法)对象一建立就运行
class Demo_1 {
        public static void main(String[] args) {
            Person p=new Person(21);
                p.speak();
        }
}
class Person {
        int age;
        public Person(int a){
                age=a;
        }
        void speak(){
        System.out.println("我今年"+age);
        }
}

构造函数的重载

class Demo_2 {
        public static void main(String[] args) {
                Person p1=new Person(20,"zhangsan");
                Person p2=new Person(23);
                p1.speak();
                p2.speak();
        }
}
class Person {
        int age;
        String name;
        public Person(int a,String abc){
                age=a;
                name=abc;
               
        }
        public Person(int a){
        age=a;
        }
        void speak(){
            System.out.println(name+","+age);
        }
}

垃圾回收的程序

class Person {
        void speak(){
        System.out.println("垃圾回收前要处理一次");

        }
}
class Demo_3 {
        public static void main(String[] args) {
                Person p1=new Person();
                Person p2=new Person();
                p1=null;
                p2=null;
                System.gc();
                for(int i=0;i<100000;i--) {
                }
        }
}

静态static的使用
Static 只能修饰成员变量不可以修饰局部变量.


class Demo_4 {
        public static void main(String[] args) {
                Student s1=new Student();
                Student s2=new Student();
                Student.school="传智播客";
                System.out.println("我的学校是"+s1.school);
         System.out.println("我的学校是"+s2.school);

        }
}
class Student {
        static String school;
}


内部类
外部类调用
class Demo_6 {
        public static void main(String[] args) {
                Outer outer=new Outer();
                outer.test();
        }
}
class Outer {
        //void text(){
            int num=2;
            void test(){
                Itnner itnner=new Itnner();
                itnner.show();

            }
        //}
        class Itnner {
                void show(){
                System.out.println("num="+num);
                }
        }
}

内部类调用       


/*class Demo_6 {
        public static void main(String[] args) {
                Outer outer=new Outer();
                outer.test();
        }
}*/
class Outer {
            int num=2;
            void test(){
                Itnner itnner=new Itnner();
                itnner.show();

            }
       
class Itnner {
                void show(){
                System.out.println("num="+num);
                }
        }
}
class Demo_6 {
        public static void main(String[] args){
                Outer.Itnner itnner=new Outer().new Itnner();
                itnner.show();
        }
}

发资料;了.预习了这部分,敲敲代码.
真是听懂不一定真懂.这几个小程序弄了一下午还没实现,到了晚上了都.

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马