Cat.java:
package com.wys.javaoop; import javax.naming.NamingEnumeration; /** * 宠物猫类 * @author snape */ public class Cat { //成员属性: String name; //昵称 默认值为null int month; //年龄 默认值为0 double weight; //体重 默认值为0.0 String species; //品种 默认值为null //成员方法: public Cat() { } //带参构造方法 public Cat(String name, int month, double weight, String species){ this.name = name; this.month = month; this.weight = weight; this.species = species; } //跑动方法 public void run(){ System.out.println("猫跑动"); } //跑动方法,重载 public void run(String name){ System.out.println(name + "跑动"); } //吃东西方法 public void eat(){ System.out.println("猫吃东西"); } }
CatTest.java:
package com.wys.javaoop; import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; public class CatTest { public static void main(String[] args) { Cat one = new Cat("mimi", 2, 3000, "英国短毛猫"); System.out.println("名字:" + one.name); System.out.println("年龄:" + one.month); System.out.println("体重:" + one.weight); System.out.println("品种:" + one.species); } }
结果: