java定义一个动物类,用构造方法实现动物的实例化
public class Animal{ int height;//身高 int weight;//体重 int age;//年龄 String sex;//性别 public Animal(int height,int weight,int age,String sex){//带4个参数的构造方法 this.height = height; this.weight= weight; this.age= age; this.sex= sex;
}public static void main(String[] args) { Animal animal = new Animal(100,60,2,"雄性")
;/*animal这个对象就具有height、weight、age、sex属性值了*/}}