//????Gun??
public abstract class Gun {
protected String name;
// ?????????????????????????????????????????
public Gun() {
public Gun(String name) {
this.name = name;
// ????????????????????м?к???塣
abstract void sound();
public String getName() {
return name;
public void setName(String name) {
this.name = name;
//Carbine?????Gun
public class Carbine extends Gun {
//?????????????????????????????????????????
public Carbine(String name) {
this.name = name;
//Gun???2????????????????sound()???????????????????????sound()??????????????Gun???????????????????
//???????Java???????
void sound() http://www.huiyi8.com/jiaoben/
// TODO Auto-generated method stub
System.out.println("DaDaDa!"); javascript
//Rifle?????Gun
public class Rifle extends Gun {
//???????????????????????????????????????????
public Rifle(String name) {
this.name = name;
//Gun???2????????????????sound()???????????????????????sound()??????????????Gun???????????????????
//???????Java???????
void sound() {
// TODO Auto-generated method stub
System.out.println("Peng! Peng! Peng!");
//????Person??
public class Person {
private String name;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
// ?????????
public void shoot(Person p?? Gun g) {
System.out.println(this.name + " shoot " + p.getName() + " using a "
+ g.getName());
g.sound();
//??main()?????в???
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p1 = new Person();
Person p2 = new Person();
p1.setName("XiaoWang");
p2.setName("XiaoLi");
Rifle r = new Rifle("rifle");
Carbine c = new Carbine("carbine");
// ??????????ú???????????????Ч????
p1.shoot(p2?? r);
p1.shoot(p2?? c);