java练习题 创建猫类

import java.awt.Color;

public class lianxi{

private String name; //猫的名字

private int age; //猫的年龄

private double weight; //猫的重量

private Color color; //猫的颜色

public lianxi(String name,int age,double weight,Color color) {

this.name=name;

this.age=age;

this.weight=weight;

this.color=color;

}

public String toString() //重写toString()方法

{

StringBuilder s=new StringBuilder();

s.append("名字:"+name+"\n");

s.append("年龄:"+age+"\n");

s.append("重量:"+weight+"\n");

s.append("颜色:"+color+"\n");

return s.toString();

}

public static void main(String[] args) {

lianxi cat1=new lianxi("java",12,21,Color.BLACK);

lianxi cat2=new lianxi("c++",12,21,Color.WHITE);

lianxi cat3=new lianxi("java",12,21,Color.BLACK);

System.out.println("猫1号:"+cat1);

System.out.println("猫2号:"+cat2);

System.out.println("猫3号:"+cat3);

}

}

你可能感兴趣的:(java,servlet,jvm)