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.ex19;
- public class Main2 {
- //
- public static void printInfo(Person p){
- //
- System.out.println("p.getName=" + p.getName());
- System.out.println("p.getLastName=" + p.getLastName());
- // узнаем является ли человек объектом типа Employee (сотрудником)
- boolean isEmployee = p instanceof Employee;
- System.out.println("p.isEmployee=" + isEmployee);
- // оператор преобразования к определенному типу: (целевой_тип)
- if(isEmployee){
- Employee e = (Employee)p;
- System.out.println("e.company=" + e.getCompany());
- }
- System.out.println();
- }
- //
- public static void main(String[] args) {
- // instanceof (результат работы - значение типа boolean)
- String s = "HELLO JAVA";
- boolean isString = s instanceof String;
- //System.out.println("isString=" + isString);
- //
- Person p = new Employee("Ivan", "Ivanov", "Google");
- //
- boolean isPerson = p instanceof Person;
- //System.out.println("isPerson=" + isPerson);
- Employee e = new Employee("Ivan", "Ivanov", "Google");
- boolean isEmployee = e instanceof Employee;
- boolean isPerson1 = e instanceof Person;
- //System.out.println("e.isEmployee=" + isEmployee);
- //System.out.println("e.isPerson1=" + isPerson1);
- Person p1 = new Person("Petr", "Petrov");
- printInfo(p1);
- printInfo(e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement