Advertisement
sergAccount

Untitled

Feb 7th, 2021
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 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.ja8;
  7.  
  8. /**
  9.  *
  10.  * @author Admin
  11.  */
  12. public class Main3 {
  13.    
  14.    
  15.     public static void main(String[] args) {
  16.         // instanceof        
  17.         String s = "HELLO";
  18.         System.out.println("s instanceof String =" + (s instanceof String));
  19.        
  20.         boolean res = s instanceof String;
  21.         System.out.println("res=" + res);
  22.        
  23.         // Student s = new Student("Olga2", "Petrova2");  
  24.         Student s1 = new Student("Olga2", "Petrova2");
  25.         boolean res2 = s1 instanceof Person;
  26.         System.out.println("res2=" + res2);
  27.         res2 = s1 instanceof Student;
  28.         System.out.println("res2=" + res2);
  29.         res2 = s1 instanceof Object;
  30.         System.out.println("res2=" + res2);        
  31.         //
  32.         Person p2 = null;
  33.         p2 = new Student("Olga", "Petrova3");
  34.         if(p2 instanceof Student){
  35.             System.out.println("STUDENT!!!!!!!!");
  36.             // используем явное преобразование типа - оператор (целевой_тип)
  37.             //Student s3 = (Student) p2;
  38.             Student s3 = (Student) p2;
  39.             System.out.println("s3=" + s3.getMark());
  40.             //System.out.println("");
  41.         }
  42.         p2 = new Person("Olga", "Petrova3");
  43.         //Student s4 = (Student) p2;
  44.         if(p2 instanceof Student){
  45.             Student s4 = (Student) p2;
  46.         }
  47.        
  48.         System.out.println("OK!");        
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement