Advertisement
sergAccount

Untitled

Dec 13th, 2020
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.mycompany.ja9;
  7.  
  8. /**
  9.  *
  10.  * @author Admin
  11.  */
  12. public class Main3 {
  13.     //    
  14.     // реализовать метод  printCar с использованием опреторов
  15.     // instanceof и оператора явного преобразования типа (ИмяТипа)
  16.     public static void printCar(Car car){
  17.        
  18.         System.out.println("PRINT CAR OBJECT");
  19.     }
  20.    
  21.     public static void main(String[] args) {
  22.        
  23.         Truck t1 = new Truck();
  24.         Truck t2 = new Truck();
  25.         //
  26.         System.out.println("t1.cap=" + t1.getCapacity());
  27.  
  28.         t1.speedUp(10);
  29.         t1.speedUp(20);
  30.        
  31.         System.out.println("t1.speec=" + t1.getSpeed());
  32.         //
  33.         Car c2 = null;
  34.         c2 = t1;
  35.        
  36.         Car c3 = new Truck();
  37.         Car c4 = t1;
  38.        
  39.         Truck t3;
  40.         //t3 = c2;
  41.        
  42.         printCar(new Car());
  43.         printCar(new Truck());
  44.        
  45.         Car c5 = new Car();
  46.         // получаем имя класса
  47.         System.out.println("c5.getClass().name=" + c5.getClass().getName());
  48.         Car c6 = new Truck();
  49.         System.out.println("c6.getClass().name=" + c6.getClass().getName());
  50.         // использование явного преобразования к типу Truck - испозуется оператор (ИмяТипа)        
  51.         Truck t7 = (Truck) c6;  
  52.         System.out.println("t7.cap=" + t7.getCapacity());
  53.         //    Car c5 = new Car();        
  54. //        Truck t8 = (Truck)c5;
  55. //        System.out.println("t8.cap=" + t8.getCapacity());
  56.         // использование оператора istanceof
  57.         boolean res = c5 instanceof Car;
  58.         System.out.println("c5 instanceof Car=" + res);
  59.         res = c5 instanceof Truck;
  60.         System.out.println("c5 instanceof Truck=" + res);        
  61.         //Car c3 = new Truck();
  62.         res = c3 instanceof Car;
  63.         System.out.println("c3 instanceof Car=" + res);
  64.         res = c3 instanceof Truck;
  65.         System.out.println("c3 instanceof Truck=" + res);
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement