Cats.java
public class Cats { /* @该类有4个变量,分别对变量进行封装,包括: @名字; @年龄; @种类; @食物: */ private String name; private int age; private String variety; private String food; public int getAge() { return age; } public void setAge(int age) { if (age < 0 || age >30) { System.out.println("0"); }else { this.age = age; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getVariety() { return variety; } public void setVariety(String variety) { this.variety = variety; } public String getFood() { return food; } public void setFood(String food) { this.food = food; } /* @该类中有3种行为,包括: @吃饭时间; @睡觉时间; @发狂时间; */ public void eat(){ System.out.println(this.name+"正在吃饭"); } public void sleep(){ System.out.println(this.name+"正在睡觉"); } public void crazy(){ System.out.println(this.name+"正在发狂"); } }
ApplicationRun.java
public class ApplicationRun {
public static void main(String[] args) {
Cats xiaoMing_family_pets = new Cats();
Cats xiaoZhang_family_pets = new Cats();
xiaoMing_family_pets.setName("Tom");
xiaoMing_family_pets.setAge(2);
xiaoMing_family_pets.setVariety("布偶");
xiaoMing_family_pets.setFood("猫粮和小鱼干");
xiaoZhang_family_pets.setName("Jerry");
xiaoZhang_family_pets.setAge(2);
xiaoZhang_family_pets.setVariety("狸花猫");
xiaoZhang_family_pets.setFood("老鼠和小鸟");
System.out.println("xiaoMing_family_pets.name = " + xiaoMing_family_pets.getName());
System.out.println("xiaoMing_family_pets.age = " + xiaoMing_family_pets.getAge());
System.out.println("xiaoMing_family_pets.variety = " + xiaoMing_family_pets.getVariety());
System.out.println("xiaoMing_family_pets.food = " + xiaoMing_family_pets.getFood());
System.out.println("xiaoZhang_family_pets.name = " + xiaoZhang_family_pets.getName());
System.out.println("xiaoZhang_family_pets.age = " + xiaoZhang_family_pets.getAge());
System.out.println("xiaoZhang_family_pets.variety = " + xiaoZhang_family_pets.getVariety());
System.out.println("xiaoZhang_family_pets.food = " + xiaoZhang_family_pets.getFood());
//记录吃饭时间
xiaoMing_family_pets.eat();
xiaoZhang_family_pets.eat();
//记录睡觉时间
xiaoMing_family_pets.sleep();
xiaoZhang_family_pets.sleep();
//记录发狂时间
xiaoMing_family_pets.crazy();
xiaoZhang_family_pets.crazy();
}
}