package zxs; public class Demo3 { public static void main(String[] args) { Cat cat1=new Cat(); cat1.setAge(2); cat1.setBrand("niunaimao"); cat1.setColor("黑白色"); cat1.setGender(true); cat1.setWeight(10); System.out.println("brand="+cat1.getBrand()+",age="+cat1.getAge()+",olor="+cat1.getColor()+",gender="+cat1.isGender() +",weight="+cat1.getWeight()); if(cat1.isGender()){ System.out.println("这个猫是母猫"); }else{ System.out.println("这个猫是公猫"); } System.out.println("--------------"); Cat cat2 =new Cat(age:5,brand:"英短",color:"蓝色",weight:15,gender:true,weight:9.9); System.out.println("brand="+cat2.getBrand()+",age="+cat2.getAge()+",olor="+cat2.getColor()+",gender="+cat2.isGender() +",weight="+cat2.getWeight()); if(cat2.isGender()){ System.out.println("这个猫是母猫"); }else{ System.out.println("这个猫是公猫"); } } }
package zxs; public class Cat { private int age; private String brand; private String color; private double weight; private boolean gender; public Cat(int age, String brand, String color, double weight, boolean gender) { this.age = age; this.brand = brand; this.color = color; this.weight = weight; this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public boolean isGender() { return gender; } public void setGender(boolean gender) { this.gender = gender; } }