黑马程序员技术交流社区

标题: 终于弄懂,自己做的小程序 [打印本页]

作者: 时光无痕    时间: 2015-7-13 22:03
标题: 终于弄懂,自己做的小程序
/*
需求:4个类
父类Animal:名字和年龄变量,构造方法,输出名字年龄方法,运动方式、吃的食物方法
Dog类:继承Animal
Cat类:继承Animal
AnimalText类:测试
*/
class  AnimalText
{
public static void main(String[] args)
{
  //狗的介绍
  Dog d =new Dog();
  d.nameAge("狗",10);
  d.run();
  d.eat("骨头");
  d.show();
  //猫的介绍
  Cat c =new Cat();
  c.nameAge("猫",8);
  c.run();
  c.eat("鱼");
  c.show();
}
}
class Animal
{
private String name;
private int age;
String food;
//静态代码块,执行一次
static
{
  System.out.println("动物园的动物介绍:");
}
//构造函数
public Animal()
{}
public void nameAge(String name,int age)
{
  this.name=name;
  this.age=age;
  System.out.println("动物的名字是:"+name);
  System.out.println(name+"的年龄是:"+age);
}
public void run()
{
  System.out.println("运动方式:跑步");
}
public void eat(String food)
{
}
//final修饰方法
public final void show()
{
  System.out.println("------");
}
}
//Dog继承Animal类
class Dog extends Animal
{
//传递参数,调用父类中的方法
public void nameAge(String name,int age)
{
  super.nameAge(name,age);
}
//调用父类中的无参方法
public void run()
{
  super.run();   
}
//重写父类中的方法
public void eat(String food)
{
  System.out.println("最喜欢吃的是:"+food);
}
}
class Cat extends Animal
{
public void nameAge(String name,int age)
{
  super.nameAge(name,age);  
}
public void run()
{
  super.run();
}
public void eat(String food)
{
  System.out.println("一般喜欢吃的是:"+food);
}
}

作者: 惜爱上缘    时间: 2015-7-13 22:19
今天的作业小程序,我也做了
作者: 时光无痕    时间: 2015-7-13 22:27
恭喜, 继续加油
作者: 童杭杰    时间: 2015-7-13 22:31

恭喜, 继续加油
作者: 噜啦啦噜啦啦    时间: 2015-7-13 22:36
继续加油  加油
作者: Queen_one    时间: 2015-7-13 22:37
加油加油↖(^ω^)↗可是 为什么程序那么熟 (⊙o⊙)哦 我们今天作业也有
作者: 何鹏程    时间: 2015-7-13 22:41
继续努力,加油啊。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2