超类中的java访问子类

时间:2017-05-16 03:59:13

标签: 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 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

4 个答案:

答案 0 :(得分:1)

如果我理解您的问题,然后是一个可能的解决方案,请将String传递给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("B a");super("C a");。但我建议你将这种方法提取到实用方法中,我不会个人提示在构造函数中输入用户。

答案 1 :(得分:0)

在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();
     }
   }
}

答案 2 :(得分:0)

您必须使用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打印为类

的名称

答案 3 :(得分:0)

放弃使用构造函数执行输入/输出的任何想法。它永远不会以你喜欢的方式工作。这将是:

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);
    }    
}
相关问题