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);
    }

    }

  • 相关阅读:
    jQuery Ajax 实例 全解析
    用Javascript评估用户输入密码的强度
    常用网址
    常用的107条Javascript
    根据键盘操作表格
    HTML5吧
    css3动画简介以及动画库animate.css的使用
    jquery插件下载地址
    CEO、COO、CFO、CTO
    springboot与shiro配置
  • 原文地址:https://www.cnblogs.com/wlp1115/p/6704909.html
Copyright © 2011-2022 走看看