创建猫类
创建Cat类,类中包含表示名字的属性name、表示年龄的属性age、表示重量的属性weight和表示颜色的属性color。重写toString()方法。
package haha;
public class Cat{
String name;
int age;
double weight;
String color;
static int id=0;
public Cat(String name,int age,double weight,String color) {
this.name=name;
this.age=age;
this.weight=weight;
this.color=color;
id++;
}
public String toString() {
return "猫咪"+id+"号:名字:"+name+"\n"+"年龄:"+age+"\n"+"重量:"+weight+"\n"+"颜色:"+color+"\n";
}
public static void main(String[] args) {
Cat s1=new Cat("Java",12,21,"java.awt.Color[r=0,g=0,b=0]");
System.out.println(s1);
Cat s2=new Cat("C++",12,21,"java.awt.Color[r=255,g=255,b=255]");
System.out.println(s2);
Cat s3=new Cat("Java",12,21,"java.awt.Color[r=0,g=0,b=0]");
System.out.println(s3);
}
}