现在的位置: 首页 > 综合 > 正文

面向对象基础三 14集

2014年09月05日 ⁄ 综合 ⁄ 共 965字 ⁄ 字号 评论关闭

1、对象的使用方法;

     使用对象调用变量和函数;

     1、对象.变量

      2、对象.函数()例如:小明15岁,而不能直接说15岁,所以函数都 要有具体的对象。

class Dog{
     int age;
     String name;
     String color;
   void jump(){
        System.out.println("jump");
}
}

class Test{

          public static void main(String args[]){

                       Dog d = new Dog();

                       d.name = "旺财";

                       d.age = 2;

                       d.jump();

                       System.out.println("名字是" + d.name);

}

}

2、多对象的创建方法;

生成多个对象

Dog d1 = new Dog();

Dog d2 = new Dog();

class Test{

          public static void main(String args[]){

                       Dog d1 = new Dog();

                       Dog d2 = new Dog();

                       d1.name = "旺财";

                       d2.name = "四喜";

               

}

}

 

3、匿名对象的创建和使用方法;

匿名对象的使用

可以不定义对象的引用名称,而直接调用这个对象的方法。这样的对象叫做匿名对象,例如:

                                  new Dog().jump();

 

 

 

class Test{

          public static void main(String args[]){

                      new Dog().jump();

                      new Dog().jump();

匿名对象只能被调用一次。

                    

               

}

}

 

抱歉!评论已关闭.