“Codegrepper Java Instanceof” Kode Jawaban

Codegrepper Java Instanceof

if (vehicle instanceof Car) {
        Car car = (Car) vehicle;
        return car.wheelCount;
     } else if (vehicle instanceof Bike) {
          Bike bike = (Bike) vehicle;
         return bike.numberOfWheels;
     } else {
         throw new IllegalArgumentException("Unrecognized vehicle");
     }
Smoggy Snail

Codegrepper Java Instanceof

 if (vehicle instanceof Car car) {
        return car.wheelCount;
     } else if (vehicle instanceof Bike bike) {
         return bike.numberOfWheels;
     } else {
         throw new IllegalArgumentException("Unrecognized vehicle");
     }
Smoggy Snail

Jawaban yang mirip dengan “Codegrepper Java Instanceof”

Pertanyaan yang mirip dengan “Codegrepper Java Instanceof”

Lebih banyak jawaban terkait untuk “Codegrepper Java Instanceof” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya