我不知道如何处理这个 java.lang.ClassCastException。

huangapple 未分类评论45阅读模式
标题翻译

I don't know how to deal with this java.lang.ClassCastException

问题

以下是翻译好的部分:

Personaje

public abstract class Personaje {
    //attributes
    int vida;
    //constructor
    Personaje() {
        vida = 100;
    }
    //getters and setters
    public int getVida() {
        return vida;
    }
    public void setVida(int vida) {
        this.vida = vida;
    }

    //abstract method movimientoLucha
    public abstract int movimientoLucha();
}

Mago

public class Mago extends Personaje {
    //attributes
    int magia;
    //Constructor of superclass
    Mago() {
        this.vida = vida + (int)(vida * 0.1);
        Date fecha_creacion = new Date();
        GregorianCalendar calendario_creacion = new GregorianCalendar();
        calendario_creacion.setTime(fecha_creacion);
        System.out.println("La fecha de creacion del Mago Test1 es: "
            + calendario_creacion.get(Calendar.DAY_OF_MONTH) + "/"
            + calendario_creacion.get(Calendar.MONTH) + "/"
            + calendario_creacion.get(Calendar.YEAR) + " a las "
            + calendario_creacion.get(Calendar.HOUR) + ":"
            + calendario_creacion.get(Calendar.MINUTE) + ":"
            + calendario_creacion.get(Calendar.SECOND) + ":"
            + calendario_creacion.get(Calendar.MILLISECOND));
    }
    //Getters & setters
    public int getMagia() {
        return magia;
    }
    public void setMagia(int magia) {
        this.magia = magia;
    }
    //Method
    public int movimientoLucha() {
        int movement = (int)(Math.random() * magia);
        System.out.println("****Hechizazo****");
        return movement;
    }
    public void setVida(int vida) {
        super.setVida(vida);
    }
    public int getVida() {
        return vida;
    }
}

(以下类的翻译类似,不再重复翻译)

Guerrero

GuerreroFuerte

GuerreroNormal

Arena

public class Arena {
    //Creation of method Lucha 

    //Creation of method Lucha first round

    //Creation of method Lucha second round 

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner Entrada = new Scanner(System.in);
        //Inizialization of variables 
        int vida = 200;
        int ataque = 800;
        //7.Creation of two variables danoValor & wrapper danoReferencia
        int danoValor = 0;
        Integer danoReferencia = 0;
        //Creation of a warrior 
        final Guerrero Test1 = new Guerrero();
        System.out.println("El nivel de vida del guerrero es " + Test1.getVida());
        System.out.println(Test1.movimientoLucha());
        //Creation of a Magician
        final Mago Test2 = new Mago();
        System.out.println("El nivel de vida del mago es " + Test2.getVida());
        System.out.println(Test2.movimientoLucha());
        //Call the method
        Lucha(Test1, Test2, danoValor, danoReferencia);
        //Creation of a new Guerrero Fuerte
        GuerreroFuerte BigTest1 = new GuerreroFuerte();
        Lucha(BigTest1, Test2, danoValor, danoReferencia);
        GuerreroNormal Prueba2 = new GuerreroNormal();
        Prueba2 = (GuerreroNormal)BigTest1;
        Lucha(Prueba2, Test2, danoValor, danoReferencia);
    }
}

请注意,由于篇幅较长,我只提供了部分的翻译内容。如果你有其他部分需要翻译,请继续提供。

英文翻译

I have to do the following :

The program is divided in the following classes:12. In our Arena class we have first defined a Fight (first round) between the Magician and the Strong Warrior. When this is over, we must create a normal Warrior from our Strong Warrior and he must fight against the magician (second round).

It is important that we observe that having cast, our Warrior takes the life of the Strong Warrior, but his attacks are simple, without the extra points that Strong Warrior has. For practical purposes it is as if our Warrior had lost his strength for the second round.

Personaje

public abstract class Personaje {
	//attributes
	int vida ;
	//constructor
	Personaje() {
		vida=100;
	}
	//getters y setters
	public int getVida() {
		return vida;
	}
	public void setVida(int vida) {
		this.vida = vida;
	}

	//abstract method movimientoLucha
	public abstract int movimientoLucha();
}

Mago

