简体   繁体   English

如何从Java中的扩展类初始化对象

[英]how to initiate an object from an extended class in java

I've a mother class livre. 我有一个母亲课堂。

Bd & album are two extended classes from Livre BD和专辑是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: 我在main()上遇到问题,我无法从类相册启动和声明对象(myAlb):这是我所做的:

album[] myAlb; 相册[] myAlb;

myAlb= new album[nbr_of_albums]; myAlb =新专辑[nbr_of_albums];

myAlb[i] = album(1,5,"author","title"); myAlb [i] = album(1,5,“ author”,“ title”); // for an album i I call the constructor of album= error //对于专辑,我将其称为专辑的构造函数= error

here is the error: No enclosing instance of type livre is accessible. 这是错误:无法访问类型为livre的封闭实例。 Must qualify the allocation with an enclosing instance of type livre (egxnew A() where x is an instance of livre). 必须用一个封闭的livre类型的实例(例如xxlivre的实例,例如xxnew A())来限定分配。

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 public static abstract class book替换public abstract class book public static abstract class book

And replace public final class album extends book with public final static class album extends book 并用public final static class album extends book替换public final class album extends book 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. 添加static修饰符可让您实例化内部类,而无需封闭类的实例。 You have the book class as an inner class, that's why you need the static modifier. 您将book类作为内部类,这就是为什么需要static修饰符的原因。

If you also want to instantiate bd class, you should also put static for it. 如果还想实例化bd类,则还应该为其添加static

Your Code can be rewritten as below: 您的代码可以如下重写:

public class livre { public static void main(String[] args) { 公共类livre {公共静态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 有关嵌套类的更多信息,请单击以下链接: https : //docs.oracle.com/javase/tutorial/java/javaOO/nested.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM