java类的继承,接口,抽象类总结

1. 类的继承
类和类之间可以实现继承关系,即从一个类中继承它的部分属性和部分方法,避免重写代码,提高了代码的重用性。比如武侠小说,言情小说,玄幻小说都属于小说,拥有小说所拥有的属性。继承的格式是public class A extends B,即A继承自B,A称为子类,B称为父类。
子类能用父类的那些方法呢?如果子类和父类同一个文件夹下,则除私有的属性和方法外,其他都能被调用;如果子类父类不同文件夹,但对象在子类外去调用方法,只有公有的方法和属性能被调用;如果不同文件夹下,但是在子类中调用,则公有的和受保护的都能被调用。
2. 方法的重写
如果子类和父类中的方法名字,访问修饰符,返回值和参数值完全相同,则可进行方法重写。方法重写后在子类建立对象之后会优先调用子类上写的方法。
3. 自动转型
实例化对象时候可以这么写
父类名 对象名 = new 子类名();
即父类自动转型为子类;
如果方法在父类里面找不到,则会编译错误。
第一步,public 返回值类型 方法名(父类 父类名)
第二步, 父类 类名=new 子类名(参数。。。);
或者 子类 类名=new 子类名(参数);
第三步,方法名 .方法(子类);
4. interface 接口
接口与类相似,只包含常量和抽象方法。目的是指明多个对象的共同方法。
Public interface 接口名
{
Public static final 数据结构 常量名= 常量;//static final。。。。
Public abstract 返回值类型 方法名(参数…..);//abstract
}
接口中默认的修饰符是public
接口可以继承接口,且可以继承多个接口
接口内的方法只能在类中具体实现
Public class 类名 implements 接口,。。。。。。{
接口中所有的抽象方法必须能得到实现
}
5. 抽象类
Public abstract 抽象类名
{
能定义类中所有的属性,方法;
能定义接口中所有的抽象方法,常量;但定义抽象方法时候必须加abstract。
}
抽象类,接口,类只有类可以实例化对象。

练习实例:角色扮演类战斗,怪物和英雄都是Role的子类
Role class :

public class Role {
private String name;
private int blood,attack,defense;
private int speed;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setBlood(int blood)
{
this.blood=blood;
}
public void setAttack(int attack)
{
this.attack=attack;
}
public void setDefense(int defense)
{
this.defense=defense;
}
public void setSpeed(int speed)
{
this.speed=speed;
}
public int getBlood()
{
return blood;
}
public int getAttack()
{
return attack;
}
public int getDefense()
{
return defense;
}
public int getSpeed()
{
return speed;
}
}


Monster class :
public class Monster extends Role
{
public void NormalAttack(Hero hero)
{
int a,b;
a=hero.getBlood()-(int)(0.8*getAttack()+hero.getDefense());
b=(int)(0.8*getAttack()-hero.getDefense());
hero.setBlood(a);
System.out.println(getName()+"攻击了"+hero.getName()+",使之掉了"+b+"血量");
System.out.println("英雄血量剩余"+hero.getBlood());
}
public void Vampire(Hero hero)
{
int a,b,c;
a=hero.getBlood()-(int)(0.8*getAttack())+hero.getDefense();
c=(int)(0.8*getAttack())-hero.getDefense();
hero.setBlood(a);
b=(int)(c*0.8);
setBlood(getBlood()+b);
System.out.println(getName()+"吸取了"+b+"血量,对"+hero.getName()+"造成"+c+"伤害");
System.out.println("英雄血量剩余"+hero.getBlood()+",怪物血量剩余"+getBlood());
}
}


Hero class:
public class Hero extends Role
{
public void Whirlwind(Monster monster)
{
int a,b;
a=monster.getBlood()-(int)(1.4*getAttack())+getDefense();
monster.setBlood(a);
b=(int)(1.4*getAttack())-getDefense();
System.out.println(getName()+"使用旋风斩攻击"+monster.getName()+"使之掉血"+b);
System.out.println("怪物血量剩余"+monster.getBlood());
}
public void Recovery()
{

setBlood(getBlood()+150);
System.out.println(getName()+"使用圣光术,恢复了150生命值");
System.out.println("英雄血量剩余"+getBlood());
}
public void NormalAttack(Monster monster)
{

int a,b;
a=monster.getBlood()-(int)(0.8*getAttack()+monster.getDefense());
b=(int)(0.8*getAttack()-monster.getDefense());
monster.setBlood(a);
System.out.println(getName()+"使用普通攻击,"+monster.getName()+"损失"+b+"血量.");
System.out.println("怪物血量剩余"+monster.getBlood());
}
}



Fight class:
import java.util.*;
public class Fight {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Hero hero=new Hero();
Monster monster=new Monster();
hero.setName("英雄");
monster.setName("怪物");
hero.setBlood(3000);
monster.setBlood(2800);
hero.setAttack(500);
monster.setAttack(450);
hero.setDefense(150);
monster.setDefense(100);
hero.setSpeed(40);
monster.setSpeed(35);
Random ran1=new Random();
Random ran2=new Random();
//System.out.println("请输入你想扮演的角色,1英雄,2怪物");
//Scanner num=new Scanner(System.in);
//int choose=num.nextInt();
int i;
//if(choose==1)
//{
for(i=1;i>=0;i++)
{
System.out.println(" fight "+i);
//System.out.println("您的角色有以下技能:1.旋风斩 2圣光术 3普通攻击");
if(hero.getSpeed()>monster.getSpeed())
{
int value1=ran1.nextInt(3);
int value2=ran2.nextInt(2);
switch(value1)
{
case 0:
hero.Whirlwind(monster);break;
case 1:
hero.Recovery();break;
case 2:
hero.NormalAttack(monster);break;
}
if(hero.getBlood()<=0 || monster.getBlood()<=0)
break;
switch(value2)
{
case 0:
monster.Vampire(hero);break;
case 1:
monster.NormalAttack(hero);break;

}
if(hero.getBlood()<=0 || monster.getBlood()<=0)
break;
}
}
if(hero.getBlood()>monster.getBlood())
System.out.println("英雄获胜");
else
System.out.println("怪物获胜");

//}



}

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值