Java 狗狗类定义及测试 - 属性、方法和对象创建
// \u005c"定义狗狗类\u005c" public class Dog { // \u005c"属性\u005c" private String gender; private String name;
// \u005c"构造方法\u005c"
public Dog(String name, String gender) {
this.name = name;
this.gender = gender;
}
// \u005c"方法\u005c"
public void eat(String food) {
System.out.println(name + \u005c"正在吃\u005c" + food);
}
}
// \u005c"测试类\u005c" public class DogTest { public static void main(String[] args) { // \u005c"创建狗狗对象并调用吃方法\u005c" Dog dog1 = new Dog(\u005c"母狗1号\u005c", \u005c"母\u005c"); dog1.eat(\u005c"包子\u005c");
Dog dog2 = new Dog(\u005c"公狗1号\u005c", \u005c"公\u005c");
dog2.eat(\u005c"大馒头\u005c");
}
}
原文地址: https://www.cveoy.top/t/topic/pLjQ 著作权归作者所有。请勿转载和采集!