zoukankan      html  css  js  c++  java
  • Cast

    class Person{
    public String name;
    Person(String name){
    this.name=name;
    }
    }
    class Student extends Person{
    public int studentId;
    Student(String str,int id){
    super(str);
    studentId=id;

    }
    void studying(){
    System.out.println("I am studying hard!");
    }
    }
    class Employee extends Person{
    public int salary;
    Employee(String str,int s){
    super(str);
    salary=s;
    }
    void working(){
    System.out.println("I am working hard!");
    }
    }
    public class Cast {

    public void testCast(Person p) {
    System.out.println("name is:"+p.name);
    if(p instanceof Student){
    Student s=(Student)p;
    System.out.println("studentId is:"+s.studentId);
    }
    else if(p instanceof Employee){
    Employee e=(Employee)p;
    System.out.println("studentId is:"+e.salary);
    }

    }
    public static void main(String[] args) {
    Cast cast=new Cast();
    Person p=new Person("Tom");
    Student s=new Student("John",18);
    Employee e=new Employee("Lucy",2000);
    System.out.println(p instanceof Person);
    System.out.println(s instanceof Person);
    System.out.println(e instanceof Person);
    System.out.println(p instanceof Student);
    p=new Employee("Mary",3000);
    System.out.println(p.name);
    System.out.println(p instanceof Person);
    System.out.println(p instanceof Employee);
    Employee e1=(Employee) p;
    System.out.println(e1.salary);
    e1.working();
    p=new Student("Lily",4000);
    System.out.println(p.name);
    Student s1=(Student) p;
    System.out.println(s1.studentId);
    s1.studying();
    cast.testCast(p);
    cast.testCast(s);
    cast.testCast(e);
    }

    }

  • 相关阅读:
    6章-项目进度管理-day5
    常规正则表达式
    axios删除接口
    elk
    英语笔记
    升级打怪
    用computed实现watch的保持子组件与父组件值同步
    vertical-align不生效的问题
    css居右
    使用maven创建spring工程出现配置文件打不开/不存在的错误
  • 原文地址:https://www.cnblogs.com/wlp1115/p/6704909.html
Copyright © 2011-2022 走看看