Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.mycompany.ja9;
- /**
- *
- * @author Admin
- */
- public class Main3 {
- //
- // реализовать метод printCar с использованием опреторов
- // instanceof и оператора явного преобразования типа (ИмяТипа)
- public static void printCar(Car car){
- System.out.println("PRINT CAR OBJECT");
- }
- public static void main(String[] args) {
- Truck t1 = new Truck();
- Truck t2 = new Truck();
- //
- System.out.println("t1.cap=" + t1.getCapacity());
- t1.speedUp(10);
- t1.speedUp(20);
- System.out.println("t1.speec=" + t1.getSpeed());
- //
- Car c2 = null;
- c2 = t1;
- Car c3 = new Truck();
- Car c4 = t1;
- Truck t3;
- //t3 = c2;
- printCar(new Car());
- printCar(new Truck());
- Car c5 = new Car();
- // получаем имя класса
- System.out.println("c5.getClass().name=" + c5.getClass().getName());
- Car c6 = new Truck();
- System.out.println("c6.getClass().name=" + c6.getClass().getName());
- // использование явного преобразования к типу Truck - испозуется оператор (ИмяТипа)
- Truck t7 = (Truck) c6;
- System.out.println("t7.cap=" + t7.getCapacity());
- // Car c5 = new Car();
- // Truck t8 = (Truck)c5;
- // System.out.println("t8.cap=" + t8.getCapacity());
- // использование оператора istanceof
- boolean res = c5 instanceof Car;
- System.out.println("c5 instanceof Car=" + res);
- res = c5 instanceof Truck;
- System.out.println("c5 instanceof Truck=" + res);
- //Car c3 = new Truck();
- res = c3 instanceof Car;
- System.out.println("c3 instanceof Car=" + res);
- res = c3 instanceof Truck;
- System.out.println("c3 instanceof Truck=" + res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement