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

    }

  • 相关阅读:
    Linux mysql 远程访问
    Linux下高并发socket最大连接数所受的各种限制
    Linux之gunzip命令
    不停在终端中报log
    FIO测试
    yum是什么?(linux命令)
    ubuntu grub 登录
    百度网盘命令行方式,解决ubuntu16.04百度网盘无法运行的问题
    excel使用经验汇总
    ubuntu 安装 ipfs 经验
  • 原文地址:https://www.cnblogs.com/wlp1115/p/6704909.html
Copyright © 2011-2022 走看看