I've a mother class livre.
Bd & album are two extended classes from Livre
I've a problem on the main(), I can't initiate & declare an object (myAlb) from the class album: here is what i did:
album[] myAlb;
myAlb= new album[nbr_of_albums];
myAlb[i] = album(1,5,"author","title"); // for an album i I call the constructor of album= error
here is the error: No enclosing instance of type livre is accessible. Must qualify the allocation with an enclosing instance of type livre (egxnew A() where x is an instance of livre).
here is my full code source:
import java.util.*;
public class livre {
public abstract class book {
String titre;
String auteur;
float prix;
int nbr_pages;
book(String titre,String auteur, float prix,int nbr_pages){
this.titre = titre;
this.auteur = auteur;
this.prix = prix;
this.nbr_pages = nbr_pages;
}
abstract void affichage();
}
public class bd extends book {
String couleur;
bd(String titre,String auteur, float prix,int nbr_pages,String couleur){
super(titre,auteur,prix,nbr_pages);
this.couleur = couleur;
}
void affichage(){
System.out.println("\n\nbook:"+titre);
System.out.println("+ auteur"+auteur);
System.out.println("+ prix"+prix);
System.out.println("+ nbr_pages"+nbr_pages);
System.out.println("+ "+couleur);
}
}
public final class album extends book {
String [] couleur;
void changerCouleur(){
int nbr = 0;
System.out.print("Plz set the nbr of the page that you want to color: ");
Scanner sc = new Scanner(System.in);
while (!(nbr<= nbr_pages && nbr > 0 )){ nbr = sc.nextInt();}
System.out.print("Plz set what color u wanna colorate this page: ");
couleur[nbr] = sc.nextLine();
sc.close();
}
void affichage(){
System.out.println("\t\t book:"+titre);
System.out.println("+ auteur"+auteur);
System.out.println("+ prix"+prix);
System.out.println("+ nbr_pages"+nbr_pages);
System.out.println("+ couleurs des pages: ");
for(int i=0;i<nbr_pages;i++) System.out.println(" =>Page["+i+"]= "+couleur[i]);
}
album(String titre,String auteur, float prix,int nbr_pages){
super(titre,auteur,prix,nbr_pages);
couleur = new String[nbr_pages];
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
album[] myAlb; bd[] myBd;
Scanner sc = new Scanner(System.in);
System.out.print("Set the nbr of Albums that you want to make: ");
int nbrAlbum = sc.nextInt();
myAlb= new album[nbrAlbum];
System.out.print("Set the nbr of BD that you want to make: ");
int nbrBd = sc.nextInt();
myBd= new bd[nbrBd];
for(int i=0;i<nbrAlbum;i++){
System.out.print("\tAlbum nbr "+i+": ");
System.out.print("=>titre = ");
String titre = sc.nextLine();
System.out.print("=>auteur = ");
String auteur = sc.nextLine();
System.out.print("=>prix = ");
float prix = sc.nextFloat();
System.out.print("=>nbr de Pages = ");
int nbr_pages = sc.nextInt();
myAlb[i] = new album(titre,auteur,prix,nbr_pages);
}
}
}
Replace public abstract class book
with public static abstract class book
And replace public final class album extends book
with public final static class album extends book
That should work. Adding static
modifier allows you to instantiate an inner class without having an instance of the enclosing class. You have the book class as an inner class, that's why you need the static
modifier.
If you also want to instantiate bd
class, you should also put static
for it.
Your Code can be rewritten as below:
public class livre { public static void main(String[] args) {
abstract class book {
String titre;
String auteur;
float prix;
int nbr_pages;
book(String titre,String auteur, float prix,int nbr_pages){
this.titre = titre;
this.auteur = auteur;
this.prix = prix;
this.nbr_pages = nbr_pages;
}
abstract void affichage();
}
class bd extends book {
String couleur;
bd(String titre,String auteur, float prix,int nbr_pages,String couleur){
super(titre,auteur,prix,nbr_pages);
this.couleur = couleur;
}
@Override
void affichage(){
System.out.println("\n\nbook:"+titre);
System.out.println("+ auteur"+auteur);
System.out.println("+ prix"+prix);
System.out.println("+ nbr_pages"+nbr_pages);
System.out.println("+ "+couleur);
}
}
final class album extends book {
String [] couleur;
void changerCouleur(){
int nbr = 0;
System.out.print("Plz set the nbr of the page that you want to color: ");
Scanner sc = new Scanner(System.in);
while (!(nbr<= nbr_pages && nbr > 0 )){ nbr = sc.nextInt();}
System.out.print("Plz set what color u wanna colorate this page: ");
couleur[nbr] = sc.nextLine();
sc.close();
}
@Override
void affichage(){
System.out.println("\t\t book:"+titre);
System.out.println("+ auteur"+auteur);
System.out.println("+ prix"+prix);
System.out.println("+ nbr_pages"+nbr_pages);
System.out.println("+ couleurs des pages: ");
for(int i=0;i<nbr_pages;i++) System.out.println(" =>Page["+i+"]= "+couleur[i]);
}
album(String titre,String auteur, float prix,int nbr_pages){
super(titre,auteur,prix,nbr_pages);
couleur = new String[nbr_pages];
}
}
album[] myAlb; bd[] myBd;
Scanner sc = new Scanner(System.in);
System.out.print("Set the nbr of Albums that you want to make: ");
int nbrAlbum = sc.nextInt();
myAlb= new album[nbrAlbum];
System.out.print("Set the nbr of BD that you want to make: ");
int nbrBd = sc.nextInt();
myBd= new bd[nbrBd];
for(int i=0;i<nbrAlbum;i++){
System.out.print("\tAlbum nbr "+i+": ");
System.out.print("=>titre = ");
String titre = sc.nextLine();
System.out.print("=>auteur = ");
String auteur = sc.nextLine();
System.out.print("=>prix = ");
float prix = sc.nextFloat();
System.out.print("=>nbr de Pages = ");
int nbr_pages = sc.nextInt();
myAlb[i] = new album(titre,auteur,prix,nbr_pages);
}
}
}
For more info on nested class follow the below link: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.