201871010102《面向对象程序设计(java)》第67周学习总结

博文正文开头:(2分)

项目

内容

这个作业属于哪个课程

https://www.cnblogs.com/nwnu-daizh/

这个作业的要求在哪里

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

作业学习目标

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

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

 一、类,超类和子类

1>继承:用已有类来构建新类的一种机制。当定义了一个新类继承了一个类时,一个新类就继承了这个类的方法和域,同时在新类中添加新的方法和域以适应新环境;

类继承的格式:class 新类名   extends  已有类名

2>多态性的概念:多态性泛指在程序中同一个符号在不同情况下具有不同解释的现象;

3>抽象类的定义:

    abstract   class   Person

      {

        public  abstract  String  getDescription();

      ......                   ↓       

      }      abstract  方法:只能声明,不能实现;

二、Object:所有类的超类

Object 类是Java中所有类的祖先——每一个类都由它扩展而来。在不给出超累的情况下,Java会自动把Object作为要定义的超类。

三、泛型数组列表

ArrayList是一个采用类型参数的泛型类。为指定数组列表保存元素的对象类型,需要用一对尖括号将数组元素的对象类名括起来加在后面;

ArrayList的定义:ArrayList<T> 对象 = new ArrayList<T>();

相当于一个容器。

四、对象包装器与自动装箱

所有基本数据类型都有与之对应的预定义类,它们被称为对象包装器。

五、参数数量可变的方法

用户自己可以定义可变参数的方法,并将参数指定为任意类型,甚至是基本类型;

六、枚举类

声明枚举类:public    enum  Grade{A、B、C、D、E},它包括一个关键字enum,一个新枚举类型的名字Grade定义的一组值,这里的值既不是整型,也不是字符型;

七、反射

反制机制可以用来:

1>在运行时分析类的能力;

2>在运行时查看对象

3>实现通用的数组代码能力

4>利用Method对象

八、继承的设计技巧

1>将公共操作和域放在超类;

2>不要使用受保护的域;

3>除非所有继承的方法都有意义,否则就不要使用继承;

4>在覆盖方法时,不要改变预期的行为;

第二部分:实验部分

1、实验目的与要求

(1) 理解继承的定义;

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

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

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

2、实验内容和步骤

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

1> 在elipse IDE中编辑、调试、运行程序5-1 —5-3(教材152页-153

2> 掌握子类的定义及用法;

3> 结合程序运行结果,理解并总结OO风格程序构造特点,理解Employee和Manager类的关系子类的用途,并在代码中添加注释;

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

 代码如下

ManageTest类

/**
 * This program demonstrates inheritance.
 * @version 1.21 2004-02-21
 * @author Cay Horstmann
 */
public class ManagerTest
{
   public static void main(String[] args)
   {
      // 创建一个 Manager 对象
      var boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      boss.setBonus(5000);

      var staff = new Employee[3];

      //用Manager and Employee 对象填充staff数组

      staff[0] = boss;
      staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
      staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

      //输出所有关于Employee 对象的信息
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
   }
}

Employee类

import java.time.*;

public class Employee
{
   private String name;
   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;
   }
}

Manage类

public class Manager extends Employee
{
   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);
      bonus = 0;
      //子类中多了一个成员变量
   }

   public double getSalary()
   {
      double baseSalary = super.getSalary();
      return baseSalary + bonus;
      //重写父类中的getSalary方法
   }

   public void setBonus(double b)
   {
      bonus = b;
   }
}

 运行结果如下:

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

1>编辑、编译、调试运行教材PersonTest程序(教材163页-165页);

2>掌握超类的定义及其使用要求;

3>掌握利用超类扩展子类的要求;

4>在程序中相关代码处添加新知识的注释;

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

Employee类

import java.time.*;

public class Employee extends 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);
   }

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

Person类

public abstract class Person
//抽象类
{
   public abstract String getDescription();
   private String name;

   public Person(String name)
   {
      this.name = name;
   }

   public String getName()
   {
      return name;
   }
}

Student类

public class Student extends Person
{
   private String major;

   /**
    * @param name the student's name
    * @param major the student's major
    */
   public Student(String name, String major)
   {
       //传递姓名给父类的管理者
      super(name);
      this.major = major;
   }

   public String getDescription()
   {
      return "a student majoring in " + major;
   }
}

PersonTest类

