N
N
newdancer2016-07-09 18:01:06
Java
newdancer, 2016-07-09 18:01:06

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)

how to access the variables of the required class passed to the method after?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Evgeny Kornachev, 2016-07-09
@zelan

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; //результат - продукт переработки котлет
 }
}

And let's say you have a Granny, to whom you give the animal for the weekend and she will have to feed them periodically. Agree - granny does not care who will devour cutlets, she does not want to feed them in different ways, it is only important for her that they eat cutlets and she removes the products of digestion.
Accordingly, granny does not want to have 2 methods:
class GrandMa{
 public void feedCat(Cat cat){}
 public void feedDog(Dog dog){}
}

This is where interfaces come in. We see that both the cat and the dog are eating meatballs. Let's say that they, like animals, should eat them (cutlets) - this is the interface.
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; //результат - продукт переработки котлет
 }
}

Everything, now granny will be glad she will consider animals as eaters of cutlets, and not as a cat or a dog.
class GrandMa{
 public void feedBeafEater(BeafEater BeafEater){
   int weightBeaf = 5;
   int shit = beafEater.eatBeaf(weightBeaf ); //кормим поедателя котлет, колучаем какахи
   cleanToilet(shit ); //бабушка убирает какахи
}

}

That's it, you just pass a cat or dog in your grandmother and she feeds them.
public static void main(String[] args){

 GrandMa grandMa = new GrandMa();
 Cat cat = new Cat();
 Dog dog = new Dog;
 
//напоминаю и кот и пес - рассматриваются бабушкой как пожиратели котлет, ей важно чтобы они поодерживали определенный интерфейс (умели делать определенные вещи)
grandMa.feedBeafEater(cat);
grandMa.feedBeafEater(dog);

}

B
belozerow, 2016-07-09
@belozerow

public int calc(String data, Object value, Boolean rez){
    if(value instanceof MyObject1){
        ((MyObject1)value).doSomething()
    }
}

But in general a bad approach, read about OOP

M
mitekgrishkin, 2016-07-09
@mitekgrishkin

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

2) Interfaces:
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();
}

N
NoMoneyException, 2016-07-09
@eugene_leshchinskiy

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 question

Ask a Question

731 491 924 answers to any question