public  class Mago extends Personaje {
	//attributes
	int magia;
	//Constructor of superclass
	Mago() {

	this.vida=vida+(int)(vida*0.1);
	Date fecha_creacion =new Date();
	GregorianCalendar calendario_creacion=new GregorianCalendar();
	calendario_creacion.setTime(fecha_creacion);
	System.out.println("La fecha de creacion del Mago Test1 es: "
						+calendario_creacion.get(Calendar.DAY_OF_MONTH)+"/"
						+calendario_creacion.get(Calendar.MONTH)+"/"
						+calendario_creacion.get(Calendar.YEAR)+" a las "
						+calendario_creacion.get(Calendar.HOUR)+":"
						+calendario_creacion.get(Calendar.MINUTE)+":"
						+calendario_creacion.get(Calendar.SECOND)+":"
						+calendario_creacion.get(Calendar.MILLISECOND));

	}
	//Getters & setters
	public int getMagia() {
		return magia;
	}
	public void setMagia(int magia) {
		this.magia = magia;
	}
	//Method
	public int movimientoLucha(){
		int movement=(int)(Math.random()*magia);
		System.out.println("****Hechizazo****");
		return movement;
	}
	public void setVida(int vida) {
		super.setVida(vida);
	}
	public int getVida() {
		return vida;
	}
}

Guerrero

package p1_t5;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.text.DateFormat;

public class Guerrero extends Personaje {
	//attributes
	int ataque=0;
	//constructor
	public Guerrero() {
		this.vida=vida;
		ataque=10;
		//Date construction
		 Date fecha_creacion = new Date();
		 GregorianCalendar calendario_creacion=new GregorianCalendar();
		 calendario_creacion.setTime(fecha_creacion);
			System.out.println("La fecha de creacion del Guerrero Test2 es: "
					+calendario_creacion.get(Calendar.DAY_OF_MONTH)+"/"
					+calendario_creacion.get(Calendar.MONTH)+"/"
					+calendario_creacion.get(Calendar.YEAR)+" a las "
					+calendario_creacion.get(Calendar.HOUR)+":"
					+calendario_creacion.get(Calendar.MINUTE)+":"
					+calendario_creacion.get(Calendar.SECOND)+":"
					+calendario_creacion.get(Calendar.MILLISECOND));
		
	}
	
	//getters y setters
	public int getAtaque() {
		return ataque;
	}

	public void setAtaque(int ataque) {
		this.ataque = ataque;
	}
	//method
	public int movimientoLucha(){
		int movement=(int)(Math.random()*ataque);
		System.out.println("****Espadazo****");
		return movement;
	}
}

GuerreroFuerte

package p1_t5;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.text.DateFormat;



public class GuerreroFuerte extends Guerrero {
	GuerreroFuerte()
	{
		this.vida=vida;
		this.ataque=ataque;
		//Date construction
		Date fecha_creacion=new Date();
		GregorianCalendar calendario_creacion=new GregorianCalendar();
		calendario_creacion.setTime(fecha_creacion);
		System.out.println("La fecha de creacion del Guerrero Fuerte SuperAnaquin es: "
							+calendario_creacion.get(Calendar.DAY_OF_MONTH)+"/"
							+calendario_creacion.get(Calendar.MONTH)+"/"
							+calendario_creacion.get(Calendar.YEAR)+" a las "
							+calendario_creacion.get(Calendar.HOUR)+":"
							+calendario_creacion.get(Calendar.MINUTE)+":"
							+calendario_creacion.get(Calendar.SECOND)+":"
							+calendario_creacion.get(Calendar.MILLISECOND));
	}
	//Method
	public int movimientolucha(){
		int movement=((int)(Math.random()*ataque)+5);
		System.out.println("****Espadazooooo super fuerte****");
		return movement;
	}
}

GuerreroNormal

package p1_t5;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.text.DateFormat;

public class GuerreroNormal extends GuerreroFuerte {
	GuerreroNormal()
	{
		this.vida=vida;
		this.ataque=ataque;
		
		Date fecha_creacion=new Date();
		GregorianCalendar calendario_creacion=new GregorianCalendar();
		calendario_creacion.setTime(fecha_creacion);
		System.out.println("La fecha de creacion del Guerrero Normal Anaquinino es: "
							+calendario_creacion.get(Calendar.DAY_OF_MONTH)+"/"
							+calendario_creacion.get(Calendar.MONTH)+"/"
							+calendario_creacion.get(Calendar.YEAR)+" a las "
							+calendario_creacion.get(Calendar.HOUR)+":"
							+calendario_creacion.get(Calendar.MINUTE)+":"
							+calendario_creacion.get(Calendar.SECOND)+":"
							+calendario_creacion.get(Calendar.MILLISECOND));
	}

}

