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.ja8;
- /**
- *
- * @author Admin
- */
- public class Main3 {
- public static void main(String[] args) {
- // instanceof
- String s = "HELLO";
- System.out.println("s instanceof String =" + (s instanceof String));
- boolean res = s instanceof String;
- System.out.println("res=" + res);
- // Student s = new Student("Olga2", "Petrova2");
- Student s1 = new Student("Olga2", "Petrova2");
- boolean res2 = s1 instanceof Person;
- System.out.println("res2=" + res2);
- res2 = s1 instanceof Student;
- System.out.println("res2=" + res2);
- res2 = s1 instanceof Object;
- System.out.println("res2=" + res2);
- //
- Person p2 = null;
- p2 = new Student("Olga", "Petrova3");
- if(p2 instanceof Student){
- System.out.println("STUDENT!!!!!!!!");
- // используем явное преобразование типа - оператор (целевой_тип)
- //Student s3 = (Student) p2;
- Student s3 = (Student) p2;
- System.out.println("s3=" + s3.getMark());
- //System.out.println("");
- }
- p2 = new Person("Olga", "Petrova3");
- //Student s4 = (Student) p2;
- if(p2 instanceof Student){
- Student s4 = (Student) p2;
- }
- System.out.println("OK!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement