实例化对象
实例化对象就是一个创建对象的过程。
首先建立一个class为Cat的对象:
package com.imooc.animal;
public class Cat {
//属性:昵称,年龄,体重,品种
String name; //String类型默认值null
int month; //int类型默认值0
double weight; //double类型默认值0.0
String species;
//成员方法:跑动,吃东西
//跑动的方法
public void run() {
System.out.println("小猫快跑");
}
public void run(String name) {
System.out.println(name + "快跑");
}
//吃东西的方法
public void eat() {
System.out.println("小猫吃鱼");
}
}
再建立一个class为CatTest的对象:
package com.imooc.animal;
public class CatTest {
public static void main(String[] args) {
//对象实例化
Cat one = new Cat();
//测试
one.eat();
one.run();
one.name = "花花";
one.month = 2;
one.weight = 1000;
one.species = "英国短毛猫";
System.out.println(one.month);
//默认存在初始值,和在方法里定义的局部变量没有初始值不同
System.out.println("昵称:&#