• 实验七:类的多态性


    Package zuoye7;

    public class MyDate {

        private int year,month,day;

        private static int thisYear;

        public int getMonth() {

            return month;

        }

        public int getDay() {

            return day;

        }

        public void setDay(int day) {

            this.day = day;

        }

        public void setYear(int year) {

            this.year = year;

        }

        public void setMonth(int month) {

            this.month = month;

        }

        public int getYear() {

            return year;

        }

        static {

            thisYear=2018;

        }

        public MyDate(int year,int month,int day){

            this.day=day;

            this.month=month;

            this.year=year;

        }

        public MyDate(MyDate birthdate){

            this(1900,1,1);

            System.out.println("默认出生日期数据");

        }

        public static void set (){

        }

        public String toString(){

            return year+" "+month+" "+day;

        }

    }

    public class Person {

    public String name;

    public static MyDate birthdate;

    public String gender,province,city;

    private static int count=0;

    public Person(String name,MyDate birthdate,String gender,String province,String city) {

    this.set(name,birthdate,gender,province,city);

    count++;

    }

    public Person(String name,MyDate birthdate) {

    this(name,birthdate,"","", name);

    }

    public Person() {

    this("",new MyDate(birthdate));

    }

    @SuppressWarnings("static-access")

    public Person(Person per)

    {

    this(per.name,new MyDate(per.birthdate),per.gender,per.province,per.city);

    }

    public void finalize() {

    System.out.println("释放对象("+this.toString()+")");

    Person.count--;

    }

    public static void howMany()

    {

    System.out.println(Person.count+"个Person对象,");

    }

        @SuppressWarnings("static-access")

    public void set(String name,MyDate birthdate,String gender,String province,String city) {

         this.name=name==null?"":name;

        this.birthdate=birthdate;

        this.province=province==null?"":province;

        this.city=city==null?"":city;

        }

        public void set(String name,MyDate birthdate) {

         this.set(name, birthdate,"","", name);

        }

        @SuppressWarnings("static-access")

    public String toString() {

         return this.name+","+(this.birthdate==null?"":birthdate.toString())+","+this.gender+","+this.city;

        }

    public void set(String string, String string2, String string3, boolean b) {

    // TODO Auto-generated method stub

    }

    }

     

    Import java.util.Scanner;

    public class Student<birthdate, birthdate1> extends Person

    {

    public String department,speciality,number;

    public boolean member;

    private int birthdate1;

    private static int count=0;

    public Student(String name,MyDate birthdate,String gender,String province,String city,

    String department,String speciality,String number,boolean member) {

    super(name,birthdate,gender,province,city);

    this.set(speciality, department,number,member);

    count++;

    }

    public Student()

    {

    super();

    this.set("","","",false);

    Student.count++;

    }

    public Student(Person person,String department,String speciality,String number,boolean member)

    {

    super(person);

    this.set(number,birthdate,speciality,member);

    Student.count++;

    }

    public Student(@SuppressWarnings("rawtypes") Student stu) {

    this(stu,stu.department,stu.speciality,stu.number,stu.member);

    }

    public void finalize()

    {

    super.finalize();

    Student.count--;

    }

    public static void howMany() {

    Person.howMany();

    System.out.println(Student.count+"个Student对象");

    }

    public void set(String department,MyDate birthdate,String number,boolean member) {

    Object departemnt = null;

    this.department=departemnt==null?"":department;

    this.speciality=birthdate==null?"":number;

    this.member=member;

    }

    @SuppressWarnings("static-access")

    public void chazhao(String name) {

    @SuppressWarnings("unused")

    int m;

    int h = 0;

    @SuppressWarnings("rawtypes")

    Student m1=new Student();

    String n;

    @SuppressWarnings("resource")

    Scanner shuru=new Scanner(System.in);

    System.out.println("请输入你要查询的姓n");

    n=shuru.nextLine();

    if(m1.name==n) {

    System.out.println(m1.name);

    }

    System.out.println("请输入你要查询的生日n");

    n=shuru.nextLine();

    if(m1.birthdate1==h) {

    System.out.println(m1.birthdate);

    }

    System.out.println("请输入你要查询的城市");

    n=shuru.nextLine();

    if(m1.city==n) {

    System.out.println(m1.city);

    }

    }

    public String toString() {

    return super.toString()+","+this.department+","+this.speciality+","+this.number+(member?",团员":"");

    }

    @SuppressWarnings("null")

    public static void main(String[] args)

    {

    Person per=new Person("李小明",new MyDate(1994,3,15),"男","湖南省","长沙市");

    @SuppressWarnings("rawtypes")

    Student stu1=new Student(per,"计算机","计算机科学与技术","211994001",true);

    @SuppressWarnings("rawtypes")

    Student stu2=new Student(stu1);

    Person stu = null;

    stu.set("张丽",new MyDate(1998,4,5),"女","湖北省","武汉市");

    stu.set("经济管理系","信息管理专业","3211998003",true);

    Student.howMany();

    System.out.println("per: "+per.toString()+" stu1:"+stu1.toString()+"nstu2:"+stu2);

    stu2.finalize();

    Student.howMany();

    }

    }

    心得: 在该实验中只是根据书上的例子敲写进去的,有好多地方还不太完整,想看同学编写的程序也看不明白。在实验中有许多逻辑错误,程序只是完成了一部分,还有待改善。

  • 相关阅读:
    进制转化
    8.28总结前日及今日
    Uva1213(线性筛模板+dp)
    Uva1363(余数性质/减少枚举量)
    Uva1640(统计数字出现的次数)
    Uva1639(概率期望/对数处理避免丢失精度)
    Uva12034 (组合数取模)
    Uva10820 欧拉公式模板(求小于n且与n互素的数的个数)
    Uva1635 二项式递推+质因子分解+整数因子分解
    8.19总结今日
  • 原文地址:https://www.cnblogs.com/Java199-wfx/p/10822308.html
Copyright © 2020-2023  润新知