对象的使用方法
使用对象调用变量和函数:
对象.变量 对象.函数()
例如创建一个Dog类并使用对象
//类文件Dogclass Dog{String name;//首字母要大写int age;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.color="白色";d.jump();System.out.println("名字是"+d.name);}}
生成多个对象:
Dog d1 = new Dog();Dog d2 = new Dog(); //两个对象是独立的
匿名对象的使用:
可以不定义对象的引用名称,而直接调用这个对象的方法。如:
new Dog().jump();
匿名对象通常是一次性的,用完了下次就找不着了。。
新闻热点
疑难解答