我刚开始学习Java,我认为这是一个非常基本的问题,但我找不到正确的答案,所以我试试这里。
我只想在我的控制台中显示一些动物的名字和描述,但我不知道如何操纵类中的变量。
我想我必须使用参数,但我找不到如何在课堂上使用它们......
我的“Animal.java”
public class Animal {
private String nom;
public static String DESCRIPTION;
public String toString(){
return "Je suis " + this.nom;
}
public void direNom(){
System.out.println(toString());
}
public void direDESCRIPTION(){
System.out.println("Description: " + this.DESCRIPTION);
}
public String getNom(){
return nom;
}
public void setNom(String nom){
this.nom = nom;
}
class Vertebre{
int nbrVertebre;
class Mammifere{
class Ours{
String nom = "Poumba";
String DESCRIPTION = "Description de Poumba";
}
class Chimpanze{
String nom = "Cheeta";
String DESCRIPTION = "Description de Cheeta";
}
class Rats{
String nom = "Ratata";
String DESCRIPTION = "Description de Ratata";
}
}
class Poisson{
class Requins{
String nom = "Jaws";
String DESCRIPTION = "Description de Jaws";
}
class Raies{
String nom = "Raimonta";
String DESCRIPTION = "Description de Raimonta";
}
class Truites{
String nom = "Truita";
String DESCRIPTION = "Description de Truita";
}
}
class Reptile{
class Tortue{
String nom = "Tortega";
String DESCRIPTION = "Description de Tortega";
}
class Serpent{
String nom = "Serpento";
String DESCRIPTION = "Description de Serpento";
}
}
}
}
我的“TestZoo.java”
public class TestZoo {
public static void main(String[] args){
Animal unAnimal = new Animal();
unAnimal.setNom("Jaws");
unAnimal.direNom();
unAnimal.direDESCRIPTION();
unAnimal.setNom("Cheeta");
unAnimal.direNom();
unAnimal.direDESCRIPTION();
unAnimal.setNom("Ham");
unAnimal.direNom();
unAnimal.direDESCRIPTION();
}
}
答案 0 :(得分:0)
试试这个:
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone{
public static void main(String []args){
Ours poumba = new Ours();
poumba.direNom();
}
}
class Animal {
protected String nom;
public String toString(){
return "Je suis " + this.nom + ", je suis un " + this.getClass();
}
public void direNom(){
System.out.println(toString());
}
public String getNom(){
return nom;
}
public void setNom(String nom){
this.nom = nom;
}
}
class Vertebre extends Animal {
int nbrVertebre;
}
class Mammifere extends Vertebre {}
class Ours extends Mammifere {
public Ours(){
this.setNom("Poumba");
}
}