Answer the question
In order to leave comments, you need to log in
How can you pass objects of different classes to a method?
How can you pass objects of different classes to a method?
For example, there is a method:
public int calc(String data, Object value, Boolean rez)
Answer the question
In order to leave comments, you need to log in
Those objects that you want to pass - should they, according to the logic of your program, have common methods (to use ONLY them in the method to which you pass the object) or do you want to pass completely different objects?
Update:
look, there is such a thing as an interface.
For example, there are 2 classes Cat and Dog. And there are actions that both are required to do (in different ways) but with a certain result. For example, this action is eating cutlets, and if there is a result (then it should be uniform) or it should not be at all (void). So the classes should look something like this (omitting other things)
class Cat{
public int eatBeaf(int weight){
//тут описываем съедение
return result; //результат - продукт переработки котлет
}
}
class Dog{
public int eatBeaf(int weight){
//тут описываем съедение
return result; //результат - продукт переработки котлет
}
}
class GrandMa{
public void feedCat(Cat cat){}
public void feedDog(Dog dog){}
}
interface BeafEater{
int eatBeaf(int weight);
}
//и скажем что и кот и пес - пожиратели котлет и они умеют их есть
class Cat implements BeafEater{
public int eatBeaf(int weight){
//тут описываем съедение
return result; //результат - продукт переработки котлет
}
}
class Dog implements BeafEater{
public int eatBeaf(int weight){
//тут описываем съедение
return result; //результат - продукт переработки котлет
}
}
class GrandMa{
public void feedBeafEater(BeafEater BeafEater){
int weightBeaf = 5;
int shit = beafEater.eatBeaf(weightBeaf ); //кормим поедателя котлет, колучаем какахи
cleanToilet(shit ); //бабушка убирает какахи
}
}
public static void main(String[] args){
GrandMa grandMa = new GrandMa();
Cat cat = new Cat();
Dog dog = new Dog;
//напоминаю и кот и пес - рассматриваются бабушкой как пожиратели котлет, ей важно чтобы они поодерживали определенный интерфейс (умели делать определенные вещи)
grandMa.feedBeafEater(cat);
grandMa.feedBeafEater(dog);
}
public int calc(String data, Object value, Boolean rez){
if(value instanceof MyObject1){
((MyObject1)value).doSomething()
}
}
I see 2 options:
1) inheritance
public class A {
private Object b;
public Object getB() {
return b;
}
}
public class A1 extends A {
}
public class A2 extends A {
}
public int calc(String data, A value, Boolean rez) {
value.getB();
}
public interface I {
Object getC();
}
public class B implements I {
private Object c;
public Object getC() {
return c;
}
}
public class D implements I {
private Object c;
public Object getC() {
return c;
}
}
public int calc(String data, I value, Boolean rez) {
value.getC();
}
What's the problem? You transmit, then you communicate through the dot
public static double c(NJTax objNJT, Tax objT){
return objNJT.calculateTax() + objT.calculateTax();
}
public static void main(String[] args) {
NJTax NJT = new NJTax(65000);
Tax T = new Tax();
c(NJT, T);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question