Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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_Inheritance_Constructor - Fatal编程技术网

超类中的java访问子类

超类中的java访问子类,java,inheritance,constructor,Java,Inheritance,Constructor,我的代码有点像这样 class A { int a; A() { //Here I want to print B or C System.out.println("Enter the value of a"); a= new Scanner(System.in).nextInt(); } } class B extends A { int b; B() { //Here i can't write

我的代码有点像这样

class A
{
   int a;
   A()
   {
     //Here I want to print B or C
     System.out.println("Enter the value of a");
     a= new Scanner(System.in).nextInt();
   }
}
class B extends A
{
    int b;
    B()
    {
         //Here i can't write print B
         super();
         System.out.println("Enter the value of b");
         b= new Scanner(System.in).nextInt();
     }
   }
}
class C extends A
{
    int c;
    C()
    {
         //Here i can't write print C
         super();
         System.out.println("Enter the value of C");
         c= new Scanner(System.in).nextInt();
     }
 }
//我的主要职能

public static void main(String[] args)
{
     B b1= new B();
     C c1=new C();
//Some other code
//.....
    C c2= new C();
    C c3= new C();
    B b2= new B();
}
在输出中,我希望它在请求输入之前告诉我它是B还是C。我认为在main中添加它是不好的,那么我如何使用构造函数来实现这一点呢

//Output somewhat like this
Enter values of class B
Enter the value of a
10
Enter the value of b
20
Enter values of class B
Enter the value of a
10
Enter the value of C
25

在a中创建一个受保护的变量,然后可以重写其子类B或C中的值

class A
{
   int a;
   protected String type;
   A()
   {
     type = "Class A"
     a= new Scanner(System.in).nextInt();
   }
}

class B extends A
{
    int b;
    B()
    {
         type = "Class B"; 
         b= new Scanner(System.in).nextInt();
     }
   }
}

如果我理解您的问题,那么一个可能的解决方案是将
字符串
传递给
a
中的构造函数。像

class A
{
   int a;
   A(String msg)
   {
     System.out.printf("Enter the value of %s%n", msg);
     a= new Scanner(System.in).nextInt();
   }
}

然后使用
super(“ba”)
超级(“CA”)分别为。但是我建议您将这种方法提取到实用方法中,我个人不会在构造函数中提示用户输入。

您必须在运行时使用java.lang.Class对象来查找类名

将A类修改为:

class A {
    int a;

    A() {
        // Here I want to print B or C
        System.out.println(this.getClass().getSimpleName());
        System.out.println("Enter the value of a");
        a = new Scanner(System.in).nextInt();
    }
}

当您实例化B时,它将调用类A的超级构造函数,并将B打印为类的名称

放弃使用构造函数执行输入/输出的想法。它永远不会像你想的那样工作。以下是一些可以:

public class Main {
    static class A {
        int a;
        public A(int a){
            this.a = a;
        }
    }

    static class B extends A {
        int b;
        public B(int a, int b) {
            super(a);
            this.b = b;
        }
    }

    static class C extends B {
        int c;
        public C(int a, int b, int c){
            super(a,b);
            this.c = c;
        }
    }

    static B getB (Scanner sc){
        System.out.println("Enter values of class B...");
        System.out.println("Enter value of a:");
        int a = sc.nextInt();
        System.out.println("Enter value of b:");
        int b = sc.nextInt();
        return new B(a, b);
    }


    static C getC (Scanner sc){
        System.out.println("Enter values of class C...");
        System.out.println("Enter value of a:");
        int a = sc.nextInt();
        System.out.println("Enter value of b:");
        int b = sc.nextInt();
        System.out.println("Enter value of c:");
        int c = sc.nextInt();
        return new C(a, b, c);
    }


    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        B b1= getB(sc);
        C c1=getC(sc);
        //Some other code
     //.....
        C c2= getC(sc);
        C c3= getC(sc);
        B b2= getB(sc);
    }    
}