Java 我创建了两个类,我想在类B的方法中使用类A

Java 我创建了两个类,我想在类B的方法中使用类A,java,Java,我创建了两个类,我想在类B的方法中使用类A 创建A类 我使用protected是因为我在代码中创建了一个超类,不介意 public class A { protected String modulname; protected String verantwortliche; protected int sws; protected int credits; public A ( String modulname, String

我创建了两个类,我想在类B的方法中使用类A
创建A类 我使用protected是因为我在代码中创建了一个超类,不介意

public class A {
protected String modulname; 
        protected String verantwortliche;
        protected int sws;
        protected int credits;

        public A ( String modulname, String verantwortliche, int sws, int credits ){//beginning of the methode of class A
            this.modulname =modulname;
            this.verantwortliche = verantwortliche;
            this.sws= sws;
            this.credits = credits;
        }
        public class B {
        private String pruefer;
        private double leistungeninprozent;
        //i want to use the Attributes/Constructors form class A in the class B
        //an put it like this:
        public B (class A, String pruefer, double
            leistungeninprozent){ 
                this.leistungeninprozent = leistungeninprozent;
                this.modul = modul;
                this.pruefer = pruefer;
        }
更改此项:

public B (class A, String pruefer, double
        leistungeninprozent){ 
            this.leistungeninprozent = leistungeninprozent;
            this.modul = modul;
            this.pruefer = pruefer;
    }
为此:

public B (A aObject, String pruefer, double  //change "class A" to "A aObject"
        leistungeninprozent){ 
            this.leistungeninprozent = leistungeninprozent;
            this.modul = modul;
            this.pruefer = pruefer;
    }
然后您可以在B构造函数中访问它,如下所示:
aoobject.modulename

更改此选项:

public B (class A, String pruefer, double
        leistungeninprozent){ 
            this.leistungeninprozent = leistungeninprozent;
            this.modul = modul;
            this.pruefer = pruefer;
    }
为此:

public B (A aObject, String pruefer, double  //change "class A" to "A aObject"
        leistungeninprozent){ 
            this.leistungeninprozent = leistungeninprozent;
            this.modul = modul;
            this.pruefer = pruefer;
    }

然后可以在B构造函数中访问它,如下所示:
aoobject.modulename

它工作得很好。我如何实现toString方法? 我的意思是将类A的toString实现为类b的toString

这就是toString表格A类:

@Override
    public String toString() {
        return "Module [Modulname=" + modulname + ", Verantwortliche=" + verantwortliche + ", SWS=" + sws
                + ", Credits=" + credits + "]";
    }
这是B类的toString方法:

@Override
    public String toString() {
        return "Leistungen [Pruefer=" + pruefer + ", LeistungeninProzent=" + leistungeninprozent
                + "]";
    }

现在,我只想从两个不同的类中创建一个toString方法。

它很有效。我如何实现toString方法? 我的意思是将类A的toString实现为类b的toString

这就是toString表格A类:

@Override
    public String toString() {
        return "Module [Modulname=" + modulname + ", Verantwortliche=" + verantwortliche + ", SWS=" + sws
                + ", Credits=" + credits + "]";
    }
这是B类的toString方法:

@Override
    public String toString() {
        return "Leistungen [Pruefer=" + pruefer + ", LeistungeninProzent=" + leistungeninprozent
                + "]";
    }

现在我只想从两个不同的类中创建一个toString方法。

在学习Java的早期阶段,我建议避免使用嵌套类。B的实例只能存在于A类的实例中


要回答您的问题,参数应该表示类的实例,而不是类本身。然后成为方法中的局部变量,通过它可以访问其字段和方法。

在学习Java的早期阶段,我建议避免使用嵌套类。B的实例只能存在于A类的实例中

要回答您的问题,参数应该表示类的实例,而不是类本身。然后成为方法中的局部变量,通过它可以访问其字段和方法