构造方法是练习

 
(一)、创建一个武器类Weapen
定义类中属性:攻击力Power,速度speed,并实现构造方法
创建一个Tank子类从Weapen类继承
具有私有的方向字段dir(上下左右),并用属性封装
定义攻击方法Attack(),打印“我是坦克,向**方向运动,速度***,攻击力***”
创建一个子弹类Bullen从Weapen类继承
具有私有的type字段(表示子弹类型,如:机枪子弹,步枪子弹),用属性封装
定义攻击方法Attack(),打印”我是子弹***,速度***,攻击力***”
为Tank类和Bullen类定义构造函数初始化属性(要求使用super调用父类的构造函数)
创建一只Tank对象,调用其方法;创建一只Bullen,调用其方法

public class Weapen {
 
 double Power;
 double speed;
 public Weapen() {
 
  // TODO Auto-generated constructor stub
 }
}
public class Tank extends Weapen {
 private String dir;
 public Tank() {
  super();
  super.Power=100.32;
  super.speed=10000;
  this.dir="上";
  // TODO Auto-generated constructor stub
 }
 
 public String getDir() {
  return dir;
 }
 public void setDir(String dir) {
  this.dir = dir;
 }
 public void  Attack(){
  System.out.println("我是坦克,向"+getDir()+"方向运动,速度"+this.speed+",攻击力"+super.Power);
 }
}
package weaoan;
public class Bullen extends Weapen{
 public Bullen() {
  super();
  super.Power=100.2;
  super.speed=2222;
  this.type="机枪子弹";
  // TODO Auto-generated constructor stub
 }
 private String type;
 public String getType() {
  return type;
 }
 public void setType(String type) {
  this.type = type;
 }
 public void Attack(){
  System.out.println("我是"+getType()+", 速度"+this.speed+",攻击力"+super.Power);
  }
}
package weaoan;
public class Text {
public static void main(String[] args) {
 Tank tk=new Tank();
 tk.Attack();
 Bullen Bl=new Bullen();
    Bl.Attack();
}
}
(二)、请编码实现动物世界的继承关系:
动物(Animal)具有行为:吃(eat)、睡觉(sleep)
动物包括:兔子(Rabbit),老虎(Tiger)
这些动物吃的行为各不相同(兔子吃草,老虎吃肉);但睡觉的行为是一致的
package animals;
public class Animal {
 String eat;
 String sleep;
 
}
package animals;
public class Rabbit extends Animal {
 String eat="兔子吃草";
 public Rabbit() {
  super.sleep="睡觉";
  System.out.println(eat+"兔子在"+sleep);
 }
 
}
package animals;
public class Tiger extends Animal {
 public Tiger() {
  super();
  // TODO Auto-generated constructor stub
  this.eat="吃肉";
  super.sleep="睡觉";
  System.out.println("老虎"+eat+",老虎在"+sleep);
 }
}
package animals;
public class Text {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
   Rabbit Ab=new Rabbit();
   Tiger Tg=new Tiger();
 }
}
(三)、创建一动物类:Animal
1) 创建三个子类:Dog、Cat、Pig都继承于Animal
2) 为Dog添加love(),打印我喜欢吃骨头
3) 为Cat添加love(),打印我喜欢吃小鱼
4) 为Pig添加love(),打印我喜欢吃饲料
5) 请创建一个长度为3的动物数组Animal[] animals = new Animal[3];
6) 分别创建一个狗、猫、猪对象,并将三个对象加入至动物数组内
7) 请采用循环调用动物数组内的每一个动物的love()方法
package Animal;
public class Animal {
 //定义动物的共有功能
 public void Love() {
  System.out.println("我喜欢吃东西");
  }
}
package Animal;
public class Dog   extends Animal{
 //为Dog添加Love()功能,
 public void Love() {
  System.out.println("我喜欢吃骨头");
  }
}
package Animal;
public class Cat extends Animal {
 //为Cat添加Love()功能,
 public void Love() {
  System.out.println("我喜欢吃小鱼");
  }
}
package Animal;
public class Pig extends Animal {
 ////为pig添加Love()功能,
 public void Love() {
  System.out.println("我喜欢吃饲料");
  }
}
package Animal;
public class Text {
public static void main(String[] args) {
 Animal []An=new Animal[3]; 
 Manage Mn=new Manage();
    Dog dog=new Dog();
    An[0]=dog;
    Cat cat=new Cat();
    An[1]=cat;
    Pig pig =new Pig();
    An[2]=pig;
    for (int i = 0; i < An.length; i++) {
  Mn.show(An[i]);
 }
}
}
package Animal;
public class Manage {
 //用含参的构造方法实现动物的功能
 public void show(Animal animal){
  animal.Love();
 }
}
原文地址:https://www.cnblogs.com/www-hsy-com/p/8093422.html