Java如何使方法参数接受子类类型和超类

我有一个超类和一个带有某些变量的子类,如下所示:

public class A{

    private int first;
    private int second;

    public A(int _first,int _second){
        first = _first;
        second = _second;
    }

    public int getFirst(){
        return first;
    }

}

public class B extends A{

    private int third;

    public B(int _first,int _second,int _third){
        super(_first,_second);
        third = _third;
    }

    public int getThird(){
        return third;
    }

}

我想在主类中构建一个接受通用参数的方法,该参数可以是A类型,也可以是B类型,如下所示:

public class Main{

    public int val = 2;

     public static void main(String []args){
        A a = new A(1,2);
        B b = new B(1,2,3); 

        printObject(a);
        printObject(b);

     }

     public void printObject(A a){

         int f = a.getFirst() * val;
         int s = a.getSecond() * val;

         if(a instanceOf B){
             int t = a.getThird() * val; // compiler does not find the getThird() method this way
         }
     }
}

如何实现?泛型是一种选择吗?我曾考虑过在printObject()内制作A方法,然后在B内重写它,但是我还有其他一些像val这样的变量,是我在main中创建的。

更新

我尝试像上述方法一样使用instanceOf。但是通过这种方式,编译器无法找到子类的特定方法。

DJJ980620 回答:Java如何使方法参数接受子类类型和超类

首先,根据定义,如果您将A声明为任何方法的参数,而B是它的子类,则可以将任何A或B传递给该方法。

可以然后使用instanceof运算符来实现所需的功能(检查传入的参数是否为B类型)。但是,通常应使用继承/方法重写,而不要使用instanceof。

您可以将'val'传递到A / B上的printObject()方法中。如果涉及多个变量,例如“ val”,则可以传入另一个对象,或者可能需要将代码拆分为A类(在B中覆盖)的多个方法,并适当地传入不同的值? (您通常不会在旨在打印对象的方法中进行计算,但这也许只是一个例子?)

,

一切都更加简单)您可以在主类中放弃此方法,因为它会产生一些多余的耦合。而所有这些instanceof确实在2019年闻起来。您可以使其更加独立。

A级:

public class A{

    private int first;
    private int second;

    public A(int _first,int _second){
        first = _first;
        second = _second;
    }

    public int getFirst(){
        return this.first;
    }

    public int getSecond(){
        return this.second;
    }

    public void print(int multiplier) {
        System.out.println(this.first * multiplier);
        System.out.println(this.second * multiplier);
    }

}

B级:

public class B{

    private int third;

    public B(int _first,int _second,int _third){
        super(_first,_second);
        third = _third;
    }

    public int getThird(){
        return this.third;
    }

    @Override
    public void print(int multiplier) {
        super.print(multiplier);
        System.out.println(this.third * multiplier);
    }

}

主要班级:

   public class Main{

         public int val = 2;

         public static void main(String []args){
            A a = new A(1,2);
            B b = new B(1,2,3); 

            a.print(val);
            b.print(val);

         }
   }
,

编写面向对象的代码不仅仅是扩展类,还应将您的API和其他功能设计为解决方案的一部分。

对于您而言,最合适的方法是将print方法添加到对象本身,您可以覆盖整个函数,也可以在覆盖的类中调用超类。

public class A{
    /// ... your code

 public void print(){
    System.out.println("first :"+first+",second : "+second);
}
}

public class B extends A{
     /// ... your code
    public void print(){
       //Option A - use parent class getters/setters to implement print for object B
       System.out.println("first :"+super.getFirst()+",second : "+super.getsecond() +" third" + third);
      }

      //Option B (More usable to methods returning a value or performing an update) - Perform operation on parent variables,then perform on class specific variables       
       super.print()
       System.out.println("third : "+third);
    }    


}

然后

A a  = new A();
A b = new B();

a.print();
b.print();

每个人都会根据其实际实现调用正确的运行时函数

本文链接:https://www.f2er.com/3159512.html

大家都在问