Advertisement
sergAccount

Untitled

Jul 10th, 2021
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 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.ex19;
  7.  
  8. public class Main2 {    
  9.     //
  10.     public static void printInfo(Person p){
  11.         //
  12.         System.out.println("p.getName="     + p.getName());
  13.         System.out.println("p.getLastName=" + p.getLastName());        
  14.         // узнаем является ли человек объектом типа Employee (сотрудником)
  15.         boolean isEmployee = p instanceof Employee;
  16.         System.out.println("p.isEmployee=" + isEmployee);
  17.         // оператор преобразования к определенному типу: (целевой_тип)
  18.         if(isEmployee){
  19.             Employee e = (Employee)p;
  20.             System.out.println("e.company=" + e.getCompany());
  21.         }
  22.         System.out.println();
  23.     }
  24.     //
  25.     public static void main(String[] args) {
  26.         // instanceof (результат работы - значение типа boolean)
  27.         String s = "HELLO JAVA";
  28.         boolean isString = s instanceof String;
  29.         //System.out.println("isString=" + isString);
  30.         //  
  31.         Person p = new Employee("Ivan", "Ivanov", "Google");
  32.         //
  33.         boolean isPerson = p instanceof Person;
  34.         //System.out.println("isPerson=" + isPerson);
  35.        
  36.         Employee e = new Employee("Ivan", "Ivanov", "Google");
  37.         boolean isEmployee = e instanceof Employee;
  38.         boolean isPerson1  = e instanceof Person;
  39.         //System.out.println("e.isEmployee=" + isEmployee);
  40.         //System.out.println("e.isPerson1="  + isPerson1);
  41.        
  42.         Person p1 = new Person("Petr", "Petrov");
  43.         printInfo(p1);
  44.         printInfo(e);
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement