I wonder how to retrieve the Java instance name.

Asked 2 years ago, Updated 2 years ago, 23 views

public class Car {
    public int gas; // fields accessible to anyone
}
public class Suv extends Car{
    //The child class can use the gas field of the parent class.

}
public class Truck extends Car{
//The child class can use the gas field of the parent class.
}
public class Bus extends Car{
//The child class can use the gas field of the parent class.

}
public class Test {
    public static void main(String[] args) {
        Test gasStation= new Test();
        Suv suv=new Suv();
        Truck truck = new Truck(); 
        Bus bus = new Bus();

        gasStation.fill(suv); //suv Add oil
        gasStation.fill(truck);//truck oil
        gasStation.fill(bus);//bus oil
        System.out.println(suv.gas+truck.gas+bus.gas); 
        //3 are all the same variables (the gas field values (10) of the parent class are shared together)

    }


    public void fill(Car car){
        System.out.println(car.getClass().getSimpleName()+");
        car.gas += 10;
        System.out.println ("Oil contains "+car.gas+" liters).");
//      I want to call String name=~~/sub, truck, bus.
//      System.out.println ("The regional variable currently in use is "+name+"); 
//      The final goal is the field car of the parent class.I didn't put a value in gas, but I called the name of the local variable 
//      Each one is suv.gas, truck.gas, bus.I want to add a different value with gas
        }

}

Here, I would like to take the local variable names suv, truck, and bus without sharing the value 10 of the field name gas and make the desired result value as follows. (suv.gas=10, truck.gas=20, bus.I want to substitute gas=30)

Add oil to the suv.

It contains 10 liters of oil.

Oil the truck.

It contains 20 liters of oil.

Oil the bus.

It contains 30 liters of oil.

java

2022-09-21 20:25

2 Answers

Perhaps the question means a factory method pattern, although I don't understand it clearly.

A method of encapsulating object creation, usually a pattern delegated to a subclass.

Encapsulating object creation gives you the advantage of further abstraction. This means less direct access to specific classes.

public class CarType {
    public final static String CAR_SUV = "suv";
    public final static String CAR_TRUCK = "trcuk";
}

...
public Car createCar(String carType){
   if("suv".equals(carType)){ return new Suv(); }
   else if("truck".equals(carType)){ return new Truck(); }
   else { return null; }
 }
...
Car car = createCar(CarType.CAR_SUV); // suv
Car car = createCar(CarType.CAR_TRUCK); // truck

Call fill method of fill(car) // question...Car is either suv or truck

It can be used as above.


2022-09-21 20:25

Why do you think it's Gong Yoo?

gas is not a shared variable.

To be shared, it must be a static variable as shown below.

public class Car {
    public static int gas; // static variable
}

Variables that exist for each instance unless static.

As desired

suv.gas=10, truck.gas=20, bus.gas=30

You can use it with .


2022-09-21 20:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.