zoukankan      html  css  js  c++  java
  • 201871010115 马北 《面向对象程序设计(java)》 第67周学习总结

    项目

    内容

    《面向对象程序设计(java)》

    https://home.cnblogs.com/u/nwnu-daizh/

    这个作业的要求在哪里

    https://www.cnblogs.com/nwnu-daizh/p/11605051.html

    作业学习目标

    1. 深入理解程序设计中算法与程序的关系;
    2. 深入理解java程序设计中类与对象的关系;
    3. 理解OO程序设计的第2个特征:继承、多态;
    4. 学会采用继承定义类设计程序(重点、难点);
    5. 能够分析与设计至少包含3个自定义类的程序;
    6. 掌握利用父类定义子类的语法规则及对象使用要求。

    随笔博文正文内容包括:

    第一部分:总结第五章理论知识(30分)

    一.继承:

      用已有的类来构建新类的一种机制。当定义了一个新类继承了一个类时,这个类就继承了这个方法和域,同时在新类中添加新的方法和域以适应新情况。继承时Java程序设计中的一项核心技术,也是面向对象特征之一。

        继承的优点:具有层次结构。子类继承父类的域和方法。 代码可重用性。父类的域和方法课用于子类。可以轻松定义子类。设计应用程序变得更加简单。

    二.类、超类和子类:

      1.定义子类:关键字extends表明正在构造的新类派生于一个已存在的类,已存在的类称为超类、基类或父类;新类称为子类、派生类或孩子类。子类比父类拥有的功能更加丰富。

      在通过扩展超类定义子类的时候,仅需要指出子类与超类的不同之处。在设计类的时候,将通用的方法放在超类中,将具有特殊用途的方法放在子类中。

      2.覆盖方法:在子类中可以增加域,增加方法或覆盖超类的方法,但绝对不能删除继承的任何域和方法。

      3.子类构造器:如果子类的构造器没有显式地调用超类的构造器,则将自动地调用超类默认地构造器,如果超类没有不带参数地构造器,并且在子类的构造器中又没有显式地调用超类地其他构造器,则Java编译器将报告错误。

       一个对象变量可以指示多种实际类型的现象被称为多态,在运行时能够自动地选择调用哪个方法地现象称为动态绑定。

       this :1.引用隐式传参 2.调用类其他构造器

            super:1.调用超类的方法 2.调用超类的构造器

      使用super调用基类的方法

      子类构造器会默认调用基类的无参构造来初始化基类的私有域,如果基类没有无参构造,则会报错

      java中,不需要将方法声明为虚拟方法方法,基类指针指向不同对象时,虚拟指针知道指针指向的实际对象类型,所以能正确调用对应的方法,如果不希望让一个方法具有虚拟特征,可以标记为final

      4.继承层次:继承并不仅限于一个层次,由一个公共超类派生出来的所有类的集合被称为继承层次,在继承层次中,从某个特定的类到其祖先的路径被称为该类的继承链。Java不支持多继承。

      5.动态绑定、静态绑定和方法表:

      编译器准确知道调用哪个方法的叫静态绑定(用private、static、final声明过的方法)

      在运行时,调用方法依赖于实际类型并实现方法绑定(运行时可知)的叫动态绑定,虚拟机为基类和派生类生成一个方法表,虚拟机会在方法表中搜索最适合的方法进行调用。

      动态绑定的优点就是:对程序进行拓展时,不需要重新对调用方法的代码进行重新编译,方法重写时,子类的方法的访问性不能低于基类方法。
      6.阻止继承 final类  : 
       final修饰的类不能被继承,final修饰的方法不能被覆盖,final修饰的域在构造方法内初始化,并且不能被改动。  
      被final修饰的类,其方法自动为final方法,但而不包括域。
      动态绑定对系统开销很大,所以即时编译器会自动为没有被覆盖的类标记为内联函数,减少对系统的开销。
      7.强制转换:
      只能在继承层次内进行转换,将基类转换为子类之后,需判断是否转换成功 使用instanceof运算符检验。如果转换失败java不会生成一个空对象,而是抛出一个异常。
      static_cast:基本数据类型转换。
      const_cast:常量转化为非常量,或者反过来。
      reinterpret_cast:任意类型转换而无须考虑安全和常量问题,不要轻易使用。
      dynamic_cast:运行时转换,运行时要进行类型检查,存在继承关系之间(类中要有虚函数才能进行此转换,需检查类型信息,而类型信息保存在虚函数表中)。
      8.抽象类与抽象方法
      抽象类包含抽象方法也可以包含具体数据、具体方法。
      类即时不含有抽象方法,也可以声明为抽象类。
      抽象类不能被实例化。
      9.受保护访问:
      private:本类可见(也是c++初始化列表的原因,因为子类不可方位基类的private字段,需要调用基类的构造函数)
      public:对所有类可见
      protected:本包和所有子类可见(c++只对子类可见)
      默认(无修饰符):对本包可见
    三.object:所有类的超类 :
      在java中,只有基本类型不是对象,其他类型(包括数组,枚举)都是对象,都扩展于超类
         Object类是Java中所有类的祖先——每一个类都由它扩 展而来。在不给出超类的情况下,Java会自动把Object 作为要定义类的超类。 
            可以使用类型为Object的变量指向任意类型的对象。但 要对它们进行专门的操作都要进行类型转换。
      equals方法:定义子类的equals方法时,可调用超类的equals方法。 super.equals(otherObject)
      hashCode方法:Object类中的hashCode方法导出某个对象的散列 码。散列码是任意整数,表示对象的存储地址。 两个相等对象的散列码相等。
    四.泛型数组列表:
      利用ArrayList类,课允许程序在运行时确定数组的大小。
            ArrayList是一个采用类型参数的泛类型。
    五.对象包装器与自动装箱:
      每个基本类型都有之对应的类(对象包装类)
      对象包装类不允许修改其中的值,同时也是final声明的
      使用包装器声明变量时,其==表示其是否指向同一个对象,因此
      两个对象包装器比较时调用equal方法
    六.参数数量可变的方法
      java 使用持有者类型(IntHolder、BooleanHold等)
      可变参数 使用:public static double max(double... value);实际上将若干参数绑定在数组中传递该max函数:public static void main(String... args)。
    七.枚举类:
      实际上是一个类,包含若干个实例,因此两个枚举类型的值比较,直接使用 == ,永远不要调用equal。 
      所有的枚举类型都是Enum类的子类。所以他们继承了这个类的许多方法String toString() //返回枚举常量名
      static Enum valueOf(Class enumClass,String name)//返回指定名字给定类的枚举常量
      int ordinal() //返回在enum声明中枚举常量的索引 (从0 开始)
      int compareTo(E other) //在枚举中的顺序比较
    八.反射:
      反射被大量用在javaBeans中。
      反射的作用:
      ①在运行中分析类的能力
      ②在运行在查看类的对象
      ③实现通过的数组操作代码
      ④利用Method对象。

    第二部分:实验部分

    1、实验目的与要求

    (1) 理解继承的定义;

    (2) 掌握子类的定义要求

    (3) 掌握多态性的概念及用法;

    (4) 掌握抽象类的定义及用途。

    2、实验内容和步骤

    实验1:测试程序1(10分)

     5-1程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package inheritance;
     
    /**
     * This program demonstrates inheritance.
     * @version 1.21 2004-02-21
     * @author Cay Horstmann
     */
    public class ManagerTest
    {
       public static void main(String[] args)
       {
          // construct a Manager object   构造一个manager对象
          var boss = new Manager("Carl Cracker"8000019871215);  //将经理放到数组中去
          boss.setBonus(5000);   //由boss.setBonus单独设置津贴,其中setBonus是Manager的特有方法
     
          var staff = new Employee[3];     //定义一个包含三个雇员的数组
     
          // fill the staff array with Manager and Employee objects
     
          staff[0] = boss;        // 父类可以引用子类
          staff[1] = new Employee("Harry Hacker"500001989101);   //将经理和雇员都放到数组中去
          staff[2] = new Employee("Tommy Tester"400001990315);
     
          // print out information about all Employee objects
          for (Employee e : staff)     //for循环打印出各个雇员对象的信息
             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
       }
    }

     

      运行结果如下:

    5-2程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    package inheritance;
     
    import java.time.*;
     
    public class Employee
    {
       private String name;//private定义了一个只能在该类中访问的字符串常量
       private double salary;
       private LocalDate hireDay;
     
       public Employee(String name, double salary, int year, int month, int day)//定义变量
       {
          this.name = name;//将局部变量的值传递给成员变量
          this.salary = salary;
          hireDay = LocalDate.of(year, month, day);
       }//一个构造器,构造器与类同名
     
       public String getName()
       {
          return name;
       }//访问器
     
       public double getSalary()
       {
          return salary;
       }//访问器
     
       public LocalDate getHireDay()
       {
          return hireDay;
       }//访问器
     
       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }
    }

     

      程序运行结果如下:

     

    5-3程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    package inheritance;
     
    public class Manager extends Employee    //由继承Employee类来定义Manager类的格式,关键字extend表示继承
    {
       private double bonus;
     
       /**
        * @param name the employee's name
        * @param salary the salary
        * @param year the hire year
        * @param month the hire month
        * @param day the hire day
        */
       public Manager(String name, double salary, int year, int month, int day)
       {
          super(name, salary, year, month, day);    //调用超类Employee中含有这些参数的构造器
          bonus = 0;
       }
     
       public double getSalary()
       {
          double baseSalary = super.getSalary();    //用关键字super调用超类Employee中的getsalary方法
          return baseSalary + bonus;
       }
     
       public void setBonus(double b)
       {
          bonus = b;
       }
    }

     

      程序运行结果如下:

    删除程序中Manager类、ManagerTest类,背录删除类的程序代码,在代码录入中理解父类与子类的关系和使用特点。

     删除Manager类程序代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    package inheritance;
     
    public class Manager extends Employee    //由继承Employee类来定义Manager类的格式,关键字extend表示继承
    {
       private double bonus;
     
       /**
        * @param name the employee's name
        * @param salary the salary
        * @param year the hire year
        * @param month the hire month
        * @param day the hire day
        */
       public Manager(String name, double salary, int year, int month, int day)
       {
          super(name, salary, year, month, day);    //调用超类Employee中含有这些参数的构造器
          bonus = 0;
       }
       @Override
       public double getSalary()
       {
          double baseSalary = super.getSalary();    //用关键字super调用超类Employee中的getsalary方法
          return baseSalary + bonus;
       }
     
       public void setBonus(double b)
       {
          bonus = b;
       }
    }

      删除ManagerTest类程序代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    package inheritance;
     
    /**
     * This program demonstrates inheritance.
     * @version 1.21 2004-02-21
     * @author Cay Horstmann
     */
    public class ManagerTest
    {
       public static void main(String[] args)
       {
          // construct a Manager object   构造一个manager对象
          var boss = new Manager("Carl Cracker"8000019871215);  //将经理放到数组中去
          boss.setBonus(5000);   //由boss.setBonus单独设置津贴,其中setBonus是Manager的特有方法
     
          var staff = new Employee[3];     //定义一个包含三个雇员的数组
     
          // fill the staff array with Manager and Employee objects
     
          staff[0] = boss;        // 父类可以引用子类
          staff[1] = new Employee("Harry Hacker"500001989101);   //将经理和雇员都放到数组中去
          staff[2] = new Employee("Tommy Tester"400001990315);
     
          // print out information about all Employee objects
          for (Employee e : staff)     //for循环打印出各个雇员对象的信息
             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
       }
    }

      程序运行结果如下:

     

    实验1:测试程序2(10分)

     5-4程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package abstractClasses;
     
    /**
     * This program demonstrates abstract classes.
     * @version 1.01 2004-02-21
     * @author Cay Horstmann
     */
    public class PersonTest
    {
       public static void main(String[] args)
       {
          var people = new Person[2];   //定义一个包含2个雇员的数组
     
          // fill the people array with Student and Employee objects
          people[0] = new Employee("Harry Hacker"500001989101);
          people[1] = new Student("Maria Morris""computer science");   //将雇员和学生的对象填充到Person引用数组
     
          // print out names and descriptions of all Person objects
          for (Person p : people)
             System.out.println(p.getName() + ", " + p.getDescription());   // 输出对象的姓名和信息描述
       }
    }

     

      程序运行结果如下:

     

    5-5程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package abstractClasses;
     
    public abstract class Person   // 使用abstract关键字
    {
       public abstract String getDescription();   // 定义抽象类型person
       private String name;
     
       public Person(String name)
       {
          this.name = name;//将局部变量的值赋给成员变量
       }//为子类person类的构造器提供代码的构造器
       public String getName()
       {
          return name;    //Person类中还保存着姓名和一个返回姓名的方法
       }//构造器
    }

     

      程序运行结果如下:

     

    5-6程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    package abstractClasses;
     
    import java.time.*;
     
    public class Employee extends Person  //扩展了一个子类Person
    {
       private double salary;
       private LocalDate hireDay;
     
       public Employee(String name, double salary, int year, int month, int day)//定义变量
       {
          super(name);
          this.salary = salary;
          hireDay = LocalDate.of(year, month, day);//hireday使用LocalDate的方法
       }
     
       public double getSalary()
       {
          return salary;
       }
     
       public LocalDate getHireDay()
       {
          return hireDay;
       }
     
       public String getDescription()
       {
          return String.format("an employee with a salary of $%.2f", salary);
       }
     
       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }
    }

     

      程序运行结果如下:

    5-7程序代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package abstractClasses;
     
    public class Student extends Person  //扩展了一个子类Student
    {
       private String major;
     
       /**
        * @param name the student's name
        * @param major the student's major
        */
       public Student(String name, String major)
       {
          // pass name to superclass constructor
          super(name);
          this.major = major;   //将名称传递给超类构造函数
       }
     
       public String getDescription()
       {
          return "a student majoring in " + major;  //返回学生信息
       }
    }

      程序运行结果如下:

    删除程序中Person类,PerTest类,背景录入删除类的程序代码,在代码录入中理解抽象类与子类的关系和使用特点。

    删除Person类程序代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package abstractClasses;
     
    public abstract class Person   // 使用abstract关键字
    {
       public abstract String getDescription();   // 定义抽象类型person
       private String name;
     
       public Person(String name)
       {
          this.name = name;//将局部变量的值赋给成员变量
       }//为子类person类的构造器提供代码的构造器
       public String getName()
       {
          return name;    //Person类中还保存着姓名和一个返回姓名的方法
       }//构造器
    }

     删除PersonTest类程序代码如下: 

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package abstractClasses;
     
    /**
     * This program demonstrates abstract classes.
     * @version 1.01 2004-02-21
     * @author Cay Horstmann
     */
    public class PersonTest
    {
       public static void main(String[] args)
       {
          var people = new Person[2];   //定义一个包含2个雇员的数组
     
          // fill the people array with Student and Employee objects
          people[0] = new Employee("Harry Hacker"500001989101);
          people[1] = new Student("Maria Morris""computer science");   //将雇员和学生的对象填充到Person引用数组
     
          // print out names and descriptions of all Person objects
          for (Person p : people)
             System.out.println(p.getName() + ", " + p.getDescription());   // 输出对象的姓名和信息描述
       }
    }

      程序运行结果如下:

    实验1:测试程序3(11分)

     5-8程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    package equals;
     
    /**
     * This program demonstrates the equals method.
     * @version 1.12 2012-01-26
     * @author Cay Horstmann
     */
    public class EqualsTest
    {
       public static void main(String[] args)
       {
          var alice1 = new Employee("Alice Adams"7500019871215);
          var alice2 = alice1;
          var alice3 = new Employee("Alice Adams"7500019871215);
          var bob = new Employee("Bob Brandson"500001989101);
     
          System.out.println("alice1 == alice2: " + (alice1 == alice2));
     
          System.out.println("alice1 == alice3: " + (alice1 == alice3));
     
          System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
     
          System.out.println("alice1.equals(bob): " + alice1.equals(bob));
     
          System.out.println("bob.toString(): " + bob);
     
          var carl = new Manager("Carl Cracker"8000019871215);
          var boss = new Manager("Carl Cracker"8000019871215);
          boss.setBonus(5000);
          System.out.println("boss.toString(): " + boss);
          System.out.println("carl.equals(boss): " + carl.equals(boss));
          System.out.println("alice1.hashCode(): " + alice1.hashCode());
          System.out.println("alice3.hashCode(): " + alice3.hashCode());
          System.out.println("bob.hashCode(): " + bob.hashCode());
          System.out.println("carl.hashCode(): " + carl.hashCode());
       }
    }

     

      程序运行结果如下:

     

    5-9程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    package equals;
     
    import java.time.*;
    import java.util.Objects;
     
    public class Employee
    {
       private String name;//private定义了一个只能在该类中访问的字符串变量
       private double salary;
       private LocalDate hireDay;
    //创建私有属性
       public Employee(String name, double salary, int year, int month, int day)
       {
          this.name = name;
          this.salary = salary;
          hireDay = LocalDate.of(year, month, day);
       }
     
       public String getName()
       {
          return name;
       }
     
       public double getSalary()
       {
          return salary;
       }
     
       public LocalDate getHireDay()
       {
          return hireDay;
       }//访问器
     
       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }
     
       public boolean equals(Object otherObject)
       {
          // a quick test to see if the objects are identical  快速测试这些对象是否相同
          if (this == otherObject) return true;
     
          // must return false if the explicit parameter is null   如果显示参数为空,必须返回false
          if (otherObject == nullreturn false;
     
          // if the classes don't match, they can't be equal   如果第几个类不匹配,则他们不相同
          if (getClass() != otherObject.getClass()) return false;
     
          // now we know otherObject is a non-null Employee   //其他对象为非空Employee类
          var other = (Employee) otherObject;
     
          // test whether the fields have identical values   //测试是不是有相同值
          return Objects.equals(name, other.name)
             && salary == other.salary && Objects.equals(hireDay, other.hireDay);
       }
     
       public int hashCode()
       {
          return Objects.hash(name, salary, hireDay);
       }
     
       public String toString() //把其他类型的数据转换为字符串类型的数据
       {
          return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay="
             + hireDay + "]";
       }
    }

     

      程序运行结果如下:

     

     

     

    5-10程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    package equals;
     
    public class Manager extends Employee  //扩展了一个子类Manager
    {
       private double bonus; //创建一个私有属性
     
       public Manager(String name, double salary, int year, int month, int day)//定义变量
       {
          super(name, salary, year, month, day);//调用了父类的构造器
          bonus = 0;
       }
     
       public double getSalary()
       {
          double baseSalary = super.getSalary();//更改器
          return baseSalary + bonus;
       }
     
       public void setBonus(double bonus)
       {
          this.bonus = bonus;
       }
     
       public boolean equals(Object otherObject)
       {
          if (!super.equals(otherObject)) return false;
          var other = (Manager) otherObject;
          // super.equals checked that this and other belong to the same class  用super.equals检查这个类和其他类是否属于同一个类
          return bonus == other.bonus;
       }
     
       public int hashCode()
       {
          return java.util.Objects.hash(super.hashCode(), bonus);
       }
     
       public String toString()//吧其他类型的数据转换为字符串类型的数据
       {
          return super.toString() + "[bonus=" + bonus + "]";
       }
    }

     

      程序运行结果如下:

     

    实验2:编程练习(20分)

     程序代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    import java.util.Scanner;
      
    abstract class Shape {
        double PI = 3.14;
      
        public abstract double getPerimeter();
      
        public abstract double getArea();
    }
      
    class Rectangle extends Shape {
        int wide, len;
      
        Rectangle(int a, int b) {
            wide = a;
            len = b;
        }
      
        @Override
        public double getPerimeter() {
            // TODO Auto-generated method stub
            return 2 * (wide + len);
        }
      
        @Override
        public double getArea() {
            // TODO Auto-generated method stub
            return wide * len;
        }
         
        public String toString(){
            return "[width=" + wide + ", length=" + len + "]";
        }
    }
      
    class Circle extends Shape {
        int radius;
      
        Circle(int _radius) {
            radius = _radius;
        }
      
        @Override
        public double getPerimeter() {
            // TODO Auto-generated method stub
            return radius * 2 * PI;
        }
      
        @Override
        public double getArea() {
            // TODO Auto-generated method stub
            return radius * radius * PI;
        }
      
        public String toString(){
            return "[radius=" + radius + "]";
        }
      
    }
      
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            in.nextLine();
            Shape A[] = new Shape[n];
            int k = 0, j = 0;
            double sumAllArea = 0, sumAllPerimeter = 0;
            for (int i = 0; i < n; i++) {
                String S = in.next();
                if (S.equals("rect")) {
                    int wide = in.nextInt(), len = in.nextInt();
                    in.nextLine();
                    A[i] = new Rectangle(wide, len);
                else if (S.equals("cir")) {
                    int radius = in.nextInt();
                    in.nextLine();
                    A[i] = new Circle(radius);
                }
                sumAllArea += A[i].getArea();
                sumAllPerimeter += A[i].getPerimeter();
            }
      
            System.out.println(sumAllPerimeter);
            System.out.println(sumAllArea);
            System.out.print("[");
            for (int i = 0; i < n; i++) {
                if(i != 0)
                    System.out.print(", ");
                if (A[i] instanceof Rectangle) {
                        System.out.print("Rectangle ");
                    System.out.print(A[i].toString());
                }
                else {
                        System.out.print("Circle ");
                    System.out.print(A[i].toString()); 
                }
            }
      
            System.out.println("]");
      
            for(int i = 0;i < n;i++) {
                if(A[i] instanceof Rectangle) {
                    System.out.println("class Rectangle,class Shape");
                }else {
                    System.out.println("class Circle,class Shape");
      
                }
            }
            in.close();
        }
    }

     

      程序运行结果如下:

     

    3. 实验总结:(10分)

    本次学习理解了Java程序设计中类与对象的关系,理解OO程序设计的特征:继承和多态,并学会采用继承定义类设计程序,掌握利用了父类定义子类的语法规则及对象使用要求。在完成实验过程中理解了继承的定义,掌握子类的定义要求,多态性的概念及用法,抽象类的定义及用途。通过自己动手敲代码让我更加深刻的理解了本章的知识。但本章知识比较多也比较重要,仍需要多多加强巩固,继续努力。

  • 相关阅读:
    第七周——Linux内核如何装载和启动一个可执行程序
    第十八章读书笔记
    第三章读书笔记
    第六周——分析Linux内核创建一个新进程的过程
    第五章读书笔记
    Linux内核分析——分析system_call中断处理过程
    第一二章读书笔记
    20145217《网络对抗》 Web安全基础实践
    20145217《网络对抗》web基础
    20145217《网络对抗》 MSF基础应用
  • 原文地址:https://www.cnblogs.com/G19990718/p/11631883.html
Copyright © 2011-2022 走看看