#设计猫类,属性有种类|、颜色、年龄和体重,方法有输出猫的信息、猫发出叫声、猫玩东西
package demo;
class Cat {
private String species; // 种类
private String color; // 颜色
private int age; // 年龄
private double weight; // 体重
// 构造方法
public Cat(String species, String color, int age, double weight) {
this.species = species;
this.color = color;
this.age = age;
this.weight = weight;
}
// 输出猫的信息
public void printCatInfo() {
System.out.println("种类: " + species);
System.out.println("颜色: " + color);
System.out.println("年龄: " + age + "岁");
System.out.println("体重: " + weight + "公斤");
}
// 猫发出叫声
public void meow() {
System.out.println("喵喵喵~");
}
// 猫玩东西
public void playWithToy() {
System.out.println("猫咪正在玩东西");
}
// getter 和 setter 方法
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
public class Main{
public static void main(String[] args) {
// 创建一个Cat对象
Cat cat = new Cat("家猫", "白色", 3, 3.5);
// 输出猫的信息
cat.printCatInfo();
// 猫发出叫声
cat.meow();
// 猫玩东西
cat.playWithToy();
}
}
标签:String,weight,color,age,无标题,species,public
From: https://blog.csdn.net/dl20050314/article/details/136995277