/**
 * 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];

      //用Student和Employee对象填充people数组
      people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
      people[1] = new Student("Maria Morris", "computer science");

      // 输出所有Person对象的名字和描述
      for (Person p : people)
         System.out.println(p.getName() + ", " + p.getDescription());
   }
}

运行结果如下:

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

1>编辑、编译、调试运行教材程序5-8、5-9、5-10,结合程序运行结果理解程序(教材174页-177页);

2>Object类的定义及用法;

3>在程序中相关代码处添加新知识的注释。

Employee类

import java.time.*;
import java.util.Objects;

public class Employee
{
   private String name;
   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)
   {
      // 查看对象是否相同的快速测试
      if (this == otherObject) return true;

      // 如果显式参数为空,则必须返回false
      if (otherObject == null) return false;

      // 如果类不匹配,就不能相等
      if (getClass() != otherObject.getClass()) return false;

      // 现在我们知道otherobject是一个非空雇员
      var other = (Employee) otherObject;

      // 测试字段是否具有相同的值
      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 + "]";
   }
}

Manage类

public class Manager extends Employee
{
   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检查这个和其他的是否属于同一个类
      return bonus == other.bonus;
   }

   public int hashCode()
   {
      return java.util.Objects.hash(super.hashCode(), bonus);
   }

   public String toString()
   {
      return super.toString() + "[bonus=" + bonus + "]";
   }
}

EqualTest类

/**
 * 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", 75000, 1987, 12, 15);
      var alice2 = alice1;
      var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
      var bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);

      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", 80000, 1987, 12, 15);
      var boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      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());
   }
}

运行结果如下:

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

• 定义抽象类Shape:

属性不可变常量double PI,值为3.14

方法:public double getPerimeter();public double getArea())。

• RectangleCircle继承自Shape类。

• 编写double sumAllArea方法输出形状数组中的面积和和double sumAllPerimeter方法输出形状数组中的周长和。

• main方法中

1输入整型值n,然后建立n个不同的形状。如果输入rect,则再输入长和宽。如果输入cir,则再输入半径。
2) 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。
3) 最后输出每个形状的类型与父类型使用类似shape.getClass()(获得类型),shape.getClass().getSuperclass()(获得父类型);

思考sumAllAreasumAllPerimeter方法放在哪个类中更合适?

输入样例:

3

rect

1 1

rect

2 2

cir

1

输出样例:

18.28

8.14

[Rectangle [width=1, length=1], Rectangle [width=2, length=2], Circle [radius=1]]

class Rectangle,class Shape

class Rectangle,class Shape

class Circle,class Shape

代码如下:

Shape类

public abstract class Shape {
    final double PI =3.14;
    
    public abstract double getPerimeter();
    
    public abstract double getArea();
    
    public abstract double getName();

}

Rectangle类

public class Rectangle extends Shape{

    static double width;
    static double length;
    
    public Rectangle(double w,double l){
        width = w;
        length = l;
    }
    
    public double getPerimeter(){
        double Perimeter = (width+length)*2;
        return Perimeter;
    }
    
    public double getArea(){
        double Area = width*length;
        return Area;
    }

    public String toString(){
        return getClass().getName() + "[ width=" + width + "]"+ "[length=" + length + "]";
    }
    
    public double getN(){
        double a=1;
        return a;
    }

    public double getName() {
        return 0;
    }
}

Circle类

public class Circle extends Shape {

    static double radius;
    
    public Circle(double r){    
        radius = r;
    }
    
    public double getPerimeter(){    
        double Perimeter = 2*PI*radius;
        return Perimeter;
    }
    
    public double getArea(){
        double Area = PI*radius*radius;
        return Area;
    }
    
    public String toString(){
        return getClass().getName() + "[radius=" + radius + "]";
    }

    public double getName(){
        return 0;
    }
}

Main类

import java.util.*;

public class Main {
    public static void main(String[] args){    
        
    Scanner in = new Scanner(System.in);
    String rect = "rect";
    String cir = "cir";
    int n;
    n= in.nextInt();
    double[] c = new double[n];
    Shape[] shapes= new Shape[n];
    for(int i=0;i<n;i++)
    {
        String in2 = in.next();
        if(in2.equals(rect))
        {
            double length = in.nextDouble();
            double width = in.nextDouble(); 
            shapes[i] = new Rectangle(width,length);
            c[i]=1;
        }
        if(in2.equals(cir)) 
        {
            double radius = in.nextDouble(); 
            shapes[i] = new Circle(radius);
            c[i]=2;
        }
    
    }
    System.out.println(sumAllPerimeter(shapes));
    System.out.println(sumAllArea(shapes));
    System.out.printf("[");
    for(int i=0;i<n;i++)
    {
        if(c[i]==1)
            System.out.printf("Rectangle["+"length:"+Rectangle.length+" "+Rectangle.width+"]");
        else
            System.out.printf("Circle["+"radius:"+Circle.radius+"]");
        if(n!=1&&i<n-1)
            System.out.printf(",");
    }
    System.out.println("]");
    for(Shape s:shapes) 
    {
        System.out.println(s.getClass()+", "+s.getClass().getSuperclass());
    }
    in.close();
}

    private static double sumAllPerimeter(Shape shape2[]) {
        double sum = 0;
        for(int i = 0;i<shape2.length;i++)
            sum+= shape2[i].getPerimeter();
        return sum;
    }
    
    private static double sumAllArea(Shape shape1[]) {
        double sum = 0;
        for(int i = 0;i<shape1.length;i++)
            sum+=shape1[i].getArea();
        return sum;
        }
    }

运行结果如下:

3. 实验总结:(10分)

通过本章知识的学习,我了解的类之间的继承关系,Object类,泛型数组列表,参数数量可变的方法,枚举类,以及反射等相关内容,在运行程序的过程中,加深了对它的理解。在自主完成作业的时候,我查了很多的资料,对他们的理解更近了一步。虽然终于中犯过很多的错误,甚至遇到过很多无从下手的题目,但最终都成功地解决了,并且完成了本次实验报告。通过这次实验,我意识到自己还有很多的不足之处,在今后的学习生活中,我会更加努力,不断进步。

 

 

 

 

 

原文地址:https://www.cnblogs.com/xiaobeike/p/11606597.html