Java 类 练习

Posted by zhangzexin on October 6, 2017
//
import java.util.*;
public class Cat{
    private String name;
    private String color;
    private int age;
    public void setColor(String color)
    {
        this.color=color;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
    public void Cat(String name,String color,int age)
    {
        this.name=name;
        this.color=color;
        this.age=age;
    }
    public Cat(String name) {
        super();
        this.name = name;
    }
    public static void main(String[] args){
        //System.out.println("");
        Cat white=new Cat("小白");
        Cat flower=new Cat("小花");
        white.setColor("白色");
        flower.setColor("花色");
        white.setAge(3);
        flower.setAge(100);
        Scanner in=new Scanner(System.in);
        String namet = in.nextLine();
        if(namet.equals("小白")==true){
            System.out.println(white.name+" 花色为"+white.color+" 年龄为"+white.age);           
        }
        else if(namet.equals("小花")==true){
            System.out.println(flower.name+" 花色为"+flower.color+" 年龄为"+flower.age);           
        }
        else System.out.println("没有这只小猫\n");
    }
}