练习题
1,创建一个动物的类 动物类,需要有属性,颜色,名字,品种,体重 动物类,需要有行为,吃,跑,叫。 新创建一个测试的类,在main方法里面,通过上面创建的动物类,创建三个对象,猫,鸡,鸟。
2. 创建一个12个长度的String类型的数组,并且用for循环 把数组里面的元素打印到控制台
第二题
第二题比较容易先说第二题
要点为:用for循环顺序遍历数组并输出;
for(int i=0;i<j.length;i++) {//j表示创建的String【】数组的名称。。没写出完整代码
System.out.println(j[i]);
}
第一题
要求很清楚了,类 属性 方法都已经详细说明
直接上代码
public class Animal2 {
String color;
String name;
String variety;
int weight;
void eat(String food) {
System.out.println("喂食食物为:"+food);
}
void run(int speed) {
System.out.println("跑步速度为"+speed+"km/h");
}
void yell(String shout) {
System.out.println("叫声特点:"+shout+"\n");
}
public static void main(String[] args) {
Animal2 cat=new Animal2();
System.out.println(cat.name="小柒");
System.out.println(cat.color="白色");
System.out.println(cat.variety="猫");
System.out.println(cat.weight=6);
cat.eat("三文鱼");
cat.run(10);
cat.yell("喵喵喵");
Animal2 hen=new Animal2();
System.out.println(hen.name="小六");
System.out.println(hen.color="棕黄");
System.out.println(hen.variety="鸡");
System.out.println(hen.weight=3);
hen.eat("玉米");
hen.run(5);
hen.yell("嗷嗷嗷");
Animal2 bird=new Animal2();
System.out.println(bird.name="小八");
System.out.println(bird.color="黑白");
System.out.println(bird.variety="鸟");
System.out.println(bird.weight=1);
bird.eat("虫子");
bird.run(8);
bird.yell("叽叽喳喳");
}
}
运行结果如下图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200103154200132.png)
**问题来了:我觉得属性的说明不够详细,打出来不好看**
于是尝试着改
public class Animal {
String color;
String name;
String variety;
int weight;
void eat(String food) {
System.out.println("喂食食物为:"+food);
}
void run(int speed) {
System.out.println("跑步速度为"+speed+"km/h");
}
void yell(String shout) {
System.out.println("叫声特点:"+shout+"\n");
}
public static void main(String[] args) {
String name1="小柒";
String color1="白色";
String variety1="猫";
int weight1=6;
System.out.println("昵称:"+name1);
System.out.println("毛色:"+color1);
System.out.println("种类:"+variety1);
System.out.println("体重:"+weight1+"kg");
Animal cat=new Animal();
cat.eat("三文鱼");
cat.run(10);
cat.yell("喵喵喵");
String name2="小六";
String color2="棕黄";
String variety2="鸡";
int weight2=3;
System.out.println("昵称:"+name2);
System.out.println("毛色:"+color2);
System.out.println("种类:"+variety2);
System.out.println("体重:"+weight2+"kg");
Animal hen=new Animal();
hen.eat("玉米");
hen.run(5);
hen.yell("嗷嗷嗷");
String name3="小八";
String color3="黑白";
String variety3="鸟";
int weight3=1;
System.out.println("昵称:"+name3);
System.out.println("毛色:"+color3);
System.out.println("种类:"+variety3);
System.out.println("体重:"+weight3+"kg");
Animal bird=new Animal();
bird.eat("虫子");
bird.run(8);
bird.yell("叽叽喳喳");
}
}
```这种种感觉好蠢,反复声明变量 虽然打出来蛮好看的
然后第三种就不按要求来了,直接把属性也写成方法
public class Animal3 {
void eat(String food) {
System.out.println("喂食食物为:"+food);
}
void run(int speed) {
System.out.println("跑步速度为"+speed+"km/h");
}
void yell(String shout) {
System.out.println("叫声特点:"+shout+"\n");
}
void getName(String name) {
System.out.println("昵称:"+name);
}
void getColor(String color) {
System.out.println("毛色:"+color);
}
void getVariety(String variety) {
System.out.println("种类:"+variety);
}
void getWeight(int weight) {
System.out.println("体重为:"+weight+"kg");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal3 cat=new Animal3();
cat.getName("小柒");
cat.getColor("白色");
cat.getVariety("猫");
cat.getWeight(8);
cat.eat("三文鱼");
cat.run(10);
cat.yell("喵喵喵");
Animal3 hen=new Animal3();
hen.getName("小六");
hen.getColor("棕黄");
hen.getVariety("鸡");
hen.getWeight(6);
hen.eat("玉米");
hen.run(5);
hen.yell("嗷嗷嗷");
Animal3 bird=new Animal3();
bird.getName("小八");
bird.getColor("黑白");
bird.getVariety("鸟");
bird.getWeight(1);
bird.eat("虫子");
bird.run(8);
bird.yell("叽叽喳喳");
}
}