Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从同一包中的其他类调用其他java类名_Java - Fatal编程技术网

从同一包中的其他类调用其他java类名

从同一包中的其他类调用其他java类名,java,Java,我有5类,即c1,c2,c3,集装箱,主要在同一个包 从主类中,我将打印c1、c2的类名。c3是c1和c2的超级类 我想打印为 我的名字是c1 我的名字是c2 超级级是c3 但它只打印 我的名字是c3 我的名字是c3 超级班是主要的 这是我的主课 class main{ public static void main(String[] argh) { Container cont= new Container(); c3 o

我有5类,即c1,c2,c3,集装箱,主要在同一个包

从主类中,我将打印c1、c2的类名。c3是c1和c2的超级类

我想打印为

我的名字是c1

我的名字是c2

超级级是c3

但它只打印

我的名字是c3

我的名字是c3

超级班是主要的

这是我的主课

    class main{
        public static void main(String[] argh) {
            Container cont= new Container();
            c3 o1 = cont.getc3("c1");
            c3 o2 = cont.getc3("c2");
            System.out.println("My name is: " + o1.getClass().getName());
            System.out.println("My name is: " + o2.getClass().getName());
            System.out.println("Our superclass is: " + o1.getClass().getSuperclass().getName());

        }
    }
这是我的集装箱班

    class container extends main {

        private String name;

        public c3 getc3(String string) {
            // TODO Auto-generated method stub
            return new c3(string);
        }

        public String getName() {
            System.out.println("-------------------------"+name);
            return name;
        }

        public String toString() {
            return getName();
        }

    }
这是c3级,它是超级级

class c3 extends main {

    private String name;
    private String string1;


    public c3(String string1) {
        string1 = string1;
    }


    public String getName() {
        return name;
    }

    public String toString() {
        return getName();
    }

    public String getString1() {
        return string1;
    }

    public void setString1(String string1) {
        this.string1 = string1;
    }

}
这是c1班

class c1 extends c3{

    public c1(String string1) {
        super(string1);
        // TODO Auto-generated constructor stub
    }

}
这是c2

class c1 extends c3{

    public c2(String string1) {
        super(string1);
        // TODO Auto-generated constructor stub
    }

}

您的
getc3
方法返回
c3
类的对象。传递给它的字符串“c1”和“c2”并没有什么区别

如果希望
c1
c2
,则必须更改
public c3 getc3(String String)
的实现。例如:

    public c3 getc3(String string) {
        if (string.equals("c1"))
            return new c1(string);
        else if (string.equals("c2"))
            return new c2(string);
        else
            return new c3(string);
    }

怎么弄到它们,你能帮我吗?