Arena

package p1_t5;

import java.util.Scanner;

public class Arena {
	//Creation of method Lucha 
	public static void Lucha(Guerrero Test1, Mago Test2, int danoValor, Integer danoReferencia) {
		System.out.println(Test1.movimientoLucha());
		int dañoMago=Test2.getVida()-Test1.movimientoLucha();
		System.out.println("La vida del Mago Test1 se ha reducido a : "+dañoMago);
		System.out.println(Test1.movimientoLucha());
		int dañoGuerrero=Test1.getVida()-Test2.movimientoLucha();
		System.out.println("La vida del Guerrero Test2 se ha reducido a: "+dañoGuerrero);
		danoValor=Test1.movimientoLucha()+Test2.movimientoLucha();
		System.out.println("El daño acumulado por valor es: "+danoValor);
		danoReferencia=Test1.movimientoLucha()+Test2.movimientoLucha();
		System.out.println("El daño acumulado por referencia es: "+danoReferencia);
	}
	
	//Creation of method Lucha first round
	public static void Lucha(GuerreroFuerte BigTest1, Mago Test2, int danoValor, Integer danoReferencia){
		System.out.println(BigTest1.movimientoLucha());
		int dañoMago=Test2.getVida()-BigTest1.movimientoLucha();
		System.out.println("La vida del Mago Test1 en la primera ronda se ha reducido a : "+dañoMago);
		System.out.println(BigTest1.movimientoLucha());
		int dañoGuerrero=BigTest1.getVida()-Test2.movimientoLucha();
		System.out.println("La vida del GuerreroFuerte BigTest1 en la primera ronda se ha reducido a: "+dañoGuerrero);
		danoValor=BigTest1.movimientoLucha()+Test2.movimientoLucha();
		System.out.println("El daño acumulado en la primera ronda por valor es: "+danoValor);
		danoReferencia=BigTest1.movimientoLucha()+Test2.movimientoLucha();
		System.out.println("El daño acumulado en la primera ronda por referencia es: "+danoReferencia);
	}
	
	//Creation of method Lucha second round 
	public static void Lucha(GuerreroNormal Prueba2, Mago Test2, int danoValor, Integer danoReferencia){
		System.out.println(Prueba2.movimientoLucha());
		int dañoMago=Test2.getVida()-Prueba2.movimientoLucha();
		System.out.println("La vida del Mago Test2 en la segunda ronda se ha reducido a : "+dañoMago);
		System.out.println(Prueba2.movimientoLucha());
		int dañoGuerrero=Prueba2.getVida()-Test2.movimientoLucha();
		System.out.println("La vida del Guerrero Anaquinino en la segunda ronda  se ha reducido a: "+dañoGuerrero);
		danoValor=Prueba2.movimientoLucha()+Test2.movimientoLucha();
		System.out.println("El daño acumulado en la segunda ronda por valor es: "+danoValor);
		danoReferencia=Prueba2.movimientoLucha()+Test2.movimientoLucha();
		System.out.println("El daño acumulado en la segunda ronda por referencia es: "+danoReferencia);
		System.out.println("La vida del Mago Test2 en un 10 por ciento mayor: "+Test2.getVida());
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner Entrada=new Scanner(System.in);
		//Inizialization of variables 
		int vida=200;
		int ataque=800;
		//7.Creation of two variables danoValor & wrapper danoReferencia
		int danoValor=0;
		Integer danoReferencia=0;
		//Creation of a warrior 
		final Guerrero Test1=new Guerrero();
		System.out.println("El nivel de vida del guerrero es "+Test1.getVida());
		System.out.println(Test1.movimientoLucha());
		//Creation of a Magician
		final Mago Test2=new Mago();
		System.out.println("El nivel de vida del mago es "+Test2.getVida());
		System.out.println(Test2.movimientoLucha());
		//Call the method
		Lucha(Test1, Test2,danoValor,danoReferencia);
		//Creation of a new Guerrero Fuerte
		GuerreroFuerte BigTest1=new GuerreroFuerte();
		Lucha(BigTest1,Test2, danoValor, danoReferencia);
		GuerreroNormal Prueba2=new GuerreroNormal();
		Prueba2=(GuerreroNormal)BigTest1;
		Lucha (Prueba2, Test2, danoValor, danoReferencia);
		
	}
}

huangapple
  • 本文由 发表于 2020年3月16日 21:54:19
  • 转载请务必保留本文链接:https://java.coder-hub.com/60707291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定