Java如何使方法参数接受子类类型和超类
我有一个超类和一个子类,其中包含一些变量,如下所示:Java如何使方法参数接受子类类型和超类,java,oop,generics,inheritance,subclassing,Java,Oop,Generics,Inheritance,Subclassing,我有一个超类和一个子类,其中包含一些变量,如下所示: 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 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;
}
}
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
}
}
}
我想在主类中构建一个方法,该方法接受类型为a
或B
的泛型参数,如下所示:
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;
}
}
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
}
}
}
如何才能做到这一点?。泛型是一种选择吗?我曾考虑在A
中创建printObject()
方法,然后在B
中重写它,但是我在main中创建了一些其他变量,如val
更新
我试着像上面的方法一样使用
instanceOf
。但这样编译器就找不到子类的特定方法。首先,根据定义,如果您将A声明为任何方法的参数,而B是它的子类,那么任何A或B都可以传递给该方法
然后,您可以使用instanceof操作符(检查传入的参数是否为B类型)实现所需的功能。但是,通常应该使用继承/方法重写,而不是instanceof
您可以将“val”传递到A/B上的printObject()方法中。如果涉及多个变量,如“val”,则可以传递另一个对象,或者可能需要将代码拆分为类A(在B中重写)上的多个方法,并根据需要传递不同的值?(通常情况下,您不会在一个用于打印对象的方法中进行计算,但这可能只是一个示例?一切都要简单得多)您可以在主类中删除此方法,因为它会产生一些冗余耦合。2019年,所有这些
instanceof
的味道真的很难闻。你可以让它更独立
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();
每个脚本是否会根据其实际实现调用正确的运行时函数如果对象是类型A,该脚本应该做什么?那么
t
应该包含什么?注释中有一个类似if的条件。“如果对象是B…a.getThird的实例”。我想相关的问题是一个方法属性可以同时接受类型a(超类)和类型B(子类)吗?请尝试编写一些代码来说明这一点。我不认为泛型对你有帮助-请看一下泛型的概念,当你看到你的问题和泛型之间的任何联系时,编辑你的问题,我并不意味着泛型是我唯一的选择。任何有效的都是好的。我认为这是一个相当合理的问题。我如何编写一个接受基类中任何类型的方法?我得到的可能是重复的。只是打印方法比那复杂。我实际上是在android平台上,打印方法实际上是根据子类或超类的值更新视图的元素(如文本和图像)。因此,这里的主要思想是封装此对象中所有特定于对象的逻辑。如果这个方法比你可能需要检查你的应用程序架构复杂。当然,您可以通过所有这些“instanceof”和其他解决方法简单地解决您的问题,但它肯定会导致您使用意大利面代码和真正的紧密耦合。它在未来将很难维护,因此,我强烈建议考虑架构。