Advertisement
sergAccount

Untitled

Dec 13th, 2020
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 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.     public static void printCar(Car car){
  15.        
  16.         System.out.println("PRINT CAR OBJECT");
  17.     }
  18.    
  19.     public static void main(String[] args) {
  20.        
  21.         Truck t1 = new Truck();
  22.         Truck t2 = new Truck();
  23.         //
  24.         System.out.println("t1.cap=" + t1.getCapacity());
  25.  
  26.         t1.speedUp(10);
  27.         t1.speedUp(20);
  28.        
  29.         System.out.println("t1.speec=" + t1.getSpeed());
  30.         //
  31.         Car c2 = null;
  32.         c2 = t1;
  33.        
  34.         Car c3 = new Truck();
  35.         Car c4 = t1;
  36.        
  37.         Truck t3;
  38.         //t3 = c2;
  39.        
  40.         printCar(new Car());
  41.         printCar(new Truck());
  42.        
  43.         Car c5 = new Car();
  44.         // получаем имя класса
  45.         System.out.println("c5.getClass().name=" + c5.getClass().getName());
  46.        
  47.         Car c6 = new Truck();
  48.         System.out.println("c6.getClass().name=" + c6.getClass().getName());
  49.        
  50.         // использование явного преобразования к типу Truck - испозуется оператор (ИмяТипа)        
  51.         Truck t7 = (Truck) c6;  
  52.         System.out.println("t7.cap=" + t7.getCapacity());
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement